更改链式动画中的 UIView 约束
Change UIView constraints in chained animations
我正在尝试创建 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)
})
它做同样的事情。
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)
我正在尝试创建 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)
})
它做同样的事情。
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)