UIView.animateWithDuration(5, animations: {
myLabel.textColor = UIColor.redColor()
})标签文本颜色会立即改变。
发布于 2014-12-20 06:04:35
尝尝这个
[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = [UIColor redColor];
} completion:^(BOOL finished) {
}];发布于 2014-12-20 06:01:41
我写下了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
})发布于 2014-12-20 06:06:28
即使您说接受的答案有效,(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)
}https://stackoverflow.com/questions/27577443
复制相似问题