iOS 10 中的动画错误

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()