ios - Swift 3 UIView 动画

标签 ios swift uiview uiviewanimation swift3

自从将我的项目升级到 swift 3 后,我的自动布局约束动画无法正常工作;更具体地说,它们是捕捉到新位置而不是动画。

UIView.animate(withDuration: 0.1,
               delay: 0.1,
               options: UIViewAnimationOptions.curveEaseIn,
               animations: { () -> Void in
                   constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
                   self.layoutIfNeeded()
    }, completion: { (finished) -> Void in
    // ....
})

我知道他们添加了 UIViewPropertyAnimator 类,但我还没有尝试过。

最佳答案

我对 swift 3 的最新更新也遇到了这个问题。

确切地说,无论何时您想要为 View 设置动画,您实际上都在该 View 的父 View 上调用了 layoutIfNeeded。

试试这个:

UIView.animate(withDuration: 0.1,
           delay: 0.1,
           options: UIViewAnimationOptions.curveEaseIn,
           animations: { () -> Void in
               constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
               self.superview?.layoutIfNeeded()
}, completion: { (finished) -> Void in
// ....
})

在过去,他们似乎对能够重新布局您想要动画的 View 一直很宽容。但是在文档中,您确实应该在 super View 中调用 layoutIfNeeded。

关于ios - Swift 3 UIView 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489925/

相关文章:

ios - 原型(prototype) tableviewcell 内的 UIPickerView 没有选择指示器

javascript - iOS 渐进式 Web 应用程序深色模式启动图像?

c - swift 等同于 C 中的头文件?

swift - 将 'PageSetupAccessory' 添加到 PDFDocument 的 PrintPanel

swift - 请告诉我如何简短地编写代码

objective-c - 调试和发布版本之间的不同行为

ios - 将地名标签添加到 iOS 中的 Google map 标记

ios - 在 WebView 中处理 Windows 身份验证 - Swift/iOS

iOS drawRect,在曲线上寻找点

ios - 在 UIView 中初始化 UIScrollView 显示警告 : 'Local declaration hids instance variable'