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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,724评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,543评论 5 13
  • 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你...
    被吹落的风阅读 5,521评论 1 2
  • CALayer - 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮、一个文本标签、一个文本...
    Hevin_Chen阅读 4,832评论 0 10
  • 转载:http://www.cnblogs.com/jingdizhiwa/p/5601240.html 1.ge...
    F麦子阅读 5,696评论 0 1

友情链接更多精彩内容