#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
UIView *circleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,screenW , screenW)];
circleView.backgroundColor = [UIColor yellowColor];
circleView.layer.cornerRadius = screenW*0.5;
[self.view addSubview:circleView];
[self.view bringSubviewToFront:self.imageView];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建帧动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(10, 0)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(250, 50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(250, 250)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(50, 250)];
// 数组第一个是 “开始状态” 最后一个是 "结束状态"
animation.values = @[value1,value2,value3,value4,value1];
//设置时间
animation.duration = 5;
//设置动画节奏
//kCAMediaTimingFunctionEaseIn 先慢后快
//kCAMediaTimingFunctionEaseOut 先快后慢
//kCAMediaTimingFunctionLinear 线性匀速
//kCAMediaTimingFunctionEaseInEaseOut 中间快两边慢
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
#warning 内部的path的优先级大 values优先级小
//设置路径
CGMutablePathRef path = CGPathCreateMutable();
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 0, screenW, screenW));
animation.path = path;
//c语言的数据类型 如果creat/copy/retain创建要释放
CFRelease(path);
//添加动画
[self.imageView.layer addAnimation:animation forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|