UIView.animateWithDuration(5, animations: {
myLabel.textColor = UIColor.redColor()
})
标签文字颜色立即改变
答案 0 :(得分:11)
试试这个
[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = [UIColor redColor];
} completion:^(BOOL finished) {
}];
答案 1 :(得分:5)
我写下了Objective-C
和Swift
动画类型
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
} NS_ENUM_AVAILABLE_IOS(4_0);
Objective-C
的编码
[UIView transitionWithView:myLabel duration:0.20 options: UIViewAnimationOptionTransitionFlipFromBottom animations:^{
myLabel.textColor = [UIColor redColor];
} completion:^(BOOL finished) {
}];
Swift
的编码
UIView.transition(with: myLabel, duration: 0.20, options: .transitionFlipFromBottom, animations: {() -> Void in
self.myLabel.textColor = UIColor.red
}, completion: {(_ finished: Bool) -> Void in
})
答案 2 :(得分:3)
即使你说接受的答案有效,(1)你需要使用TransitionCrossDissolve
代替CurveEaseInOut
; (2)在测试时,我注意到在Swift中,似乎你不能在动画制作之前立即添加子视图。 (我认为这是一个错误。)
在您的示例中看到myLabel
似乎是本地的(因为全局变量必须在块闭包中写为self.myLabel
),您很有可能在与动画相同的方法中添加了myLabel
子视图,没有延迟。
因此,如果您仍然遇到问题,我建议(1)使myLabel
全局和(2)在添加子视图和在该子视图上执行动画之间添加延迟,例如:
self.view.addSubview(myLabel)
var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "animate", userInfo: nil, repeats: false)
}
func animate() {
UIView.transitionWithView(myLabel, duration: 5.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.myLabel.textColor = UIColor.redColor()
}, completion:nil)
}