iOS 10中的动画错误

时间:2016-09-16 20:32:16

标签: ios swift animation constraints ios10

从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?()
        })
    }
}

有谁知道为什么它不是动画?

1 个答案:

答案 0 :(得分:11)

在动画块中,您正在调用self.layoutIfNeeded(),其中self是您要设置动画的UIView实例。调用layoutIfNeeded()会重新绘制调用该方法的视图,如果是子视图则调用所有视图。在您的情况下,您不想重绘UIView,您想要重绘视图的超级视图。

如果在视图控制器中调用它们的函数会有意义并且正常工作但是因为它们是在UIView本身的扩展中调用的,所以你需要调用像view.superview?.layoutIfNeeded()这样的东西