我正在尝试创建一个UIButton的链式动画,并在两个动画之间有延迟:
let firstAnimation: TimeInterval = 0.3
let secondAnimation: TimeInterval = 0.35
let delay: TimeInterval = 2.0
let animationDuration: TimeInterval = firstAnimation + secondAnimation + delay
UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: firstAnimation / animationDuration, animations: {
self.enterEqualWidthConstraint.isActive = false
self.enterWidthConstraint.constant = self.view.frame.width
self.enterWidthConstraint.isActive = true
self.enterButton.setTitle("HAVE FUN!", for: .normal)
print("now1")
self.view.layoutIfNeeded()
})
UIView.addKeyframe(withRelativeStartTime: delay / animationDuration, relativeDuration: secondAnimation / animationDuration, animations: {
print("now2")
self.enterWidthConstraint.constant = 0
self.enterButton.setTitle("ENTER", for: .normal)
self.view.layoutIfNeeded()
})
}, completion: nil)
}
输出同时打印:
now1
now2
甚至 self.enterWidthConstraint.constat 在延迟之前更改。
如何在两个链式动画之间有延迟?
如果约束在延迟之前发生变化,则UIButton内的文本消失。
我需要在这些之后做一些其他的动画,但是我被困在第一个。
更新 我尝试了没有关键帧的方法,但延迟不起作用:
UIView.animate(withDuration: 0.30, animations: {
self.enterEqualWidthConstraint.isActive = false
self.enterWidthConstraint.constant = self.view.frame.width
self.enterWidthConstraint.isActive = true
self.enterButton.setTitle("HAVE FUN!", for: .normal)
print("now1")
self.view.layoutIfNeeded()
}, completion: { _ in
UIView.animate(withDuration: 0.35, delay: 2.0, options: [], animations: {
print("now2")
self.enterButton.setTitle("ENTER", for: .normal)
self.enterWidthConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: nil)
})
它做同样的事情。
答案 0 :(得分:1)
UIView.animate(withDuration: 0.30, animations: {
self.enterEqualWidthConstraint.isActive = false
self.enterWidthConstraint.constant = self.view.frame.width
self.enterWidthConstraint.isActive = true
self.enterButton.setTitle("HAVE FUN!", for: .normal)
print("now1")
self.view.layoutIfNeeded()
}, completion: { _ in
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.enterButton.setTitle("ENTER", for: .normal)
UIView.animate(withDuration: 0.35, animations: {
print("now2")
self.enterWidthConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: nil)
})
});
另一种方法是启动两个独立的动画而不是链接它
UIView.animate(withDuration: 0.30, animations: {
self.enterEqualWidthConstraint.isActive = false
self.enterWidthConstraint.constant = self.view.frame.width
self.enterWidthConstraint.isActive = true
self.enterButton.setTitle("HAVE FUN!", for: .normal)
print("now1")
self.view.layoutIfNeeded()
}, completion: { _ in
self.enterButton.setTitle("ENTER", for: .normal)
});
UIView.animate(withDuration: 0.35, delay: 2.0, options: [], animations: {
print("now2")
self.enterWidthConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: nil)