IOS动画总结

IOS动画总结

重要: 使用QuartzCore的类来做动画,要注意:

例如: 改变self.abcView的位置和大小, 

在设置完动画之后, 其实动画改变的是layer的位置和大小, uiview本身的位置和大小并没有改变, 你可以点击layer, 保证不会有任何响应, 这里不能通过frame来设置self.abcView的大小!!

设置位置: self.center 或者 self.frame 都可以

但是设置大小: 必须 self.transform = CGaffineTransformMakeScale(0.5, 0.5); 

为什么不能通过frame来设置大小呢, 因为frame上设置大小会影响到动画的初始值, 这个是通过测试得出来的

[UIView animateWithDuration] 操作的是UIView的frame.origin

但是CABasicAnimation/CAKeyframeAnimation fromValue toValue 操作的是UIView的center而不是frame.origin

 

key不可重复,一般设置为nil

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

 

http://blog.sina.com.cn/s/blog_611b9d9d01015dkm.html

  

我们可以通过animationWithKeyPath键值对的方式来改变动画

 

animationWithKeyPath的值:

 

  transform.scale = 比例轉換

 

    transform.scale.x = 闊的比例轉換

 

    transform.scale.y = 高的比例轉換

 

    transform.rotation.z = 平面圖的旋轉

 

    opacity = 透明度

 

    margin

 

    zPosition

 

    backgroundColor    背景颜色

 

    cornerRadius    圆角

 

    borderWidth

 

    bounds

 

    contents

 

    contentsRect

 

    cornerRadius

 

    frame

 

    hidden

 

    mask

 

    masksToBounds

    opacity

    position

 

    shadowColor

 

    shadowOffset

 

    shadowOpacity

 

    shadowRadius

 

 
一.基本方式:使用UIView类的UIViewAnimation扩展
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 开始准备动画
+ (void)commitAnimations; // 运行动画

// 没有get方法,下面的set在快外调用无效
+ (void)setAnimationDelegate:(id)delegate; // 委托default = nil
+ (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2动画时间
+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0延迟多少时间开始执行动画
+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])动画开始日期?不知道啥用.- -
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut动画方式
+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. YES的话,动画(非最后一次)结束后动态复原到最开始状态
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. YES,停止之前的动画,从现在这里开始新动画the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // 添加动画到view上,cache是YES的时候比较高效,但是动画过程中不能更新界面上的内容,NO时每一帧都重新画,可以实时更新
+ (void)setAnimationsEnabled:(BOOL)enabled; // 是否忽略一些动画设置
+ (BOOL)areAnimationsEnabled;


一个动画的代码

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:2.7];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
// operation>>>
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// end<<<<<<
[UIView commitAnimations];

其中transition取值范围

typedef enum {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;

特点:基础,使用方便,但是效果有限

二.block方式:使用UIView类的UIViewAnimationWithBlocks扩展

函数说明

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);//间隔,延迟,动画参数(好像没用?),界面更改块,结束块

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion 

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations 

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void(^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); //toView added to fromView.superview, fromView removed from its superview界面替换,这里的options参数有效

举例:

[UIView animateWithDuration:0.7 delay:0 options:0 animations:^(){
self.view.alpha = 0.2;
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
self.view.alpha = 1;
} completion:^(BOOL finished)
{

}];

当areAnimationsEnabled为NO时,上面不能动画显示

[UIView transitionFromView:lImage toView:mImage duration:0.7 options:options completion:^(BOOL finished)
{
if (finished) {
[self.view addSubview:lImage];
[self.view sendSubviewToBack:lImage];
[self.view sendSubviewToBack:mImage];

}];

options取值范围

enum {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing

UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,

UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,//ios5
};
typedef NSUInteger UIViewAnimationOptions;

特点:快捷方便,效果更多.可以如上示例1那样实现界面个元素属性渐进变化的动态展示

对于不太复杂的动画,上面的写法很精练,因为只有一条语句,如果动画太过复杂了,写在这样一条语句中就会显得冗长了,对于代码调试没那么方非常方便。
三:Core animation
core animation是以objc语言封装的一套图形渲染,投影以及动画的库的结合,使创建的用户界面变得非常容易,通过以下方法:
1:使用简单的编程方法实现高性能的合成
2:使用层对象创建复杂的用户界面。
3:轻量型数据结构,能够同时使几百个层产生动画。
4:不依赖于应用的主线程,动画在单独的线程里运行
5:改进了应用程序性能。应用程序只需要重画它变化的部分(局部刷新)。
6:灵活的布局管理方式
2.    相关类
使用core animation,开发者不需要使用底层的API或者OpenGL就可以创建漂亮的动画界面。
core animation类分成以下几个:
1:提供显示内容的层
2:动画和时间类
3:布局和约束类
4:将多个层分成几个原子更新的执行类
2.1 层(layer)
层是core animation的基础,视图的一个实例,有一个CALayer实例作为父层(superlayer)以及在这层上添加的所有子层,创建的层结构成为layer tree
2.2动画和时间类
隐式动画
层的许多可视属性的改变可以产生隐式的动画效果,因为这些属性都默认与一个动画相关联。通过简单地设置可视的属性值,层会由当前值到被设置的值产生渐变的动画效果。比如,设置层的隐藏属性为真,将触发一个逐渐消失的动画效果。

显式动画
通过创建一个动画类和指定所需要的动画效果,显式的动画并不改变层的属性。
所有的核心动画类都由抽象类CAAnimation继承而来。CAAnimation采用CAMediaTiming协议。CAMediaTiming规定了动画的持续时间,速度以及重复次数。CAAnimation也采用了CAAction协议,该协议规定了在响应由层触发的动作时,开始一个动画的标准方式。

核心动画还提供其他的动画类
CATransition,
CATransition规定了影响整个层内容的转换效果,在动画期间,它使层产生渐变(fade),推拉(push)以及揭示(reveal)的动画效果。这些常用的转换效果可以通过核心绘图过滤器进行扩展。

CAAnimation,
CAAnimation允许大量的动画对象被分为几组,并且可以同时运行。
CAPropertyAnimation,是CAAnimation的子类,支持层在动画期间,为层指定一个关键路径。
CABasicAnimation.该类为层的属性提供了简单的插值。
CAKeyframeAnimation,
CAKeyframeAnimation提供关键帧动画的支持。你可以为层的属性指定一个关键路径
2.3布局管理类
CAKeyframeAnimation类用于管理所有子层的布局,每个由CAConstraint类封装的实例描述了各个子层之间的几何位置关系。
2.4 执行管理类
可设置动画层的属性的修改必须是执行的一部分,CATransaction负责将许多动画分成几批原子型的更新进行显示。
 
关键帧动画
CGMutablePathRef
    path = CGPathCreateMutable();
    //将路径的起点定位到(50  120)
    CGPathMoveToPoint(path,NULL,50.0,120.0);
    //下面5行添加5条直线的路径到path中
    CGPathAddLineToPoint(path,
                         NULL, 60, 130);
    CGPathAddLineToPoint(path,
                         NULL, 70, 140);
    CGPathAddLineToPoint(path,
                         NULL, 80, 150);
    CGPathAddLineToPoint(path,
                         NULL, 90, 160);
    CGPathAddLineToPoint(path,
                         NULL, 100, 170);
    //下面四行添加四条曲线路径到path
    
    //椭圆坐标, 左, 右, 中间 坐标
    CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
    CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
    CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
    CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);
    
    //以“position”为关键字
    //创建 实例
    CAKeyframeAnimation
    *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //设置path属性
    [animation
     setPath:path];
    [animation
     setDuration:3.0];
    //这句代码
    //表示 是否动画回到原位
//  [animation setAutoreverses:YES];
    CFRelease(path);
    [self.UnitIDTF.layer
     addAnimation:animation forKey:NULL];
利用values运行关键帧动画 CGPoint
    p1=CGPointMake(50, 120);
    CGPoint
    p2=CGPointMake(80, 170);
    CGPoint
    p3=CGPointMake(30, 100);
    CGPoint
    p4=CGPointMake(100, 190);
    CGPoint
    p5=CGPointMake(200, 10);
    NSArray
    *values=[NSArray arrayWithObjects:[NSValue valueWithCGPoint:p1],[NSValue valueWithCGPoint:p2],[NSValue valueWithCGPoint:p3],[NSValue valueWithCGPoint:p4],[NSValue valueWithCGPoint:p5], nil];
    CAKeyframeAnimation
    *animation
    = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [animation setValues:values];
    [animation
     setDuration:3.0];
    [animation
     setAutoreverses:YES];
    [self.UnitIDTF.layer
     addAnimation:animation forKey:NULL];
关键帧中,每一帧的时间
[animation
 setCalculationMode:kCAAnimationLinear]; [animation setKeyTimes:
[NSArray
 arrayWithObjects:
[NSNumber
 numberWithFloat:0.0],
[NSNumber
 numberWithFloat:0.25], [NSNumber numberWithFloat:0.50],
[NSNumber
 numberWithFloat:0.75], [NSNumber numberWithFloat:1.0], nil]];
 CAAnimation 简单Demo
#import "AnimationViewController.h"
static NSString * const AnimationKey = @"animationKey";
@interface AnimationViewController ()
{
    CALayer *_layer;
}
@end

@implementation AnimationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    arrSelSection = [[NSMutableArray alloc] initWithCapacity:0];
    arrSection = [[NSArray alloc] initWithObjects:@"CALayer",@"UIViewAnimation",@"CATransition",@"CABasicAnimation",@"CAKeyframeAnimation",@"CAAnimationGroup",@"CATransfrom3D", nil];
    arrLayerItem = [[NSArray alloc] initWithObjects:@"圆角红边框", nil];
    arrUIViewItem = [[NSArray alloc] initWithObjects:@"CurlUp(上翻页)",@"Flip(从左翻转)",@"block(下翻页)",@"block(从右翻转)", nil];
    arrCATransitionItem = [[NSArray alloc] initWithObjects:@"cube(立方体)",@"Push(推出)",@"Reveal(揭开)",@"MoveIn(覆盖)",@"Fade(淡出)",@"suckEffect(吸收)",@"oglFlip(翻转)",@"rippleEffect(波纹)",@"Open(镜头开)",@"Close(镜头关)", nil];
    arrCABasicAnimationItem = [[NSArray alloc]initWithObjects:@"scale(比例缩放)",@"opacity(透明)", nil];
    arrCAKeyframeAnimationItem = [[NSArray alloc]initWithObjects:@"border(边框闪动)",@"position(位置)", nil];
    arrCAAnimationGroup = [[NSArray alloc]initWithObjects:@"动画组", nil];
    arrCATransfrom3D = [[NSArray alloc] initWithObjects:@"UIView",@"Base",@"Keyframe",@"Affine", nil];
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height);
    tableView1 = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    tableView1.delegate = self;
    tableView1.dataSource = self;
    tableView1.backgroundColor = [UIColor clearColor];
    [tableView1.layer setBorderWidth:1];
    [tableView1.layer setBorderColor:[UIColor grayColor].CGColor];
    [self.view addSubview:tableView1];
    [tableView1 bringSubviewToFront:image];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 130, 30)];
    label.text = @"跳动文字";
    [label setTextAlignment:UITextAlignmentCenter];
    label.backgroundColor = [UIColor clearColor];
    [image addSubview:label];
    
//    _layer = [CALayer layer];
//    [_layer setBackgroundColor:[UIColor blueColor].CGColor];
//    [_layer setBounds:image.bounds];
//    CGPoint anchorPoint = {0.0, 0.0};
//    [_layer setAnchorPoint:anchorPoint];
//    [image.layer addSublayer:_layer];
    
}

- (void)viewDidUnload
{
    image = nil;
    image = nil;
    image = nil;
    animationDuration = nil;
    animationDurationLabel = nil;
    enableAnimation = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberO fRowsInSection:(NSInteger)section{
    NSInteger count = 0;
    for (int i = 0; i < arrSelSection.count; i++) {
        NSString *strSection = [NSString stringWithFormat:@"%@",[arrSelSection objectAtIndex:i]];
        NSInteger selSection = strSection.integerValue;
        if (section == selSection) {
            return 0;
        }
    }
    if(section == 0){
        count = arrLayerItem.count;
    }else if (section == 1) {
        count = arrUIViewItem.count;
    }else if(section == 2){
        count = arrCATransitionItem.count;
    }else if(section == 3){
        count = arrCABasicAnimationItem.count;
    }else if(section == 4){
        count = arrCAKeyframeAnimationItem.count;
    }else if(section == 5){
        count = arrCAAnimationGroup.count;
    }else if(section == 6){
        count = arrCATransfrom3D.count;
    }
    return count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return arrSection.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 20)];
    [view.layer setBorderWidth:1];
    UIButton *customView = [[UIButton alloc] initWithFrame:view.bounds];
    [customView.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [customView setBackgroundColor:[UIColor orangeColor]];
    [customView setTitle:[arrSection objectAtIndex:section] forState:UIControlStateNormal];
    
    [customView setTag:section];
    [customView addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:customView];
    
    BOOL isSelSection = NO;
    for (int i = 0; i < arrSelSection.count; i++) {
        NSString *strSection = [NSString stringWithFormat:@"%@",[arrSelSection objectAtIndex:i]];
        NSInteger selSection = strSection.integerValue;
        if (section == selSection) {
            isSelSection = YES;
            [customView setImage:[UIImage imageNamed:@"jiantou_up.png"] forState:UIControlStateNormal];
            break;
        }
    }
    if (!isSelSection) {
        [customView setImage:[UIImage imageNamed:@"jiantou_down.png"] forState:UIControlStateNormal];
    }
    [customView setImageEdgeInsets:UIEdgeInsetsMake(3, 145, 3, 0)];
    [customView setTitleEdgeInsets:UIEdgeInsetsMake(0, -25, 0, 15)];
    return view;
}

-(void)onClick:(id)sender{
    UIButton *btn = (UIButton *)sender;
    BOOL isSelSection = NO;
    tmpSection = btn.tag;
    for (int i = 0; i < arrSelSection.count; i++) {
        NSString *strSection = [NSString stringWithFormat:@"%@",[arrSelSection objectAtIndex:i]];
        NSInteger selSection = strSection.integerValue;
        if (tmpSection == selSection) {
            isSelSection = YES;
            [arrSelSection removeObjectAtIndex:i];
            break;
        }
    }
    if (!isSelSection) {
        [arrSelSection addObject:[NSString stringWithFormat:@"%i",tmpSection]];
    }
    UITableView *tableView = (UITableView *)[[btn superview] superview];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3];
    [tableView reloadData];
    [UIView commitAnimations];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    BOOL isSelSection = NO;
    for (int i = 0; i < arrSelSection.count; i++) {
        NSString *strSection = [NSString stringWithFormat:@"%@",[arrSelSection objectAtIndex:i]];
        NSInteger selSection = strSection.integerValue;
        if (section == selSection) {
            isSelSection = YES;
            break;
        }
    }
    if (!isSelSection) {
        NSString *item = nil;
        if (section == 0) {
            item = [arrLayerItem objectAtIndex:indexPath.row];
        }else if (section == 1) {
            item = [arrUIViewItem objectAtIndex:indexPath.row];
        }else if(section == 2){
            item = [arrCATransitionItem objectAtIndex:indexPath.row];
        }else if(section == 3){
            item = [arrCABasicAnimationItem objectAtIndex:indexPath.row];
        }else if(section == 4){
            item = [arrCAKeyframeAnimationItem objectAtIndex:indexPath.row];
        }else if(section == 5){
            item = [arrCAAnimationGroup objectAtIndex:indexPath.row];
        }else if(section == 6){
            item = [arrCATransfrom3D objectAtIndex:indexPath.row];
        }
        
        [cell.textLabel setFont:[UIFont systemFontOfSize:18]];
        cell.textLabel.text = item;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self setAnimation:indexPath];
}

-(void)setAnimation:(NSIndexPath *) indexPath{
    switch (indexPath.section) {
        case 0:{
            [self setLayerAnimation:indexPath.row];
        }
            break;
        case 1:
        {
            [self setUIViewAnimation:indexPath.row];
        }
            break;
        case 2:
        {
            [self setCATransition:indexPath.row];
        }
            break;
        case 3:
        {
            [self setCABasicAnimation:indexPath.row];
        }
            break;
        case 4:
        {
            [self setCAKeyframeAnimation:indexPath.row];
        }
            break;
        case 5:
        {
            [self setCAAnimationGroup:indexPath.row];
        }
            break;
        case 6:{
            [self setCATransfrom3D:indexPath.row];
        }
            break;
        default:
            break;
    }
}

-(void)setLayerAnimation:(NSInteger) row{
    switch (row) {
        case 0:
        {
            [CATransaction begin];
            [CATransaction setDisableActions:!enableAnimation.isOn];
            [CATransaction setAnimationDuration:animationDuration.value];
            [_layer setCornerRadius:[_layer cornerRadius]==0?30:0];
            [_layer setBorderWidth:[_layer borderWidth]==0?5:0];
            [_layer setBorderColor:[UIColor redColor].CGColor];
            [CATransaction commit];
        }
            break;
        default:
            break;
    }
    
}

//基础动画
-(void)setUIViewAnimation:(NSInteger) row{
    switch (row) {
        case 0://CurlUp
        {
            [UIView beginAnimations:@"animationID" context:nil];
            [UIView setAnimationDuration:animationDuration.value];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            [UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:image cache:YES];
            [UIView commitAnimations];
        }
            break;
        case 1://FlipFromLeft
        {
            [UIView beginAnimations:@"animationID" context:nil];
            [UIView setAnimationDuration:animationDuration.value];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            [UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:image cache:YES];
            [UIView commitAnimations];
        }
            break;
        case 2://block CurlDown
        {
            [UIView animateWithDuration:animationDuration.value animations:^(void){
                [UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
                [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:image cache:YES];
            }];
        }
            break;
        case 3://block FlipFromRight
        {
            [UIView animateWithDuration:animationDuration.value delay:.5 options:UIViewAnimationOptionCurveEaseOut animations:^(void){
                [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:image cache:YES];
            } completion:^(BOOL finish){
                animationDurationLabel.text = @"动画结束";
            }];
        }
            break;
        default:
            break;
    }
}

-(void)setCATransition:(NSInteger) row{
    switch (row) {
        case 0://cube
        {
            CATransition *transtion = [CATransition animation];
//            [transtion setStartProgress:0.5];
//            [transtion setEndProgress:0.6];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:@"cube"];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
            
        }
            break;
        case 1://kCATransitionPush
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:kCATransitionPush];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:AnimationKey];
        }
            break;
        case 2:
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:kCATransitionReveal];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        case 3:
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:kCATransitionMoveIn];
            [transtion setSubtype:kCATransitionFromTop];
            [transtion setDelegate:self];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        case 4://kCATransitionFade
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
            [transtion setType:kCATransitionFade];
            [transtion setSubtype:kCATransitionFromTop];
            [image setImage:[UIImage imageNamed:@"img.jpeg"]];
            [image.layer addAnimation:transtion forKey:AnimationKey];
        }
            break;
        case 5:
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:@"suckEffect"];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        case 6:
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:@"oglFlip"];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        case 7:
        {
            CATransition *transtion = [CATransition animation];
            [transtion setStartProgress:.2];
            [transtion setEndProgress:.8];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
            [transtion setType:@"rippleEffect"];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        case 8://cameraIrisHollowOpen
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:@"cameraIrisHollowOpen"];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        case 9://cameraIrisHollowClose
        {
            CATransition *transtion = [CATransition animation];
            transtion.duration = animationDuration.value;
            [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
            [transtion setType:@"cameraIrisHollowClose"];
            [transtion setSubtype:kCATransitionFromTop];
            [image.layer addAnimation:transtion forKey:@"transtionKey"];
        }
            break;
        default:
            break;
    }
}

-(void)setCABasicAnimation:(NSInteger) row{
    switch (row) {
        case 0:
        {
            CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            [basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
            [basic setFromValue:[NSNumber numberWithFloat:1]];
            [basic setToValue:[NSNumber numberWithFloat:.3]];
            [basic setDuration:animationDuration.value];
            [basic setDelegate:self];
            [image.layer addAnimation:basic forKey:AnimationKey];
            
        }
            break;
        case 1:
        {
            CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"opacity"];
            [basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
            [basic setFromValue:[NSNumber numberWithFloat:1]];
            [basic setToValue:[NSNumber numberWithFloat:.3]];
            [basic setDuration:animationDuration.value];
            [image.layer addAnimation:basic forKey:AnimationKey];
        }
            break;
        default:
            break;
    }
}

-(void)setCAKeyframeAnimation:(NSInteger) row{
    switch (row) {
        case 0:
        {
            CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"borderWidth"];
            keyframe.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0],
                               [NSNumber numberWithFloat:5],
                               [NSNumber numberWithFloat:10],
                               [NSNumber numberWithFloat:15],nil];
            keyframe.repeatCount = MAXFLOAT;
            keyframe.autoreverses = enableAnimation.isOn;
            keyframe.duration = animationDuration.value;
            [image.layer addAnimation:keyframe forKey:AnimationKey];
        }
            break;
        case 1://position
        {
            UIBezierPath *path = [UIBezierPath bezierPath];
            //            [path moveToPoint:image.frame.origin];
            [path moveToPoint:CGPointMake(image.frame.origin.x + image.frame.size.width/2, image.frame.origin.y + image.frame.size.height/2)];
            [path addLineToPoint:CGPointMake(image.frame.origin.x + image.frame.size.width/2,400)];
            [path addLineToPoint:CGPointMake(20, 400)];
            CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            keyframe.path = path.CGPath;
            keyframe.duration = animationDuration.value;
            [image.layer addAnimation:keyframe forKey:AnimationKey];
        }
            break;
        default:
            break;
    }
}

-(void)setCAAnimationGroup:(NSInteger) row{
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [basic setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    [basic setFromValue:[NSNumber numberWithFloat:1]];
    [basic setToValue:[NSNumber numberWithFloat:.3]];
    
    CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.x"];
    keyframe.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:M_PI], nil];
    
    [group setDuration:animationDuration.value];
    [group setAnimations:[NSArray arrayWithObjects:basic,keyframe, nil]];
    [image.layer addAnimation:group forKey:AnimationKey];
}

-(void)setCATransfrom3D:(NSInteger) row{
    switch (row) {
        case 0:
        {
            [UIView animateWithDuration:animationDuration.value animations:^{
                [UIView setAnimationRepeatCount:MAXFLOAT];
                [UIView setAnimationRepeatAutoreverses:enableAnimation.isOn];
                CATransform3D transform = CATransform3DMakeTranslation(0, -150, 0);
                CATransform3D trans = CATransform3DScale(transform, 1.5, 1.5, 10);
                [label.layer setTransform:trans];
            } completion:^(BOOL finished) {
                animationDurationLabel.text = @"finished";
            }];
        }
            break;
        case 1:{
            CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            [basic setDuration:animationDuration.value];
            [basic setRepeatCount:MAXFLOAT];
            [basic setAutoreverses:enableAnimation.isOn];
            
            NSValue *valueForm = [NSValue valueWithCATransform3D:CATransform3DIdentity];
            CATransform3D transTo = CATransform3DMakeScale(.5, .5, 0);
            NSValue *valueTo = [NSValue valueWithCATransform3D:transTo];
            
            [basic setFromValue:valueForm];
            [basic setToValue:valueTo];
            
            [image.layer addAnimation:basic forKey:AnimationKey];
        }
            break;
        case 2:{
            CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            [keyframe setRepeatCount:MAXFLOAT];
            [keyframe setDuration:animationDuration.value];
            [keyframe setAutoreverses:enableAnimation.isOn];
            
            CATransform3D transForm = CATransform3DIdentity;
            CATransform3D transTo = CATransform3DMakeScale(.5, .5, 0);
            NSValue *valueForm = [NSValue valueWithCATransform3D:transForm];
            NSValue *valueTo = [NSValue valueWithCATransform3D:transTo];
            
            [keyframe setValues:[NSArray arrayWithObjects:valueTo,valueForm,nil]];
            
            [image.layer addAnimation:keyframe forKey:AnimationKey];
        }
            break;
        case 3:{
            CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform"];
            [basic setRepeatCount:MAXFLOAT];
            [basic setDuration:animationDuration.value];
            [basic setAutoreverses:enableAnimation.isOn];
            
            NSValue *valueForm = [NSValue valueWithCATransform3D:CATransform3DIdentity];
            CGAffineTransform affine = CGAffineTransformMakeTranslation(0, -150);
            CATransform3D t = CATransform3DMakeAffineTransform(affine);
            CATransform3D trans = CATransform3DScale(t, 1.5, 1.5, 10);
            NSValue *valueTo = [NSValue valueWithCATransform3D:trans];
            
            [basic setFromValue:valueForm];
            [basic setToValue:valueTo];
            
            [label.layer addAnimation:basic forKey:AnimationKey];
        }
            break;
        default:
            break;
    }
}

-(void)animationDidStart:(CAAnimation *)anim{
    animationDurationLabel.text = @"开始动画";
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    animationDurationLabel.text = @"动画完毕";
}

- (IBAction)animationDurationChanged:(id)sender {
    [image.layer removeAnimationForKey:AnimationKey];
    [label.layer removeAllAnimations];
    animationDuration = (UISlider *)sender;
    [animationDurationLabel setText:[NSString stringWithFormat:@"%1.1f", animationDuration.value]];
}
原文地址:https://www.cnblogs.com/apem/p/4314635.html