zoukankan      html  css  js  c++  java
  • CALayer

    一、CALayer

    1、CALayer一般作为UIViewiew的容器使用

    2、CALayer是一个管理着图片载体的层结构

    3、直接修改单独创建出的CALayer的属性可以触发隐式动画

    4、UIview中的CALayer动画必须显示触发才能生效

     例一、

    @property(nonatomic,strong)CALayer * layer;

    - (void)viewDidLoad {

        [super viewDidLoad];

        UIView * containerView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 3)];

        containerView.backgroundColor=[UIColor redColor];

        [self.view addSubview:containerView];

        

        self.layer=[CALayer layer];

        self.layer.frame=CGRectMake(0, 0, 0, 3);

        self.layer.backgroundColor=[UIColor greenColor].CGColor;

        [containerView.layer addSublayer:self.layer];

        

        [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3.0f];

    }

    -(void)layerAnimation

    {

        self.layer.frame=CGRectMake(0, 0, 50, 3);

    }

    如图做隐式动画的缓冲进度条

     

    例二:

     自定义一个view

    ProgressView.h文件

    @interface ProgressView : UIView

    @property(nonatomic,assign)CGFloat progress;//进度参数

    @end

    ProgressView.m文件

    #import "ProgressView.h"

    /**

     存放不想让外部类访问的变量

     */

    @interface ProgressView ()

    @property(nonatomic,strong)CALayer * progressLayer;

    @property(nonatomic,assign)CGFloat currentViewWidth;

    @end

    @implementation ProgressView

    - (instancetype)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            self.progressLayer=[CALayer layer];

            self.progressLayer.frame=CGRectMake(0, 0, 0, frame.size.height);

            self.progressLayer.backgroundColor=[UIColor greenColor].CGColor;

            [self.layer addSublayer:self.progressLayer];

            

            //存储当前view的宽度值

            self.currentViewWidth=frame.size.width;

        }

        return self;

    }

    /**

     *  重写setter,getter方法

     */

    @synthesize progress=_progress;

    -(void)setProgress:(CGFloat)progress

    {

        _progress=progress;

        if (progress<=0) {

            self.progressLayer.frame=CGRectMake(0, 0, 0, self.frame.size.height);

        }else if(progress<=1)

        {

            self.progressLayer.frame=CGRectMake(0, 0, progress*self.currentViewWidth, self.frame.size.height);

        }

    }

    -(CGFloat)progress

    {

        return _progress;

    }

    @end

     viewcontroller里:

    @property(nonatomic,retain)ProgressView * progressview;

    @property(nonatomic,retain)NSTimer * timer;

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        self.progressview=[[ProgressView alloc]initWithFrame:CGRectMake(20, 100, 290, 5)];

        self.progressview.layer.borderWidth=1.0f;

        [self.view addSubview:self.progressview];

        

        [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3.0f];

        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(layerAnimation) userInfo:nil repeats:YES];

       }

     -(void)layerAnimation

    {

        self.progressview.progress=arc4random()% 100/100.f;

    }

    二、CALayer做渐变动画

    例一、

    - (void)viewDidLoad {

        [super viewDidLoad];

        UIImage * image1=[UIImage imageNamed:@"ic_empty"];

        self.imageLayer=[CALayer layer];

        self.imageLayer.frame=CGRectMake(0, 100, 100, 100);

        [self.view.layer addSublayer:self.imageLayer];

        

        self.imageLayer.contents=(__bridge id)((image1.CGImage));

        [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3.0f];

          }

    -(void)layerAnimation

    {

        UIImage * image2=[UIImage imageNamed:@"ic_error"];

        self.imageLayer.contents=(__bridge id )((image2.CGImage));

    }

    例二、

    - (void)viewDidLoad {

        [super viewDidLoad];

        UIImage * image1=[UIImage imageNamed:@"ic_empty"];

        self.imageLayer=[CALayer layer];

        self.imageLayer.frame=CGRectMake(0, 100, 100, 100);

        [self.view.layer addSublayer:self.imageLayer];

        

        self.imageLayer.contents=(__bridge id)((image1.CGImage));

        [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3.0f];

              }

    -(void)layerAnimation

    {

        //图片动画

        UIImage * image2=[UIImage imageNamed:@"ic_error"];

        CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"contents"];

        animation.fromValue=self.imageLayer.contents;

        animation.toValue=(__bridge id )((image2.CGImage));

        animation.duration=3.0f;

        

        //bounds动画

        CABasicAnimation * boundsAnimation=[CABasicAnimation animationWithKeyPath:@"bounds"];

        boundsAnimation.fromValue=[NSValue valueWithCGRect:self.imageLayer.bounds];

        boundsAnimation.toValue=[NSValue valueWithCGRect:CGRectMake(0, 100, 50, 50)];

        boundsAnimation.duration=3.0f;

        

        //组合动画

        CAAnimationGroup * groupAnimation=[CAAnimationGroup animation];

        groupAnimation.animations=@[animation,boundsAnimation];

        groupAnimation.duration=3.0f;

        

        //设定layer动画结束之后的值(必须设定,否则会恢复到动画之前的状态)

        self.imageLayer.contents=(__bridge id )((image2.CGImage));

        self.imageLayer.bounds=CGRectMake(0, 100, 50, 50);

        

        //提交动画

        [self.imageLayer addAnimation:groupAnimation forKey:nil];

        

    }

     创建遮罩

     self.imageContents=[UIImage imageNamed:@"ic_empty"];

        self.maskContents=[UIImage imageNamed:@"ic_error"];

        

        self.imageLayer=[CALayer layer];

        self.imageLayer.frame=CGRectMake(50, 50, 200, 200);

        self.imageLayer.contents=(__bridge id)((self.imageContents.CGImage));

        [self.view.layer addSublayer:self.imageLayer];

        

        self.maskLayer=[CALayer layer];

        self.maskLayer.frame=self.imageLayer.bounds;

      //  self.maskLayer.contents=(__bridge id )((self.maskContents.CGImage));

        self.maskLayer.backgroundColor=[UIColor blackColor].CGColor;

        

        self.imageLayer.mask=self.maskLayer;

  • 相关阅读:
    composer安装Workerman报错:Installation failed, reverting ./composer.json to its original content.
    从零开始搭建linux下laravel 5.5所需环境(三)
    ASPNET Razor 使用 @Ajax.BeginForm 需要注意到的细节
    .NET跨平台
    1172金币
    1044电子表
    1358统计号码牌
    1355疫情防控 数据调查
    1056反向输出一个三位数2
    1071行李托运
  • 原文地址:https://www.cnblogs.com/momosmile/p/5054455.html
Copyright © 2011-2022 走看看