更改链接动画中的UIView约束 [英] Change UIView constraints in chained animations
本文介绍了更改链接动画中的UIView约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建UIButton的链接动画,并且在两个动画之间存在延迟:
I'm trying to create a chained animation of a UIButton with a delay between the two animations:
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
now1
now2
甚至偶 self.enterWidthConstraint.constat 在延迟之前进行更改.
如何在两个链接的动画之间有一个延迟?
如果约束在延迟之前更改,则UIButton内的文本将消失.
在这些动画之后,我需要做其他一些动画,但是我被困在第一个动画中.
How can I have a delay between the two chained animations?
If the constraint changes before the delay the text that's inside the UIButton disappears.
I need to do some other animations after these ones but I'm stuck at the first ones.
更新
我尝试了没有关键帧的方法,但是延迟不起作用:
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)
})
它做同样的事情.
推荐答案
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)
})
});
另一种方法是开始制作两个独立的动画,而不是将其链接
another way is to start two independent animations instead of chaining it
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)
这篇关于更改链接动画中的UIView约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文