有关动画的总结


有关动画的总结


1.隐式动画CATransaction

    非rootLayer,即手动创建的layer都包含隐式动画;(position,bounds,backgroundColor,)

    UITouch * touch = touches.anyObject;

    

    //获取触摸点

    CGPoint point = [touch locationInView:touch.view];

    

    

    //改变myLayer的位置

    //修改动画属性

    [CATransactionbegin];

    

    //设置动画为否

    [CATransactionsetDisableActions:YES];

    //修改动画时间

//    [CATransaction setAnimationDuration:2];

    

    self.myLayer.position = point;//设置的属性要放在提交之前

    //提交

    [CATransactioncommit];

=====================================================================

2.基本动画  CABasicAnimation

#import "ViewController.h"


@interface ViewController ()

@property (nonatomic,weak) CALayer * redLayer;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //手工创建layer

    

    CALayer * redLayer = [CALayerlayer];

    

    //设置背景颜色

    redLayer.backgroundColor = [UIColorredColor].CGColor;

    

    //设置大小和位置

    redLayer.position = CGPointMake(100, 100);

    

    redLayer.bounds = CGRectMake(0, 0, 100, 100);

    

    [self.view.layeraddSublayer:redLayer];

    

    self.redLayer = redLayer;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //基本动画--旋转

    //1.创建动画对象

    CABasicAnimation * basic = [CABasicAnimationanimation];

    

    //2.设置属性 (默认绕着z轴旋转)

    basic.keyPath =@"transform.rotation";

    

    //2.1 设置结束值

    basic.toValue = @(M_PI_4);

    

    //2.2 设置动画时间

    basic.duration = 2;

    

    //设置动画结束后保持当前状态

    basic.removedOnCompletion =NO;

    

    basic.fillMode =kCAFillModeForwards;

    

    //3.添加到要作用的layer

    [self.redLayeraddAnimation:basic forKey:nil];

    

}

- (void) test02

{

    //基本动画 ---- 缩放

    

    //1.创建动画对象

    CABasicAnimation * basicAnimation = [CABasicAnimationanimation];

    

    //2.设置动画属性

    basicAnimation.keyPath = @"transform.scale";

    

    //2.1设置结束值

    //    basicAnimation.toValue = @(0.5);

    basicAnimation.byValue = @(0.5);

    

    //设置动画结束不要删除

    basicAnimation.removedOnCompletion = NO;

    

    //保持当前状态

    basicAnimation.fillMode =kCAFillModeForwards;

    

    //3.把动画添加到要做用的layer

    

    [self.redLayeraddAnimation:basicAnimation forKey:nil];

}

- (void) test01

{

    //1.创建基本动画的对象

    CABasicAnimation * basicAnimation = [CABasicAnimationanimation];

    

    //2.设置动画对象的属性

    basicAnimation.keyPath = @"position";

//    basicAnimation.keyPath = @"transform.translation";

    

    //2.1设置起始值

    //    basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 200)];

    

    //2.2设置结束值

    basicAnimation.toValue = [NSValuevalueWithCGPoint:CGPointMake(300,400)];

    

    //2.3 设置动画时间

    basicAnimation.duration = 3;

    //设置代理 (隐式代理)

    basicAnimation.delegate = self;

    

    //动画结束后不要删除

    basicAnimation.removedOnCompletion = NO;

    //保持当前状态

    basicAnimation.fillMode =kCAFillModeForwards;

    

    //3.把动画添加到要作用的layer

    [self.redLayeraddAnimation:basicAnimation forKey:nil];

}


//实现代理方法

- (void)animationDidStart:(CAAnimation *)anim

{

    NSLog(@"start---%@",NSStringFromCGPoint(self.redLayer.position));

}

//结束动画调用

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

//    self.redLayer.position = CGPointMake(300, 400);

    NSLog(@"stop---%@",NSStringFromCGPoint(self.redLayer.position));

}


=================================================================

3.关键帧动画 CAKeyframeAnimation


#import "ViewController.h"


@interface ViewController ()

@property (nonatomic,weak) CALayer * redLayer;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //手工创建layer

    

    CALayer * redLayer = [CALayerlayer];

    

    //设置背景颜色

    redLayer.backgroundColor = [UIColorredColor].CGColor;

    

    //设置大小和位置

    redLayer.position = CGPointMake(100, 100);

    

    redLayer.bounds = CGRectMake(0, 0, 100, 100);

    

    [self.view.layeraddSublayer:redLayer];

    

    self.redLayer = redLayer;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //图标抖动

    //1.创建动画对象

    CAKeyframeAnimation * keyframeAni = [CAKeyframeAnimationanimation];

    

    

    //2.设置动画属性

    keyframeAni.keyPath =@"transform.rotation";

    

    keyframeAni.values = @[@(-M_PI_4/4),@(M_PI_4/4),@(-M_PI_4/4)];

    

    //设置动画次数

    keyframeAni.repeatCount = CGFLOAT_MAX;

    

    //设置动画时间

    keyframeAni.duration = 0.1;

    

    //3.添加到要作用的layer

    

    [self.redLayeraddAnimation:keyframeAni forKey:nil];

    

}

- (void) test04

{

    //关键帧动画

    

    //1.创建动画对象

    CAKeyframeAnimation * keyframeAni = [CAKeyframeAnimationanimation];

    //设置属性

    keyframeAni.keyPath = @"position";

    

    //2.设置属性

    //    NSValue * value0 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

    //    NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(300, 100)];

    //    NSValue * value2 = [NSValue valueWithCGPoint:CGPointMake(300, 300)];

    //    NSValue * value3 = [NSValue valueWithCGPoint:CGPointMake(100, 300)];

    //    NSValue * value4 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

    ////    NSValue * value5 = [NSValue valueWithCGPoint:CGPointMake(300, 100)];

    //

    //    keyframeAni.values = @[value0,value1,value2,value3,value4];

    

    //创建路径对象(如果设置了path,value就被忽略了,path只对anchorpointposition起作用)

    UIBezierPath * path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(20,100, 300, 200)];

    

    keyframeAni.path = path.CGPath;

    

    //速度控制函数,控制动画的节奏

    keyframeAni.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    

    //设置动画时间

    keyframeAni.duration = 5;

    

    //3.把动画对象添加到要作用的layer

    

    [self.redLayeraddAnimation:keyframeAni forKey:nil];



4.组动画 CAAnimation

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //组动画

    //1.创建组动画对象

    CAAnimationGroup * animationGroup = [CAAnimationGroupanimation];

    

    //2.创建子动画

    //2.1 --------------缩放动画-------------------------

    CABasicAnimation * basicAni = [CABasicAnimationanimation];

    //设置属性

    basicAni.keyPath =@"transform.scale";

    

    basicAni.toValue = @(0.5);

    

    //2.1 --------------缩放动画-------------------------

    

    //2.2 --------------旋转动画-------------------------

    CABasicAnimation * rotationAni = [CABasicAnimationanimation];

    

    rotationAni.keyPath =@"transform.rotation";

    

    rotationAni.toValue = @(M_PI * 2 *100);

    

    //2.2 --------------旋转动画-------------------------

    

    

    //2.3--------------围绕椭圆旋转---------------------

    CAKeyframeAnimation * keyframeAni = [CAKeyframeAnimationanimation];

    

    //设置属性

    keyframeAni.keyPath = @"position";

    

    UIBezierPath * path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(20,100, 300, 200)];

    

    keyframeAni.path = path.CGPath;

    

    keyframeAni.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    //2.3--------------围绕椭圆旋转---------------------

    

    

    //3.把子动画添加到组动画中

    animationGroup.animations = @[basicAni,rotationAni,keyframeAni];

    

    

    //设置组动画时间

    animationGroup.duration = 5;

    

    //4.把组动画添加到要作用的layer

    [self.redLayeraddAnimation:animationGroup forKey:nil];

    

    

    

}



5.转场动画  CATransition

    /*

//1.创建动画对象

 CATransition * transition = [CATransition animation];

    

    //2.设置动画属性

    transition.type = @"rippleEffect";

//    transition.duration = 2;

    //判断方向

    if(swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft)

    {

        //向左滑 下一张

        self.index++;

        //向左滑 从右边进来

 transition.subtype = kCATransitionFromRight;

    }

    else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight)

    {

        //向右滑 上一张

        self.index--;

        

 transition.subtype = kCATransitionFromLeft;

    }

    

    if (self.index > 5)

    {

        self.index = 1;

    }

    else if (self.index < 1)

    {

        self.index = 5;

    }

    

    

    //拼接图片名字

    NSString * imageName = [NSString stringWithFormat:@"%d",self.index];

    

    self.imageView.image = [UIImage imageNamed:imageName];

    

//添加到要作用的laye

[self.view.layer addAnimation:transition forKey:nil];

     

    

}



6.UIView动画

    [UIVIew  animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>];

    //添加到view上的动画

    [UIViewtransitionWithView:self.viewduration:1.0options:options animations:nilcompletion:nil];


头尾式动画:

改变frame的值(不可以直接修改frame的属性,要把frame保存到一个临时变量,修改临时变量的值,在吧临时变量的值赋回去)cgrect  oldFrame = view.frame;

oldFrame.origin.x=10;


1.[UIView beginAnimations:nil context:nil];//开始动画

 2.view.frame=oldFrame;

3.[UIVIew commitAnimations];//如果设置了动画时间,可以不提交动画.



transform:

redView.transform=CGAffineTransformMakeRotation(弧度)//修改旋转角度

redView.transform=CGAffineTransformMakeScale(1,1)//缩放

redView.transform=CGAffineTransformMaketranslation(1,1)//平移

在上一次的基础上旋转一定弧度:CGAffineTransformRotate(上一次的弧度,每次旋转的弧度);


7.block动画

//通过动画的方式 1s后让它消失

        [UIView animateWithDuration:1.5 animations:^{

            imageView.alpha = 0;

        } completion:^(BOOL finished) {

            [imageView removeFromSuperview];

        }];



8.UIIMageView的帧动画

    UIImageView *imageV=[UIImageView alloc]initWithImage:[UIImage imageNamed:@""];

    NSMutableArray *imageArr=[[NSMutableArrayalloc]init];

    

    UIImage *image=[UIImageimageNamed:@""];

    [imageArr addObject:image];

    imageV.animationImages = imageArr;

    //设置动画

    imageV.animationDuration=2;//动画时长

    imageV.animationRepeatCount=2;//重复播放次数

    [imageV startAnimating];//开始动画

    [imageV stopAnimating];//停止动画

    [imageV isAnimating];//正在动画




你可能感兴趣的:(有关动画的总结)