[英]Animation bug in iOS 10
从iOS 10开始,我注意到动画布局更改( layoutIfNeeded()
)没有动画。 这是我的UIView扩展,可在iOS 9及以下版本上很好地工作。
func slideIn(from edgeConstraint: NSLayoutConstraint, withDuration duration: Double = 0.25, finishedAnimating: (() -> Void)? = nil) {
dispatch_async(dispatch_get_main_queue()) {
edgeConstraint.constant = 0
UIView.animateWithDuration(duration,
delay: 0.0,
options: .BeginFromCurrentState,
animations: { self.layoutIfNeeded() },
completion: { didComplete in
finishedAnimating?()
})
}
}
func slideOut(from edgeConstraint: NSLayoutConstraint, withDuration duration: Double = 0.25, finishedAnimating: (() -> Void)? = nil) {
dispatch_async(dispatch_get_main_queue()) {
edgeConstraint.constant = -self.frame.height
UIView.animateWithDuration(duration,
delay: 0.0,
options: .BeginFromCurrentState,
animations: { self.layoutIfNeeded() },
completion: { didComplete in
finishedAnimating?()
})
}
}
有谁知道为什么它没有动画?
在动画块中,您要调用self.layoutIfNeeded()
,其中self
是要设置动画的UIView
实例。 调用layoutIfNeeded()
重绘调用该方法的视图以及所有子视图。 在您的情况下,您不想重绘UIView
,而是想重绘视图的超级视图。
如果在视图控制器中调用这些函数,它们将很有意义并且可以正常工作,但是由于它们是在UIView
本身的扩展中调用的,因此您需要调用诸如view.superview?.layoutIfNeeded()
类的view.superview?.layoutIfNeeded()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.