将UIButton的背景色从WhiteColor设置为RedColor [英] Animate Background Color Of UIButton from WhiteColor to RedColor

查看:46
本文介绍了将UIButton的背景色从WhiteColor设置为RedColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一种颜色脉冲效果,以动画化 UIButton 的背景色,以使其从一种颜色(白色)连续更改为另一种颜色(红色).我正在尝试使用 CABasicAnimation 更改不透明度,但是我也无法使其与颜色配合使用.

I'm trying to making a kind of color pulse effect animating background color of a UIButton to make it change continously from a color (WhiteColor) to another one (RedColor). I'm trying to use CABasicAnimation for changing Opacity but I can't make it work with color too.

    CABasicAnimation *theAnimation;

    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    theAnimation.duration=1.0;
    theAnimation.repeatCount=HUGE_VALF;
    theAnimation.autoreverses=YES;
    theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
    theAnimation.toValue=[NSNumber numberWithFloat:0.0];
    [BigButton.layer addAnimation:theAnimation forKey:@"animateOpacity"];

每个建议都将不胜感激.谢谢

Every suggestion would be appreciated. Thanks

推荐答案

我找到了正确的方法.您需要记住CGColor.这是我的错编译器在这里没有帮助.

I found the right way. You need to remember about CGColor. This was my mistake. Compiler didn't help here.

客观C

CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"backgroundColor"];
theAnimation.duration=1.0;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue= [[UIColor redColor] CGColor];
theAnimation.toValue= [[UIColor blueColor] CGColor];
[BigButton.layer addAnimation:theAnimation forKey:@"ColorPulse"];

迅速

let colorAnimation = CABasicAnimation(keyPath: "backgroundColor")
colorAnimation.fromValue = UIColor.redColor().CGColor
colorAnimation.toValue = UIColor.blueColor().CGColor
colorAnimation.duration = 1
colorAnimation.autoreverses = true
colorAnimation.repeatCount = FLT_MAX
self.layer.addAnimation(colorAnimation, forKey: "ColorPulse")

这篇关于将UIButton的背景色从WhiteColor设置为RedColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆