#import "ViewController.h"
@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 类里面的属性, 而且在数学的注释里面, 要是有"Animatable"这个单词的才可以使用
CABasicAnimation *BA = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// 设置动画持续时间
BA.duration = 2.0f;
// 设置动画开始的值
BA.fromValue = @(0);
// 设置动画结束的值
BA.toValue = @(M_PI / 2);
// 核心动画一定要添加到 layer 里面 核心动画一定要添加到 layer 里面 核心动画一定要添加到 layer 里面
// 第一个参数代表: 哪一部分动画
// 第二个参数代表: 随便的一个 key 值, 这个 key 可以是任何值, 会在移除的时候使用
[self.myView.layer addAnimation:BA forKey:@"base"];
// 相关的一组动画
CAKeyframeAnimation *keyFA = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFA.duration = 5.0f; // 设置核心动画 持续时间
keyFA.repeatCount = 10; // 设置核心动画 重复次数
// 如果设置的是结构体类型的, 要把结构体构建成一个 NSValue
NSValue *value = [NSValue valueWithCGPoint:CGPointMake(0, 50)];
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(300, 0)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(0, 300)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(300,300)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(0, 600)];
keyFA.values = @[value, value1, value2, value3, value4];
[self.myView.layer addAnimation:keyFA forKey:@"base1"];
CAKeyframeAnimation *keyFA1 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// keyFA1.duration = 10.0f;
// keyFA1.repeatCount = 10;
id color1 = (id)[UIColor redColor].CGColor;
id color2 = (id)[UIColor greenColor].CGColor;
id color3 = (id)[UIColor yellowColor].CGColor;
id color4 = (id)[UIColor blackColor].CGColor;
id color5 = (id)[UIColor purpleColor].CGColor;
keyFA1.values = @[color1,color2,color3,color4,color5];
[self.myView.layer addAnimation:keyFA1 forKey:@"base2"];
// 播放一组没有关联的核心动画
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[keyFA, keyFA1];
group.duration = 10.0f;
group.repeatCount = 5;
[self.myView.layer addAnimation:group forKey:@"base3"];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 视图切换的效果
CATransition *transition = [CATransition animation];
//transition.type = `fade', `moveIn', `push' and `reveal'
transition.duration = 1.0f;
// 第一个代表的是动画的效果
transition.type = @"suckEffect";
// 第二个代表的是动画的方向
transition.subtype = kCATransitionFromBottom;
[self.myView.layer addAnimation:transition forKey:@"key"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end