【问题标题】:iOS 9 Breaks Animations of UILabeliOS 9 打破了 UILabel 的动画
【发布时间】:2015-09-08 16:33:54
【问题描述】:

我将我的代码从 Swift 1.2 迁移到 Swift 2.0

在 Swift 1.2/ iOS8 中,动画可以正常工作。但在 iOS 9 中,它不会对其进行动画处理,Label 会立即更改位置,就好像从未调用过 layoutIfNeeded

下面是我如何为我的 UILabel 和 UIButton 设置动画。 (我正在使用 Autolayout,这就是我使用 layoutIfNeeded 的原因)

使用 UILABEL(不起作用)

titleLabelTopConstraint.constant = 88;

UIView.animateWithDuration(0.8, delay: 0.38, usingSpringWithDamping: 0.54, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.titleLabel.layoutIfNeeded();
        }, completion: nil);

使用 UIBUTTON(它有效)

buttonTopConstraint.constant = 88;

UIView.animateWithDuration(0.8, delay: 0.38, usingSpringWithDamping: 0.54, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.button.layoutIfNeeded();
        }, completion: nil);

但是,如果我在 UIButton 上做同样的事情,它会起作用!

有什么想法吗?为什么 UILabel 不可动画?

【问题讨论】:

    标签: ios uiview swift2 ios9 xcode7


    【解决方案1】:

    尝试添加:

    self.titleLabel.setNeedsLayout();
    

    在动画块之前。

    【讨论】:

    • 一般来说,当你更新一个视图的约束时,你应该让 iOS 布局系统知道约束已经通过使用 setNeedsLayout 方法改变了。这是苹果的一项性能优化,因此他们不会尝试不必要地布局视图。至于为什么它适用于 UIButton,我不能肯定地说。 Apple 可能会在 UIButton 实现的底层调用 setNeedsLayout。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多