详解iOS开发中的转场动画和组动画以及UIView封装动画

2020-01-14 17:05:03于丽

打印动画块的位置:

详解iOS开发中的转场动画和组动画以及UIView封装动画

(3).UIView封装的动画与CALayer动画的对比

使用UIView和CALayer都能实现动画效果,但是在真实的开发中,一般还是主要使用UIView封装的动画,而很少使用CALayer的动画。

CALayer核心动画与UIView动画的区别:
UIView封装的动画执行完毕之后不会反弹。即如果是通过CALayer核心动画改变layer的位置状态,表面上看虽然已经改变了,但是实际上它的位置是没有改变的。

代码示例:

复制代码
//
//  YYViewController.m
//  01-uiview封装动画
//
//  Created by apple on 14-6-22.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;


@end


复制代码
@implementation YYViewController

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   //1.创建核心动画
    CABasicAnimation *anima=[CABasicAnimation animation];
    //平移
    anima.keyPath=@"position";
    //设置执行的动画
    anima.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 300)];
    
    //设置执行动画的时间
    anima.duration=2.0;
    //设置动画执行完毕之后不删除动画
    anima.removedOnCompletion=NO;
    //设置保存动画的最新状态
    anima.fillMode=kCAFillModeForwards;
//    anima.fillMode=kCAFillModeBackwards;
    
    //设置动画的代理
    anima.delegate=self;