Mic*_*are 73 iphone core-animation ios
这看起来应该可以,但不是.颜色立刻变为绿色.
self.labelCorrection.backgroundColor = [UIColor whiteColor];
[UIView animateWithDuration:2.0 animations:^{
self.labelCorrection.backgroundColor = [UIColor greenColor];
}];
Run Code Online (Sandbox Code Playgroud)
bos*_*acs 141
我找不到它的任何地方记录,但它出现的backgroundColor财产UILabel不能设置动画,因为你的代码工作正常与香草UIView.但是,只要您不设置标签视图本身的背景颜色,此hack似乎可以正常工作:
#import <QuartzCore/QuartzCore.h>
...
theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIView animateWithDuration:2.0 animations:^{
theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
} completion:NULL];
Run Code Online (Sandbox Code Playgroud)
Sur*_*gch 19
(重要)将UILabel的背景颜色设置为清除(在IB或代码中).例如:
override func viewDidLoad() {
super.viewDidLoad()
myLabel.backgroundColor = UIColor.clear
}
Run Code Online (Sandbox Code Playgroud)为图层背景颜色设置动画.
@IBAction func animateButtonTapped(sender: UIButton) {
UIView.animate(withDuration: 1.0, animations: {
self.myLabel.layer.backgroundColor = UIColor.red.cgColor
})
}
Run Code Online (Sandbox Code Playgroud)注意CGColor在之后添加UIColor.
结果
你可以动画它,但我必须首先(编程)将它设置为clearColor由于某种原因.没有它,动画要么不起作用,要么不可见.
我在自定义表格单元格中设置UILabel的背景颜色.这是willDisplayCell方法中的代码.我不会尝试在cellForRow中设置动画,因为很多布局都被表格修改了.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
((RouteListCell *)cell).name.backgroundColor = [UIColor clearColor];
[((RouteListCell *)cell).name backgroundGlowAnimationFromColor:[UIColor whiteColor] toColor:[UIColor redColor] clearAnimationsFirst:YES];
}
Run Code Online (Sandbox Code Playgroud)
这是我的动画例程:
-(void) backgroundGlowAnimationFromColor:(UIColor *)startColor toColor:(UIColor *)destColor clearAnimationsFirst:(BOOL)reset;
{
if (reset)
{
[self.layer removeAllAnimations];
}
CABasicAnimation *anAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
anAnimation.duration = 1.00;
anAnimation.repeatCount = HUGE_VAL;
anAnimation.autoreverses = YES;
anAnimation.fromValue = (id) startColor.CGColor; // [NSNumber numberWithFloat:1.0];
anAnimation.toValue = (id) destColor.CGColor; //[NSNumber numberWithFloat:0.10];
[self.layer addAnimation:anAnimation forKey:@"backgroundColor"];
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
要将标签背景颜色从白色设置为绿色,请按以下方式设置标签:
self.labelCorrection.backgroundColor = UIColor.clear
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
Run Code Online (Sandbox Code Playgroud)
像这样的动画:
UIView.animate(withDuration: 2.0) {
self.labelCorrection.layer.backgroundColor = UIColor.green.cgColor
}
Run Code Online (Sandbox Code Playgroud)
要返回原始状态,以便再次设置动画,请确保删除动画:
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
self.labelCorrection.layer.removeAllAnimations()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46116 次 |
| 最近记录: |