C语言做渐变动画,OC 颜色渐变动画和动画组

#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

#import "ViewController.h"

@interface ViewController (){

UIView * _v;

NSNumber * _from;

NSNumber * _to;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

_v = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

_v.backgroundColor = [UIColor redColor];

[self.view addSubview:_v];

UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 80, 40)];

[btn setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];

[btn setTitle:@"开始" forState:(UIControlStateNormal)];

[btn addTarget:self action:@selector(beginBtn:) forControlEvents:(UIControlEventTouchUpInside)];

[self.view addSubview:btn];

UIButton * btn2 = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 80, 40)];

[btn2 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];

[btn2 setTitle:@"结束" forState:(UIControlStateNormal)];

[btn2 addTarget:self action:@selector(beginBtn:) forControlEvents:(UIControlEventTouchUpInside)];

[self.view addSubview:btn2];

_from = 0;

_to = [NSNumber numberWithFloat:M_PI_4];

}

- (void)beginBtn:(UIButton *)sender{

[self animations:sender from:_from to:_to];

}

-(void)animations:(UIButton *)sender from:(NSNumber *)from to:(NSNumber *)to{

CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];

CABasicAnimation * ani2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

if ([sender.currentTitle isEqualToString:@"开始"]) {

ani.fromValue = (__bridge id _Nullable)(([UIColor redColor].CGColor));

ani.toValue = (__bridge id _Nullable)([UIColor greenColor].CGColor);

ani2.fromValue = from;

ani2.toValue = to;

}else{

ani.fromValue = (__bridge id _Nullable)([UIColor greenColor].CGColor);

ani.toValue = (__bridge id _Nullable)([UIColor redColor].CGColor);

ani2.fromValue = to;

ani2.toValue = from;

}

CAAnimationGroup * groupAni = [[CAAnimationGroup alloc] init];

groupAni.animations = @[ani,ani2];

groupAni.fillMode = kCAFillModeForwards;

groupAni.removedOnCompletion = false;

groupAni.duration = 1;

[_v.layer addAnimation:groupAni forKey:@"backgroundColor"];

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值