自 iOS 10 以来,我注意到动 Canvas 局更改 ( 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?()
})
}
}
有谁知道为什么它没有动画?
最佳答案
在您的动画 block 中,您正在调用 self.layoutIfNeeded()
,其中 self
是您想要设置动画的 UIView
实例。调用 layoutIfNeeded()
重绘调用该方法的 View 以及所有 subview 。在你的例子中,你不想重绘 UIView
,你想重绘 View 的父 View 。
如果在 View Controller 中调用函数,它们将有意义并正常工作,但由于它们是在 UIView
本身的扩展中调用的,因此您需要调用类似 view 的方法。 superview?.layoutIfNeeded()
关于ios - iOS 10 中的动画错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39539691/