是否有可能animationUILabel的textcolor更改?
UIView.animateWithDuration(5, animations: { myLabel.textColor = UIColor.redColor() })
标签textcolor只是立即改变
尝试这个
[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ label.textColor = [UIColor redColor]; } completion:^(BOOL finished) { }];
我写下了Objective-C
和Swift
的代码
animationtypes
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 })
即使你说接受的答案有效,(1)你需要使用TransitionCrossDissolve
而不是CurveEaseInOut
; (2)在testing的时候,我注意到,在Swift中,好像在animation之前不能立即添加子视图。 (我认为这是一个错误。)
看起来myLabel
在你的例子中似乎是本地的(因为全局variables必须在blockclosures中写成self.myLabel
),所以很有可能你在animation的同一个方法中添加了myLabel
子视图,没有延迟。
所以,如果你仍然遇到问题,我build议(1)使myLabel
全球和(2)添加子视图和执行该子视图上的animation之间的延迟,例如:
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) }