[英]UISwitch suddenly changed background color from clearColor to whiteColor
[英]Animate Background Color Of UIButton from WhiteColor to RedColor
我正在尝试制作一种颜色脉冲效果来动画UIButton
背景颜色,使其从颜色(WhiteColor)连续变化到另一种颜色(RedColor)。 我正在尝试使用CABasicAnimation
来改变Opacity,但我也无法使用颜色。
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"];
我们将不胜感激。 谢谢
我找到了正确的方法。 你需要记住CGColor。 这是我的错。 编译器在这里没有帮助。
目标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")
我终于找到了解决方案:我制作了一个两帧动画,其中我首先将背景颜色更改为红色,而在第二帧中我将其变为白色。 我也可以操纵相对持续时间
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
someView.backgroundColor = [UIColor redColor];
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
someView.backgroundColor = [UIColor whiteColor];
}];
} completion:nil];
Swift 4 - 对按钮上的任何更改进行动画处理( backgroundColor
, title
, titleColor
):
UIView.transition(with: yourButton, duration: 0.3, options: .curveEaseInOut, animations: {
self.yourButton.backgroundColor = UIColor.red
self.yourButton.setTitle("new title", for: .normal)
self.yourButton.setTitleColor(.white, for: .normal)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.