//
// 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;
@property (assign, nonatomic) int nIndex;
@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:@"doudong"];
//取消动画
// [self.iconImg.layer removeAnimationForKey:@"doudong"];
}
- (void)swiptAnimation:(UISwipeGestureRecognizer *)gesture
{
CATransition *trans=[CATransition animation];
// trans.type=@"pageCurl";
if (gesture.direction==UISwipeGestureRecognizerDirectionRight) {
self.nIndex++;
if (self.nIndex>6) {
self.nIndex=6;
}
trans.subtype=kCATransitionFromRight;
trans.type=@"pageCurl";
}
else
{
self.nIndex--;
if (self.nIndex<3) {
self.nIndex=3;
}
trans.subtype=kCATransitionFromLeft;
// trans.type=@"pageunCurl";
trans.type=@"rippleEffect";
}
NSLog(@"swiptAnimantion %d",self.nIndex);
trans.duration=1;
[self.iconImg.layer addAnimation:trans forKey:nil];
NSString *path=[NSString stringWithFormat:@"IMG_000%d.jpg",self.nIndex];
UIImage *img=[UIImage imageNamed:path];
self.iconImg.image=img;
}
- (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(50, 50, 100, 100);
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];
UISwipeGestureRecognizer *swip=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiptAnimation:)];
swip.direction=UISwipeGestureRecognizerDirectionRight;
[self.iconImg addGestureRecognizer:swip];
UISwipeGestureRecognizer *swipleft=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiptAnimation:)];
swipleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.iconImg addGestureRecognizer:swipleft];
self.iconImg.userInteractionEnabled=YES;
self.nIndex=3;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)xScaleandRotate
{
CABasicAnimation *anim=[CABasicAnimation animation];
// anim.keyPath=@"transform";
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)xTransform
{
NSLog(@"xTransform");
CABasicAnimation *basic=[CABasicAnimation animation];
basic.duration=3;
basic.keyPath=@"transform.translation.y";
basic.toValue=@(200);
[self.myLayer addAnimation:basic forKey:nil];
basic.removedOnCompletion=NO;
basic.fillMode=kCAFillModeForwards;
}
- (void)xxxTransform//可以沿着x y z 三个方向走
{
CABasicAnimation *anim=[CABasicAnimation animation];
anim.keyPath=@"transform";
CATransform3D trans=CATransform3DMakeTranslation(200, 200, 0);
id transform3D=[NSValue valueWithCATransform3D:trans];
anim.toValue=transform3D;
anim.duration=2;
[self.myLayer addAnimation:anim forKey:nil];
}
- (void)xxxScale
{
NSLog(@"xTransform");
CABasicAnimation *basic=[CABasicAnimation animation];
basic.duration=3;
basic.keyPath=@"transform.scale.y";
basic.toValue=@(20);
[self.myLayer addAnimation:basic forKey:nil];
basic.removedOnCompletion=NO;
basic.fillMode=kCAFillModeForwards;
}
- (void)xxxRotae
{
NSLog(@"xTransform");
CABasicAnimation *basic=[CABasicAnimation animation];
basic.duration=3;
basic.keyPath=@"transform.translation";
basic.toValue=@(20);
basic.toValue=[NSValue valueWithCGSize:CGSizeMake(200, 200) ];
[self.myLayer addAnimation:basic forKey:nil];
basic.removedOnCompletion=NO;
basic.fillMode=kCAFillModeForwards;
basic.repeatCount=10;
}
- (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
[self xxxRotae];
// [self traceMove];
//[self xxTranslation];
}
@end