简体   繁体   English

iOS 10中的动画错误

[英]Animation bug in iOS 10

Since iOS 10 I have noticed animating a layout change ( layoutIfNeeded() ) isn't animating. 从iOS 10开始,我注意到动画布局更改( layoutIfNeeded() )没有动画。 Here is my UIView extension that works great on iOS 9 and below. 这是我的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?()
        })
    }
}

Does anyone know why it isn't animating? 有谁知道为什么它没有动画?

In your animation blocks you're calling self.layoutIfNeeded() where self is the instance of UIView that you want to animate. 在动画块中,您要调用self.layoutIfNeeded() ,其中self是要设置动画的UIView实例。 Calling layoutIfNeeded() redraws the view that the method is called upon and all if it's subviews. 调用layoutIfNeeded()重绘调用该方法的视图以及所有子视图。 In your case, you don't want to redraw the UIView , you want to redraw the view's superview. 在您的情况下,您不想重绘UIView ,而是想重绘视图的超级视图。

Your functions would make sense and work properly if they were called in a view controller but since they are called in an extension on the UIView itself, you need to call something like view.superview?.layoutIfNeeded() 如果在视图控制器中调用这些函数,它们将很有意义并且可以正常工作,但是由于它们是在UIView本身的扩展中调用的,因此您需要调用诸如view.superview?.layoutIfNeeded()类的view.superview?.layoutIfNeeded()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM