CALayer

作者: Amy莫莫 | 来源:发表于2017-02-13 16:30 被阅读15次

一、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;

相关文章

  • 动画 (1) ----- CALayer

    CALayer 和 UIView 联系CALayer的相关属性CALayer之隐式动画 一. CALayer 和 ...

  • CALayer与UIView的区别

    CALayer与UIView的区别 基础 CALayer的定义 CALayer的基础 CALayer和UIView...

  • UIView和CALayer

    1. UIView和CALayer CALayer负责显示内容contents UIView为CALayer提供现...

  • 设置view任意边框border

    CALayer *bottomBorder = [CALayer layer]; bottomBorder.f...

  • CALayer 简介

    CALayer1-简介CALayer2-创建新的层 CALayer3-层的属性 CALayer4-自定义层 CAL...

  • CALyer介绍

    CALayer1-简介CALayer2-创建新的层CALayer3-层的属性CALayer4-自定义层 注意点 第...

  • iOS-view圆角与阴影并存

    CALayer *subLayer = [CALayer layer];CGRect fixframe = vie...

  • CALayer(一)

    CALayer属性 view和layer的关系 CALayer属性表如下 CALayer和UIView的区别 1....

  • UIView和CALayer

    UIView和CALayer和有什么关系 UIview中有个属性layer,是CALayer类型。 CALayer...

  • 问题汇总

    1、UIView和CALayer区别UIView和CALayer的区别.UIView相对于CAlayer来说就多了...

网友评论

      本文标题:CALayer

      本文链接:https://www.haomeiwen.com/subject/vjhtwttx.html