- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 SnapKit 实现 2 个 View 的动画。
这是我的动画 View :
class MatchAnimation: UIView {
let viewBackground: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.75)
view.alpha = 1
return view
}()
let matchView: UIView = {
let view = UIView()
return view
}()
let matchLabel: UILabel = {
let label = UILabel()
label.text = "Title"
label.textColor = .white
label.textAlignment = .center
return label
}()
let leftAvatarBg: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 91/2
return view
}()
let rightAvatarBg: UIView = {
let view = UIView()
view.backgroundColor = .blue
view.layer.cornerRadius = 91/2
return view
}()
let goToChatButton: UIButton = {
let button = UIButton()
button.setTitle("Button", for: .normal)
button.backgroundColor = .red
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 24.5
return button
}()
init() {
super.init(frame: UIScreen.main.bounds)
viewBackground.frame = self.frame
self.addSubview(viewBackground)
self.addSubview(matchView)
matchView.addSubview(matchLabel)
matchView.addSubview(leftAvatarBg)
matchView.addSubview(rightAvatarBg)
matchView.addSubview(goToChatButton)
matchView.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.center.equalToSuperview()
}
matchLabel.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.centerX.equalToSuperview()
make.size.equalTo(CGSize(width: 193, height: 40))
}
leftAvatarBg.snp.makeConstraints { (make) in
make.top.equalTo(matchLabel.snp.bottom).offset(20)
make.size.equalTo(CGSize(width: 91, height: 91))
make.right.equalTo(self.snp.left).offset(0)
}
rightAvatarBg.snp.makeConstraints { (make) in
make.top.equalTo(leftAvatarBg)
make.size.equalTo(leftAvatarBg)
make.left.equalTo(self.snp.right).inset(0)
}
goToChatButton.snp.makeConstraints { (make) in
make.size.equalTo(CGSize(width: 171, height: 50))
make.top.equalTo(leftAvatarBg.snp.bottom).offset(25)
make.centerX.equalToSuperview()
make.bottom.equalToSuperview()
}
}
func animate() {
UIView.animate(withDuration: 5) {
self.leftAvatarBg.snp.updateConstraints { (make) in
make.right.equalTo(self.snp.left).offset(UIScreen.main.bounds.width/2+30)
}
self.rightAvatarBg.snp.updateConstraints { (make) in
make.left.equalTo(self.snp.right).inset(UIScreen.main.bounds.width/2+30)
}
self.layoutIfNeeded()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我尝试制作动画的 2 个 View 是 leftAvatarBg 和 rightAvatarBg。
在动画之前,我将它们设置在屏幕的外部,并想让它们在一个 View 中从左向右滑动,在另一个 View 中从右向左滑动。
在我的 Controller 中,我只是调用:
func setupAnimation() {
let matchView = MatchAnimation()
view.addSubview(matchView)
matchView.animate()
}
这样做的结果是整个 View 都在动画(缩放)。
我错过了什么吗?
更新:多亏了 swift2geek,它们似乎是对象的创建和动画之间的冲突。在他的解决方案中,他通过按下按钮来触发动画。就我而言,我想尽快自动触发动画。如何确保在创建对象后触发动画?
最佳答案
我对你的 bdsm SnapKit 不太满意,所以请自行设置正确的约束条件。所以它不起作用的主要原因 - 您应该将动画和对象创建分开。
你的 View Controller :
import UIKit
class ViewController: UIViewController {
@IBOutlet var matchButton: MatchButton!
@IBOutlet var matchView: MatchAnimation!
override func viewDidLoad() {
super.viewDidLoad()
setupAnimation()
setupButton()
}
func setupAnimation() {
matchView = MatchAnimation()
matchView.isUserInteractionEnabled = true
view.addSubview(matchView)
}
func setupButton() {
matchButton = MatchButton()
matchButton.isUserInteractionEnabled = true
matchButton.isEnabled = true
matchButton.addTarget(self, action: #selector(pressed(_:)), for: .touchUpInside)
matchView.addSubview(matchButton)
}
@objc func pressed(_ sender: MatchButton!) {
print("button tapped")
matchView.animate()
}
}
你的 MatchAnimation 类:
import UIKit
import SnapKit
class MatchAnimation: UIView {
let viewBackground: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.75)
view.alpha = 1
return view
}()
let matchView: UIView = {
let view = UIView()
return view
}()
let matchLabel: UILabel = {
let label = UILabel()
label.text = "Title"
label.textColor = .white
label.textAlignment = .center
return label
}()
let leftAvatarBg: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 91/2
return view
}()
let rightAvatarBg: UIView = {
let view = UIView()
view.backgroundColor = .blue
view.layer.cornerRadius = 91/2
return view
}()
init() {
super.init(frame: UIScreen.main.bounds)
viewBackground.frame = self.frame
self.addSubview(viewBackground)
self.addSubview(matchView)
matchView.addSubview(matchLabel)
matchView.addSubview(leftAvatarBg)
matchView.addSubview(rightAvatarBg)
matchView.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.center.equalToSuperview()
}
matchLabel.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.centerX.equalToSuperview()
make.size.equalTo(CGSize(width: 193, height: 40))
}
leftAvatarBg.snp.makeConstraints { (make) in
make.top.equalTo(matchLabel.snp.bottom).offset(20)
make.centerX.equalToSuperview()
make.size.equalTo(CGSize(width: 91, height: 91))
make.right.equalTo(self.snp.left).offset(90)
}
rightAvatarBg.snp.makeConstraints { (make) in
make.top.equalTo(leftAvatarBg)
make.size.equalTo(leftAvatarBg)
make.left.equalTo(self.snp.right).inset(120)
}
}
func animate() {
UIView.animate(withDuration: 5) {
self.leftAvatarBg.snp.updateConstraints { (make) in
make.right.equalTo(self.snp.left).offset(UIScreen.main.bounds.width/2+30)
}
self.rightAvatarBg.snp.updateConstraints { (make) in
make.left.equalTo(self.snp.right).inset(UIScreen.main.bounds.width/2+30)
}
self.layoutIfNeeded()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
和匹配按钮:
import UIKit
import SnapKit
class MatchButton: UIButton {
let goToChatButton: UIButton = {
let button = UIButton()
button.setTitle("Button", for: .normal)
button.backgroundColor = .red
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 24.5
return button
}()
init() {
super.init(frame: UIScreen.main.bounds)
self.addSubview(goToChatButton)
goToChatButton.snp.makeConstraints { (make) in
make.size.equalTo(CGSize(width: 171, height: 50))
make.top.greaterThanOrEqualTo(100)
make.centerX.equalToSuperview()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
关于ios - 使用 SnapKit 约束动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50650144/
我已经尝试了各种安装“Snapkit”的方法,但到目前为止我运气不佳。 我正在使用以下内容:- Xcode 8.3.3 到目前为止,我已经尝试按照所有在线文档来解决问题,但它仍然无法正常工作。包括以下
我有几个标签 应该有以下要求 最小宽度为 38 像素 右对齐 高度为 22 像素 每侧额外填充 8 像素的宽度 到目前为止,我设法使 1、2 和 3 正常工作(如图所示),但我在第四个约束上遇到了麻烦
以下是我的 viewDidAppear() 方法,我试图用它为我的应用程序构建功能原型(prototype)。 GraphicsView 实例变量通过 Storyboard绑定(bind)到我编写的
我用一个场景制作了 Storyboard。在我的 UIViewController 中,我使用 SnapKit 定义了以下内容: @IBOutlet weak var emailTextField:
我正在使用 Snapkit 来简化我的自动布局代码,但是似乎经常弹出一个场景,我想知道是否有一种方法可以减少代码。 假设我需要将 UIView 的边缘固定到它的 super View 边距,我们可能会
我尝试了不同的教程来使用 Snapkit 制作灯光动画,但似乎没有任何帮助。我什至尝试过 Spring 框架,但效果不佳。这是我的代码。 private func setupMatchView() {
我正在尝试在 SnapKit 中创建此布局的约束,但我很难设置最大大小 (lessThanOrEqual)。 let maxWidthContainer: CGFloat = 435 let maxH
我构建了一个应用程序,它不使用任何界面生成器,而是使用 Snapkit 创建自动布局约束。纵向时一切看起来都很好,但是一些屏幕需要一些特定于横向的限制。 我在这里和谷歌上搜索了关于执行此操作的快速介绍
只是尝试使用 SnapKit 自动布局添加 UILabel。我使用的代码如下: override func viewDidLoad() { super.viewDidLoad() se
我根据iPhone X版本编辑了我的应用程序。其他 iPhone 型号由于屏幕尺寸的原因此图像已损坏。如何根据 iPhone 版本编辑我定义的值?例如,我分享一个示例代码: registerlabel
我正在尝试为我的应用程序制作 Logo (UILabel) 从中间到顶部的动画。我尝试的是更新约束,但它似乎不起作用。问题是动画,即 Logo ,从原点 (0,0) 而不是 View 的中间到顶部。必
我想要的是这样的: 这是我的代码: let label1 = UILabel() let label2 = UILabel() label1.textColor = .whit
现在如果我想在左边和右边设置相等的边距,它需要两行: label.snp.makeConstraints { make in make.left.equalTo(Constants.margi
我目前正在将 SnapKit 与 Swift 结合使用,使用演练来制作测验应用程序。我目前正在制定一些约束,并想将一些新约束附加到我的约束数组中 我收到错误消息“元组类型 '()' 的值没有成员 an
当我尝试在创建约束后立即更新 snapkit 约束时,我得到一个Updated constraint could not find existing matching constraint to up
我是SnapKit的初学者,我想用SnapKit实现一个UITableViewController,每一行有两个UILabel,一个是Title,另一个是Value。我的问题是 UITableView
我正在尝试使用 SnapKit 实现 2 个 View 的动画。 这是我的动画 View : class MatchAnimation: UIView { let viewBackground
我有一个 TableViewCell 和两个按钮来切换不同的约束。 我想更新它的高度约束和单元格高度。 喜欢下面的图1 当我点击 buttonB 时, View 会像 pic2 然后我点击button
func setupPosition() { box.snp.makeConstraints{(make)->Void in make.edges.equalTo(vi
我有一个标签,无论使用什么设备,我都希望它在屏幕上具有相同的相对位置。例如。标签的位置距 View 上边距 10%,距 View 左边距 30%。 常量将始终进行定位,例如距离 View 边距 150
我是一名优秀的程序员,十分优秀!