CAAnimation笔记

2016-04-28  本文已影响181人  选一个昵称呗

节选自:http://www.cnblogs.com/lee0oo0/p/4225730.html
设定动画的属性。以下是属性及其对应的说明:

C221A1BE-B020-41BF-B75F-4324E1A0011E.png
animation.duration = 2.5; // 动画持续时间  
animation.repeatCount = 1; // 不重复  
animation.beginTime = CACurrentMediaTime() + 2; // 2秒后执行  
animation.autoreverses = YES; // 结束后执行逆动画  
// 动画先加速后减速  
animation.timingFunction =  [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; 

设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。
3F3702E4-5460-4680-B21F-FA95056C96DE.png
// 指定position属性(移动)  
CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@"position"];  
// 设定动画起始帧和结束帧  
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 起始点  
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 终了点  




![DB8436BD-EE3B-42F6-AE6B-95091EF411C3.png](https://img.haomeiwen.com/i1766523/d664e17fb6a46dfb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *topView;
    @property (weak, nonatomic) IBOutlet UIImageView *bottomView;
    // 添加手势的View
    @property (weak, nonatomic) IBOutlet UIView *dragView;
    
    @property (nonatomic, weak) CAGradientLayer *gradientL;
    
    @end
    
    @implementation ViewController
    // 一张图片必须要通过两个控件展示,旋转的时候,只旋转上部分控件
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        // 通过设置contentsRect可以设置图片显示的尺寸,取值0~1
        //topView和bottomView的frame一样 通过设置铆点anchorPoint来调整
        _topView.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);
        _topView.layer.anchorPoint = CGPointMake(0.5, 1);
        
        _bottomView.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
        _bottomView.layer.anchorPoint = CGPointMake(0.5, 0);
        
        // 添加手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        
        [_dragView addGestureRecognizer:pan];
        
        // 渐变图层
        CAGradientLayer *gradientL = [CAGradientLayer layer];
        
        // 注意图层需要设置尺寸
        gradientL.frame = _bottomView.bounds;
        
        gradientL.opacity = 0;
        gradientL.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
        _gradientL = gradientL;
        
        [_bottomView.layer addSublayer:gradientL];
    }
    
    // 拖动的时候旋转上部分内容,200(垂直(Y)偏移量) M_PI(旋转180度)
    - (void)pan:(UIPanGestureRecognizer *)pan
    {
        // 获取偏移量
        CGPoint transP = [pan translationInView:_dragView];
        NSLog(@"%@", NSStringFromCGPoint(transP));
        // 旋转角度,往下逆时针旋转
        CGFloat angle = -transP.y / 200.0 * M_PI;
        
        CATransform3D transfrom = CATransform3DIdentity;
        
        
        // 增加旋转的立体感,近大远小,d:距离图层的距离
        transfrom.m34 = -1 / 500.0;
        
        transfrom = CATransform3DRotate(transfrom, angle, 1, 0, 0);
        
        _topView.layer.transform = transfrom;
        
        // 设置阴影效果
        _gradientL.opacity = transP.y * 1 / 200.0;
        
        
        if (pan.state == UIGestureRecognizerStateEnded) { // 反弹
            /**
             * 弹簧效果的动画
             * usingSpringWithDamping 参数:  usingSpringWithDamping的范围为0.0f到1.0f,数值越小「弹簧」的振动效果越明显。
             * initialSpringVelocity 参数: initialSpringVelocity则表示初始的速度,数值越大一开始移动越快。
             *  http://www.renfei.org/blog/ios-8-spring-animation.html
             */
            [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                _topView.layer.transform = CATransform3DIdentity;
                
            } completion:^(BOOL finished) {
                
            }];
        }
        
    }
    @end

E9E73E8C-2511-4656-94FC-9D9179D452B4.png F8B483F8-D235-4CD6-A220-1BFBEF203356.png
   #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *lightView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // CAReplicatorLayer复制图层,可以把图层里面所有子层复制
        // 创建复制图层
        CAReplicatorLayer *repL = [CAReplicatorLayer layer];
        repL.frame = _lightView.bounds;
        [_lightView.layer addSublayer:repL];
        
        //设置一个layer震动条
        CALayer *layer = [CALayer layer];
        layer.anchorPoint = CGPointMake(0.5, 1);
        layer.position = CGPointMake(15, _lightView.bounds.size.height);
        layer.bounds = CGRectMake(0, 0, 30, 150);
        layer.backgroundColor = [UIColor whiteColor].CGColor;
        [repL addSublayer:layer];
        
        //设置图层的动画
        CABasicAnimation *anim = [CABasicAnimation animation];
        anim.keyPath = @"transform.scale.y";
        anim.toValue = @0.1;
        anim.duration = 0.5;
        anim.repeatCount = MAXFLOAT;
        
        // 设置动画反转 动画结束时是否执行逆动画
        anim.autoreverses = YES;
        [layer addAnimation:anim forKey:nil];
        
        // 复制层中子层总数
        // instanceCount:表示复制层里面有多少个子层,包括原始层
        repL.instanceCount = 3;
        // 设置复制子层偏移量,不包括原始层,相对于原始层x偏移
        repL.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
        // 设置复制层动画延迟时间
        repL.instanceDelay = 0.1;
        // 如果设置了原始层背景色,就不需要设置这个属性
        repL.instanceColor = [UIColor redColor].CGColor;
        // 颜色值递减
        repL.instanceRedOffset = -0.3;
    }
    
    @end

45593372-4178-4EB6-925C-04765F9387AE.png
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *redView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        CAReplicatorLayer *repL = [CAReplicatorLayer layer];
        repL.frame = _redView.bounds;
        [_redView.layer addSublayer:repL];
        
        
        CALayer *layer = [CALayer layer];
        layer.transform = CATransform3DMakeScale(0, 0, 0);
        layer.position = CGPointMake(_redView.bounds.size.width / 2, 20);
        layer.bounds = CGRectMake(0, 0, 10, 10);
        layer.cornerRadius = 10.0;
        layer.masksToBounds = YES;
        layer.backgroundColor = [UIColor purpleColor].CGColor;
        [repL addSublayer:layer];
        
        // 设置缩放动画
        CABasicAnimation *anim = [CABasicAnimation animation];
        anim.keyPath = @"transform.scale";
        anim.fromValue = @1;
        anim.toValue = @0;
        anim.repeatCount = MAXFLOAT;
        CGFloat duration = 1;
        anim.duration = duration;
        
        [layer addAnimation:anim forKey:nil];
        
        
        int count = 30;
        CGFloat angle = M_PI * 2 / count;
        // 设置子层总数
        repL.instanceCount = count;
        repL.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
        repL.instanceDelay = duration / count;
        
    }
    @end
上一篇下一篇

猜你喜欢

热点阅读