【发布时间】:2021-04-13 01:37:04
【问题描述】:
我希望我的 swift 代码使用单独和不同的动作来制作动画。那是按顺序排列的。前 2 个动画有效,但是当我添加第 3 个动画时,它不再有效并导致编译错误。您可以看到评论所在的第三个动画。我只想让所有 3 个动画都能正常工作。
import UIKit
class ViewController: UIViewController {
var block1 = UIView()
var btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[block1,btn].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
block1.backgroundColor = .yellow
btn.backgroundColor = .red
block1.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.1)
btn.frame = CGRect(x: 100, y: 300, width: 100, height: 100)
btn.backgroundColor = .red
btn.addTarget(self, action: #selector(diverse), for: .touchDown)
}
@objc func diverse(){
//1
UIView.animate(withDuration: 1, animations: {
self.block1.frame = CGRect(x: 100, y: 100, width: 100, height: 200)
self.block1.center = self.view.center
})
//2
{ done in
if done {
UIView.animate(withDuration: 11, animations: {
self.block1.frame = CGRect(x: 100, y: 100, width: 22, height: 100)
self.block1.center = self.view.center
})
}
}
//3 does not work
{ done in
if done {
UIView.animate(withDuration: 11, animations: {
self.block1.frame = CGRect(x: 100, y: 100, width: 2, height: 100)
self.block1.center = self.view.center
})
}
}
}
}
【问题讨论】:
标签: swift animation jquery-animate cgrect completion