iOS 9破坏了UILabel的动画效果

5

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

在Swift 1.2/iOS8中,动画正常工作。但在iOS9中,它不会进行动画,标签会立即改变位置,就好像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不能被动画化?

1个回答

6
尝试添加以下内容:
self.titleLabel.setNeedsLayout();

在动画块之前。

2
通常情况下,当您更新视图上的约束时,应使用setNeedsLayout方法让iOS布局系统知道约束已更改。这是苹果的性能优化,以避免不必要地对视图进行布局。至于为什么它适用于UIButton,我不能确定。苹果可能在UIButton实现中调用setNeedsLayout。 - john_ryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接