核心动画
在ViewController.m中。
@interface ViewController ()
@property(nonatomic, strong) UIView * MyView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.MyView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.MyView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.MyView];
// 核心动画
// keypath 的参数,必须是在CALayer类里面的属性,而且在属性的注释里面要是有“Animatble”这个单词的才可以使用。
CABasicAnimation *BA = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//设置动画持续的时间
// BA.duration = 2.0f;
//设置动画开始的值
// BA.fromValue = @(0);
//设置动画结束的值
// BA.toValue = @(M_PI/2);
//核心动画一定要添加到layer里面
//第一个参数:代表的是哪一部分动画
//第二个参数:代表的是随便的一个KEY值,这个key值可以是任何值,会在移除的时候使用。
[self.MyView.layer addAnimation:BA forKey:@"base"];
//相关的一组动画
CAKeyframeAnimation * keyFA = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置核心动画的执行时间
keyFA.duration =3;
//设置核心动画重复执行的次数
keyFA.repeatCount =20;
// 如果 设置的属性是结构体类型的, 要把结构体构建成一个NSValue
NSValue * value = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
NSValue * value2= [NSValue valueWithCGPoint:CGPointMake(0, 300)];
NSValue * value3= [NSValue valueWithCGPoint:CGPointMake(0, 400)];
keyFA.values = @[value,value1,value2,value3];
[self.MyView .layer addAnimation:keyFA forKey:@"base1"];
//
//
CAKeyframeAnimation * KeyFA1 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// KeyFA1.duration =3;
// KeyFA1.repeatCount = 20;
id color = (id)[UIColor redColor].CGColor;
id color1 = (id)[UIColor yellowColor].CGColor;
id color2 = (id)[UIColor cyanColor].CGColor;
id color3 = (id)[UIColor purpleColor].CGColor;
KeyFA1.values = @[color,color1,color2,color3];
[self.MyView.layer addAnimation:KeyFA1 forKey:@"base2"];
CAAnimationGroup * gtoup = [CAAnimationGroup animation];
gtoup.animations = @[keyFA,KeyFA1];
gtoup.duration = 10.0;
gtoup.repeatCount =3;
[self.MyView.layer addAnimation:gtoup forKey:@"base23"];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 视图切换的效果
CATransition * transition = [CATransition animation];
transition.duration = 1.0f;
//代表的是动画的效果,
transition.type = @"push";
//代表的是动画的方向,
transition.subtype = kCATransitionFromBottom;
[self.MyView.layer addAnimation:transition forKey:@"key"];
}