ios - iOS 10 中的动画错误

标签 ios swift animation constraints ios10

自 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/

相关文章:

ios - 阻止未从方法调用

ios - 在现有项目中启动 React Native 后返回 native 应用程序

ios - 在 SpriteKit 中的 GameScene.swift 之前添加一个屏幕

ios - 如何使用 Swift 更改 iOS 8 自定义键盘高度?

javascript - 从右到左增加或增加 div 标签的宽度

javascript - JavaScript 中的颜色 Storm 文本效果

cocoa-touch - iOS 4.2 SDK 下载

ios - 检测标签栏项目选择

ios - 在 Swift 中访问 userInfo 字典

algorithm - 如何像在 MotionBuilder 中一样计算旋转