一、CALayer的常用属性

  • 1、@propertyCGPoint position; 
    图层中心点的位置,类似与UIView的center;用来设置CALayer在父层中的位置;以父层的左上角为原点(0,0);

  • 2、 @property CGPoint anchorPoint; 
    称『定位点』、『锚点』,该描述是相对于x、y位置比例而言的默认在图像中心点(0.5、0.5)的位置;决定着CALayer身上的哪个点会再position属性所指的位置,以自己的左上角为原点(0,0);它的x、y取值范围都是0~1。

  • 3、 @property(nullable) CGColorRef backgroundColor; 
    图层背景颜色

  • 4、 @property(nullable) CGColorRef borderColor; 
    图层边框颜色

  • 5、 @property CGFloat borderWidth; 
    图层边框宽度

  • 6、 @property CGRect bounds; 
    图层大小

  • 7、 @property(nullable, strong) id contents; 
    图层显示内容,例如可以将图片作为图层内容显示

  • 8、 @property CGRect contentsRect; 
    图层显示内容的大小和位置

  • 9、 @property CGFloat cornerRadius; 
    圆角半径

  • 10、 @property(getter=isDoubleSided) BOOL doubleSided; 
    图层背景是否显示,默认是YES

  • 11、 @property CGRect frame; 
    图层大小和位置,不支持隐式动画,所以CALyaer中很少使用frame,通常使用bound和position代替

  • 12、 @property(getter=isHidden) BOOL hidden; 
    是否隐藏

  • 13、 @property(nullable, strong) CALayer *mask; 
    图层蒙版

  • 14、 @property BOOL masksToBounds; 
    子图层是否剪切图层边界,默认是NO

  • 15、 @property float opacity; 
    图层透明度,类似与UIView的alpha

  • 16、 @property(nullable) CGColorRef shadowColor; 
    阴影颜色

  • 17、 @property CGSize shadowOffset; 
    阴影偏移量

  • 18、 @property float shadowOpacity; 
    阴影透明度,注意默认为0,如果设置阴影必须设置此属性

  • 19、 @property(nullable) CGPathRef shadowPath; 
    阴影形状

  • 20、 @property CGFloat shadowRadius; 
    阴影模糊半径

  • 21、 @property(nullable, copy) NSArray

二、CALayer不常用属性

  • 1、 @property CGFloat zPosition; 
    图层中心点在z轴中的位置

  • 2、 @property CGFloat anchorPointZ; 
    图层在z轴中的锚点;

  • 3、 - (CGAffineTransform)affineTransform;

  • 4、- (void)setAffineTransform:(CGAffineTransform)m; 
    以上属性为图层形变;该属性值指定一个CGAffineTransform对象,该对象代表对CALayer执行X、Y两个维度(也就是平面)上的旋转、缩放、位移、斜切、镜像等变换矩阵

  • 5、 @property(nullable, readonly) CALayer *superlayer; 
    图层的父图层

三、CALayer图层操作

  • 1、 - (void)addSublayer:(CALayer *)layer; 
    添加子图层

  • 2、 - (void)removeFromSuperlayer; 
    将自己从父图层中移除

  • 3、 - (void)insertSublayer:(CALayer *)layer atIndex:(unsigned)idx; 
    在自己子图层数组中的第idx位置添加图层

  • 4、 - (void)insertSublayer:(CALayer )layer below:(nullable CALayer )sibling; 
    将图层layer添加在子图层sibling的下面

  • 5、 - (void)insertSublayer:(CALayer )layer above:(nullable CALayer )sibling; 
    将图层layer添加在子图层sibling的上面

  • 6、 - (void)replaceSublayer:(CALayer )layer with:(CALayer )layer2; 
    将图层layer替换layer2;

四、CALayer动画操作

  • 1、 - (void)addAnimation:(CAAnimation )anim forKey:(nullable NSString )key; 
    图层添加某一属性的动画

  • 2、 - (nullable NSArray< NSString )animationKeys; 
    获取所有动画的属性

  • 3、 - (nullable CAAnimation )animationForKey:(NSString )key; 
    获取某一属性的动画

  • 4、 - (void)removeAnimationForKey:(NSString *)key; 
    移除某一属性动画

  • 5、 - (void)removeAllAnimations; 
    移除所有动画

五、隐式动画

ios中CALayer的设计主要是为了内容展示和动画操作,CALayer本身并不包含在UIKit中,它不能响应事件。由于CALayer在设计之初就考虑它的动画操作功能,CALayer很多属性在修改时都能形成动画效果,这种属性称为『隐式动画属性』。但是对于UIView的根视图层而言属性的修改并不形成动画效果,因为很多情况下根图层更多的充当容器的作用,如果它的属性变动形成动画效果会直接影响子图层。另外,UIView的根图层创建工作完全有iOS负责完成,无法重新创建,但是可以往根图层中添加子图层或移除子图层。

  • 1、如何查看CALayer的某个属性是否支持隐式动画

    • 可以查看头文件,看有没有Animatable,如果有表示支持; 
    • 查看官方文档
    • 下面标明都支持隐式动画
  • 2、例子

#pragma mark 绘制图层
-(void)drawMyLayer{
CGSize size=[UIScreen mainScreen].bounds.size;
//获得根图层
layer=[[CALayer alloc]init];
//设置背景颜色,由于QuartzCore是跨平台框架,无法直接使用UIColor
layer.backgroundColor=[UIColor colorWithRed:0 green:146/255.0 blue:1.0 alpha:1.0].CGColor;
//设置中心点
layer.position=CGPointMake(size.width/2, size.height/2);
//设置大小
layer.bounds=CGRectMake(0, 0, WIDTH,WIDTH);
//设置圆角,当圆角半径等于矩形的一半时看起来就是一个圆形
layer.cornerRadius=WIDTH/2;
//设置阴影
layer.shadowColor=[UIColor grayColor].CGColor;
layer.shadowOffset=CGSizeMake(2, 2);
layer.shadowOpacity=.9;
//设置边框
// layer.borderColor=[UIColor whiteColor].CGColor;
// layer.borderWidth=1;
//设置锚点
// layer.anchorPoint=CGPointZero;
[self.view.layer addSublayer:layer];
}
#pragma mark 点击放大
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
CGFloat width=layer.bounds.size.width;
if (width==WIDTH) {
width=WIDTH*4;
}else{
width=WIDTH;
}
layer.bounds=CGRectMake(0, 0, width, width);
layer.position=[touch locationInView:self.view];
layer.cornerRadius=width/2;
}

七、CALayer绘图

使用Quartz 2D绘图,是直接调用UIView的drawRect:方法绘制图形、图像,这种方式的本质还是再图层中绘制。drawRect:方法是由UIKit组件进行调用,因此厘米那可以使用到一些UIKir封装的方法进行绘制,而直接绘制到图层的方法由于并非UIKit直接调用因此只能用原生的Core Graphics方法绘制; 
图层绘制有两种方法,不管使用那种方法绘制完必须调用图层的setNeedDisplay方法,下面介绍图层的两种绘制方法:

(1)、通过图层代理drawLayer:inContext:方法绘制

通过代理方法进行图层呢个绘制只要指定图层的代理,然后在代理对象中重写 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;方法即可。需要注意这个方法虽然是代理方法但是不用手动实习那CALayerDelegate,因为CALayer定义中给NSObject做了分类扩展,所有的NSObject都包含这个方法。另外设置完代理后必须要调用图层的 - (void)setNeedsDisplay;方法,否测绘制内容无法显示; 
例如:

#pragma mark 绘制图层
-(void)drawMyLayer{
//自定义图层
CALayer *layer=[[CALayer alloc]init];
layer.bounds=CGRectMake(0, 0, PHOTO_HEIGHT, PHOTO_HEIGHT);
layer.position=CGPointMake(160, 200);
layer.backgroundColor=[UIColor redColor].CGColor;
layer.cornerRadius=PHOTO_HEIGHT/2;
//注意仅仅设置圆角,对于图形而言可以正常显示,但是对于图层中绘制的图片无法正确显示
//如果想要正确显示则必须设置masksToBounds=YES,剪切子图层
layer.masksToBounds=YES;
//阴影效果无法和masksToBounds同时使用,因为masksToBounds的目的就是剪切外边框,
//而阴影效果刚好在外边框
// layer.shadowColor=[UIColor grayColor].CGColor;
// layer.shadowOffset=CGSizeMake(2, 2);
// layer.shadowOpacity=1;
//设置边框
layer.borderColor=[UIColor whiteColor].CGColor;
layer.borderWidth=2;
//设置图层代理
layer.delegate=self;
//添加图层到根图层
[self.view.layer addSublayer:layer];
//调用图层setNeedDisplay,否则代理方法不会被调用
[layer setNeedsDisplay];
}
#pragma mark 绘制图形、图像到图层,注意参数中的ctx是图层的图形上下文,其中绘图位置也是相对图层而言的
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
// NSLog(@"%@",layer);//这个图层正是上面定义的图层
CGContextSaveGState(ctx);
//图形上下文形变,解决图片倒立的问题
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -PHOTO_HEIGHT);
UIImage *image=[UIImage imageNamed:@"001.png"];
//注意这个位置是相对于图层而言的不是屏幕
CGContextDrawImage(ctx, CGRectMake(0, 0, PHOTO_HEIGHT, PHOTO_HEIGHT), image.CGImage);
// CGContextFillRect(ctx, CGRectMake(0, 0, 100, 100));
// CGContextDrawPath(ctx, kCGPathFillStroke);
CGContextRestoreGState(ctx);
}

(2)、使用自定义图层绘图

在自定义图层绘图时只要自己编写一个类继承与CALayer然后在 - (void)drawInContext:(CGContextRef)ctx;中绘图即可。同前面在代理方法绘图一样,要显示图层中绘制的内容也要调用图层的 - (void)setNeedsDisplay;方法,否则 - (void)drawInContext:(CGContextRef)ctx;方法将不会调用; 
例如:

 KCALayer.h

#import "KCALayer.h"

@implementation KCALayer
-(void)drawInContext:(CGContextRef)ctx{
NSLog(@"3-drawInContext:");
NSLog(@"CGContext:%@",ctx);
// CGContextRotateCTM(ctx, M_PI_4);
CGContextSetRGBFillColor(ctx, 135.0/255.0, 232.0/255.0, 84.0/255.0, 1);
CGContextSetRGBStrokeColor(ctx, 135.0/255.0, 232.0/255.0, 84.0/255.0, 1);
// CGContextFillRect(ctx, CGRectMake(0, 0, 100, 100));
// CGContextFillEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
CGContextMoveToPoint(ctx, 94.5, 33.5);
//// Star Drawing
CGContextAddLineToPoint(ctx,104.02, 47.39);
CGContextAddLineToPoint(ctx,120.18, 52.16);
CGContextAddLineToPoint(ctx,109.91, 65.51);
CGContextAddLineToPoint(ctx,110.37, 82.34);
CGContextAddLineToPoint(ctx,94.5, 76.7);
CGContextAddLineToPoint(ctx,78.63, 82.34);
CGContextAddLineToPoint(ctx,79.09, 65.51);
CGContextAddLineToPoint(ctx,68.82, 52.16);
CGContextAddLineToPoint(ctx,84.98, 47.39);
CGContextClosePath(ctx);
CGContextDrawPath(ctx, kCGPathFillStroke);
}
@end

ViewController.m

#pragma mark 绘制图层
-(void)drawMyLayer{
KCALayer *layer=[[KCALayer alloc]init];
layer.bounds=CGRectMake(0, 0, 185, 185);
layer.position=CGPointMake(160,284);
layer.backgroundColor=[UIColor colorWithRed:0 green:146/255.0 blue:1.0 alpha:1.0].CGColor;
//显示图层
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
}

 
 

CALayer的属性和使用的更多相关文章

  1. IOS CALayer的属性和使用

    一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...

  2. iOS-绘制图层-CALayer的属性

    一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设 ...

  3. iOS Core Animation学习总结(1)--CALayer常用属性

    图层是core animation的基础, UIView之所以能显示在屏幕上,靠的是其内部的这个图层,即每个UIView 都有 CALayer,可通过UIView.layer或者[UIView lay ...

  4. 理解 CALayer ContentsCenter 属性

    http://aaronzjp.cn/2016/12/01/iOS-CALayer/ 这个属性和android 的 .9 文件类似,定义了图片的拉伸范围:例子中明显是四个角不拉伸,对于需要做背景,co ...

  5. 12.22笔记(关于CALayer//Attributes//CALayer绘制图层//CALayer代理绘图//CALayer动画属性//CALayer自定义子图层//绘图pdf文件//绘图渐变效果)

    12.22笔记 pdf下载文件:https://www.evernote.com/shard/s227/sh/f81ba498-41aa-443b-81c1-9b569fcc34c5/f033b89a ...

  6. CALayer的position,anchorPoint属性 与UIView的frame 属性

    彻底理解CALayer的position,anchorPoint属性 与UIView的frame 属性 一.position,anchorPoint两者都是CALayer的属性,都是CGPoint点 ...

  7. iOS开发——UI进阶篇(十七)CALayer,核心动画基本使用

    一.CALayer简介 1.CALayer在iOS中,文本输入框.一个图标等等,这些都是UIView你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个其实UIView之所以 ...

  8. IOS开发-属性动画和关键帧动画的使用

    CAMediaTiming是一个协议(protocol),CAAnimation是所有动画类的父类,但是它不能直接使用,应该使用它的子类. 继承关系: CoreAnmiation 核心动画 简写CA ...

  9. CALayer图层的基本介绍

    掌握 ● CALayer的基本属性 ● CALayer和UIView的关系 ● position和anchorPoint的作⽤用 CALayer ● 在iOS中,你能看得见摸得着的东西基本上都是UIV ...

随机推荐

  1. ubuntu opengl 开发

    开发环境: eclipse,需要安装C++开发插件,在自带的源中查找安装C++开发工具包即可 下载安装gl库: sudo apt-get install libgl1-mesa-dev 下载安装glu ...

  2. Linux系统编程----孤儿进程

    什么是孤儿进程? 孤儿进程,  指在父进程退出后,而子进程还在运行,这个子进程就成了孤儿进程,这时由init进程(pid=1)接管 来看看例子: #include <stdio.h> #i ...

  3. OpenCV特征点检测匹配图像-----添加包围盒

    最终效果: 其实这个小功能非常有用,甚至加上只有给人感觉好像人脸检测,目标检测直接成了demo了,主要代码如下: // localize the object std::vector<Point ...

  4. Android开发学习之路--传感器之初体验

    说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等.当然android手机之所以称为智能手机,少不了这几款传感器的功劳了.下面就学习下了,这里主要学习光照,加速度和磁. 新建工程emSenso ...

  5. iOS下WebRTC音视频通话(一)

    在iOS下做IM功能时,难免都会涉及到音频通话和视频通话.QQ中的QQ电话和视频通话效果就非常好,但是如果你没有非常深厚的技术,也没有那么大的团队,很难做到QQ那么快速和稳定的通话效果. 但是利用We ...

  6. [C++学习历程]基础部分 C++中的指针数组和结构

    作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/19938177 一.指针 对学习C++来说,指针是一项重要内容,以前,教 ...

  7. Uva - 11853 - Paintball

    先判断是否有解,从上到下dfs判断连通性,如果有从顶部到底部连通图,则无解.再判断最北的进出位置,从上边界开始遍历,沿途检查与边界相交的圆.这些圆的左边界的交点中最靠南边的一个就是所有的最北进入位置, ...

  8. leetcode 226 Invert Binary Tree 翻转二叉树

    大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...

  9. 分布式进阶(十二)Docker固定Container IP

    使用pipework工具. 前提:每个Container所做的工作现在还很少,可以不用save.commit. 为了便于通信,自定义一个网桥(192.168.1.180/24),使之IP与宿主主机IP ...

  10. Dynamics CRM2013 Lookup Filtering using addCustomFilter

    dynamics crm中对lookup视图的过滤是一个很平常性的需求,在2011的时候都是用添加自定义视图的方式例如下面这段示例代码 <span style="font-size: ...