iOS 9打破了UILabel的动画

Jay*_*iyk 5 uiview ios ios9 xcode7 swift2

我将我的代码从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);
Run Code Online (Sandbox Code Playgroud)

使用UIBUTTON(IT WORKS)

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);
Run Code Online (Sandbox Code Playgroud)

但是如果我在UIButton上做同样的事情,那就行了!

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

joh*_*yan 6

尝试添加:

self.titleLabel.setNeedsLayout();
Run Code Online (Sandbox Code Playgroud)

在动画块之前.

  • 通常,当您更新视图上的约束时,您应该通过使用setNeedsLayout方法让iOS布局系统知道约束已更改.这是Apple的性能优化,因此他们不会尝试不必要地布局视图.至于它为什么适用于UIButton,我不能肯定地说.Apple可能会在UIButton实现中调用setNeedsLayout. (2认同)