iOS 9 中断 UILabel 的动画
iOS 9 Breaks Animations of UILabel
我将我的代码从 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 不可设置动画?
尝试添加:
self.titleLabel.setNeedsLayout();
在动画块之前。
我将我的代码从 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 不可设置动画?
尝试添加:
self.titleLabel.setNeedsLayout();
在动画块之前。