#import "ViewController.h"
#define View_Width self.view.frame.size.width
#define View_Height self.view.frame.size.height
@interface ViewController ()
{
CALayer *_layer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.view.backgroundColor=[UIColor greenColor];
_layer=[CALayer layer];
_layer.bounds=CGRectMake(0, 0, 60,60);
_layer.cornerRadius=30;
_layer.masksToBounds=YES;
//_layer.position=CGPointMake(View_Width-20, (View_Height-20*2)/2+20);
_layer.position=CGPointMake(View_Width/2, 20);
//_layer.backgroundColor=[UIColor orangeColor].CGColor;
_layer.contents=(id)[UIImage imageNamed:@"tree.png"].CGImage;
[self.view.layer addSublayer:_layer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.创建一个关键帧动画
CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
//2.创建一个路径
CGMutablePathRef path=CGPathCreateMutable();
CGPathMoveToPoint(path, nil, View_Width/2, 20);
CGPathAddCurveToPoint(path, nil, View_Width/2-350, View_Height/3, View_Width/2+360, View_Height/3*2, View_Width/2-20, View_Height);
animation.path=path;
animation.duration=10;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
//animation.autoreverses=YES;
//animation.repeatCount=MAXFLOAT;
//3.给_layer添加动画
[_layer addAnimation:animation forKey:nil];
CABasicAnimation *animation1=[CABasicAnimation animation];
animation1.keyPath=@"transform";
animation1.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 1)];
//取消反弹
animation1.removedOnCompletion=YES;
animation1.fillMode=kCAFillModeForwards;
animation1.duration=10;
animation1.autoreverses=YES;
animation1.repeatCount=1;
//3.给_layer添加动画
[_layer addAnimation:animation1 forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end