//
// ViewController.m
// 070401核心动画
//
// Created by tianshangrenjian on 15/7/4.
// Copyright © 2015年 tianshangrenjian. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic)CALayer *myLayer;
@end
@implementation ViewController
- (void)longPress
{
NSLog(@"longpress");
CAKeyframeAnimation *frame=[CAKeyframeAnimation animation];
CGFloat left=-M_PI_2*0.125;
CGFloat right=M_PI_2*0.125;
frame.keyPath=@"postion";
frame.keyPath=@"transform.rotation";
frame.values=@[@(left),@(right),@(left)];
frame.duration=0.1;
frame.repeatCount=10;
[self.iconImg.layer addAnimation:frame forKey:nil];
//取消动画
[self.iconImg.layer removeAnimationForKey:@"transform.rotation"];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myLayer=[[CALayer alloc ] init];
self.myLayer.frame=CGRectMake(0, 0, 40, 40);
self.myLayer.backgroundColor=[UIColor greenColor].CGColor;
self.myLayer.anchorPoint=CGPointMake(0, 0);//0----1 停靠点的一个比例
[self.schLayer.layer addSublayer:self.myLayer];
UILongPressGestureRecognizer *gesture=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
gesture.minimumPressDuration=3;
[self.iconImg addGestureRecognizer:gesture];
self.iconImg.userInteractionEnabled=YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)xScaleandRotate
{
CABasicAnimation *anim=[CABasicAnimation animation];
anim.keyPath=@"transform";
anim.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 2, 3)];
anim.duration=2;
anim.removedOnCompletion=NO;
anim.fillMode=kCAFillModeBackwards;
CABasicAnimation *rotate=[CABasicAnimation animation];
rotate.keyPath=@"transform";
rotate.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 3, 1, 0)];
rotate.removedOnCompletion=NO;
rotate.fillMode=kCAFillModeBackwards;
CABasicAnimation *rotate1=[CABasicAnimation animation];
rotate1.keyPath=@"transform";
rotate1.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 3, 10, 0)];
rotate1.removedOnCompletion=NO;
rotate1.fillMode=kCAFillModeBackwards;
CAAnimationGroup *group=[CAAnimationGroup animation];
group.animations=@[anim,rotate,rotate1];
group.duration=5;
[self.myLayer addAnimation:group forKey:nil];
}
//基本动画:平移 旋转 放大缩小
- (void)xxTranslation
{
CABasicAnimation *anim=[CABasicAnimation animation];
anim.keyPath=@"transform";
anim.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(100, 100, 30)];
anim.duration=3;
anim.removedOnCompletion=NO;//不删除动画状态
anim.fillMode=kCAFillModeForwards;//保持动画状态
[self.myLayer addAnimation:anim forKey:nil];
}
- (void)traceMove
{
CAKeyframeAnimation *key=[CAKeyframeAnimation animation];
NSValue *v1=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue *v2=[NSValue valueWithCGPoint:CGPointMake(0, 100)];
NSValue *v3=[NSValue valueWithCGPoint:CGPointMake(100, 100)];
NSValue *v4=[NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue *v5=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
key.keyPath=@"position";
// key.values=@[v1,v2,v3,v4];
key.values=@[v1,v2,v3,v4,v5];
key.duration=5;
key.autoreverses=YES;
key.removedOnCompletion=NO;
key.fillMode=kCAFillModeForwards;
[self.myLayer addAnimation:key forKey:nil];
}
- (void)traceMoveline//沿着线走
{
CAKeyframeAnimation *key=[CAKeyframeAnimation animation];
CGMutablePathRef path=CGPathCreateMutable();
CGPathAddArc(path, NULL, 100, 100, 50, 0, M_PI_2*2, 0);
key.path=path;
//
// NSValue *v1=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
// NSValue *v2=[NSValue valueWithCGPoint:CGPointMake(0, 100)];
// NSValue *v3=[NSValue valueWithCGPoint:CGPointMake(100, 100)];
// NSValue *v4=[NSValue valueWithCGPoint:CGPointMake(100, 0)];
// NSValue *v5=[NSValue valueWithCGPoint:CGPointMake(0, 0)];
key.keyPath=@"position";
// // key.values=@[v1,v2,v3,v4];
// key.values=@[v1,v2,v3,v4,v5];
key.duration=5;
key.autoreverses=YES;
key.removedOnCompletion=NO;
key.fillMode=kCAFillModeForwards;
[self.myLayer addAnimation:key forKey:nil];
}
- (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
[self traceMove];
//[self xxTranslation];
}
@end