iOS CALayer 简单介绍
https://www.jianshu.com/p/09f4e36afd66
什么是CALayer:
总结:能看到的都是uiview,uiview能显示在屏幕上是因为它内部的一个层calyer层。
在创建uiview的时候,uiview的内部会自动创建一个层(calayer对象)通过uiview的layer属性可以访问这个层。当uiview需要显示在屏幕上时,会调用drawrect 方法进行绘制,并将所有的内容绘制在自己的层上,绘制完毕之后,系统会将层拷贝到屏幕上,于是uiview就显示了。
换句话说,uiview本身并不具备显示功能,它的内部的层才有显示功能。
CALayer的基本功能
通过操作CALayer对象,可以调整uiview的一些外观属性。比如阴影,圆角,边框的颜色等、
项目中的具体使用
1.做渐变。有时候项目中可能要用到一个渐变的图片,如果用图片的话 是会简单很多,但是也会相应的占用内存,增加开销,而Calayer的效率相对来说就会高很多。
下边附上代码:
- //1.渐变的简单实现demo
- UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 15, 100, 100)];
- bgView.layer.cornerRadius = 10;
- bgView.layer.masksToBounds = YES;
- bgView.backgroundColor =[UIColor blackColor];
- [self.view addSubview:bgView];
- CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
- gradientLayer2.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
- gradientLayer2.locations = @[@0.3, @0.4, @1.0];
- gradientLayer2.startPoint = CGPointMake(0, 0);
- gradientLayer2.endPoint = CGPointMake(1.0, 0);
- gradientLayer2.frame = CGRectMake(0, 0, 100, 100);
- [bgView.layer addSublayer:gradientLayer2];
2. 实现类似于加载图的加载效果。两种方式,一种是使用 n张图片去循环,这样的话对内存的开销比较大,不建议使用,第二种就是采用CALayer的相关属性 加上核心动画来实现:代码如下:
- //2.渐变转换为图形形成动画
- CALayer *layer = [CALayer layer];
- layer.backgroundColor = [UIColor redColor].CGColor; //圆环底色
- layer.frame = CGRectMake(100, 100, 110, 110);
- //
- //
- // //创建一个圆环
- UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(55, 55) radius:50 startAngle:0 endAngle:M_PI*2 clockwise:YES];
- //
- // //圆环遮罩
- CAShapeLayer *shapeLayer = [CAShapeLayer layer];
- shapeLayer.fillColor = [UIColor clearColor].CGColor;
- shapeLayer.strokeColor = [UIColor redColor].CGColor;
- shapeLayer.lineWidth = 5;
- shapeLayer.strokeStart = 0;
- shapeLayer.strokeEnd = 0.8;
- shapeLayer.lineCap = @"round";
- shapeLayer.lineDashPhase = 0.8;
- shapeLayer.path = bezierPath.CGPath;
- //
- // //颜色渐变
- NSMutableArray *colors = [NSMutableArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColor whiteColor].CGColor, nil];
- CAGradientLayer *gradientLayer = [CAGradientLayer layer];
- gradientLayer.shadowPath = bezierPath.CGPath;
- gradientLayer.frame = CGRectMake(50, 50, 60, 60);
- gradientLayer.startPoint = CGPointMake(0, 1);
- gradientLayer.endPoint = CGPointMake(1, 0);
- [gradientLayer setColors:[NSArray arrayWithArray:colors]];
- [layer addSublayer:gradientLayer]; //设置颜色渐变
- [layer setMask:shapeLayer]; //设置圆环遮罩
- [self.view.layer addSublayer:layer];
- //
- // //动画
- CABasicAnimation *scaleAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- scaleAnimation1.fromValue = [NSNumber numberWithFloat:1.0];
- scaleAnimation1.toValue = [NSNumber numberWithFloat:1.5];
- scaleAnimation1.autoreverses = YES;
- // scaleAnimation1.fillMode = kCAFillModeForwards;
- scaleAnimation1.duration = 0.8;
- CABasicAnimation *rotationAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- rotationAnimation2.fromValue = [NSNumber numberWithFloat:0];
- rotationAnimation2.toValue = [NSNumber numberWithFloat:6.0*M_PI];
- rotationAnimation2.autoreverses = NO;//此行是控制是否倒转,yes为倒转,no为不倒转
- rotationAnimation2.repeatCount = MAXFLOAT;
- rotationAnimation2.beginTime = 0.8; //延时执行,注释掉动画会同时进行
- rotationAnimation2.duration = 2;
- //
- //
- // //组合动画
- CAAnimationGroup *groupAnnimation = [CAAnimationGroup animation];
- groupAnnimation.duration = 4;
- // groupAnnimation.autoreverses = YES;
- groupAnnimation.animations = @[scaleAnimation1, rotationAnimation2];
- // groupAnnimation.animations = @[ rotationAnimation2];
- groupAnnimation.repeatCount = MAXFLOAT;
- [layer addAnimation:groupAnnimation forKey:@"groupAnnimation"];
下边附上以上代码的效果图:
iOS CALayer 简单介绍的更多相关文章
- iOS开发简单介绍
概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的iOS程序.但是这里我想强调一下,前面的 ...
- ios GCD简单介绍 后台运行~
本从实践出发简单说明: 首先,gcd是Grand Central Dispatch的缩写,意为多线程优化技术,是苹果为多核处理优化的技术.使用简单.清晰. 多线程就分同步.异步方法如下: //异步线程 ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- iOS开发多线程篇—多线程简单介绍
iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
- iOS开发UI篇—Modal简单介绍
iOS开发UI篇—Modal简单介绍 一.简单介绍 除了push之外,还有另外一种控制器的切换方式,那就是Modal 任何控制器都能通过Modal的形式展⽰出来 Modal的默认效果:新控制器从屏幕的 ...
- iOS开发网络篇—简单介绍ASI框架的使用
iOS开发网络篇—简单介绍ASI框架的使用 说明:本文主要介绍网络编程中常用框架ASI的简单使用. 一.ASI简单介绍 ASI:全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大 ...
- iOS开发数据库篇—SQLite简单介绍
iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1 ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
随机推荐
- Java面向对象详解-下
一. static static:静态的,可以用来修饰属性.方法.代码块(或初始化块).内部类 static修饰属性(类变量): 由类创建的所有的对象,都共用这一个属性 当其中一个对象对此属性进行修改 ...
- three.js 几何体(二)
上一篇简单的介绍了几何体的构造体参数,这一篇郭先生就更加详细的说一说(十分简单的几何体我就不说了). 1. ShapeGeometry形状几何体 形状几何体方便我们从一个或多个路径形状中创建一个单面多 ...
- web 部署专题(七):Ubuntu + Nginx + Flask + Gunicore环境搭建(服务器)
现在我们手里有一个准备发布的项目,那么如何将他上传到你的服务器,并让外网访问呢? 安装: 安装Flask pip3 install flask 安装UWSGI pip3 install uwsgi 安 ...
- Elasticsearch从入门到放弃:再聊搜索
在前文中我们曾经聊过搜索文档的方法,Elasticsearch 一般适用于读多写少的场景,因此我们需要更多的关注读操作. Elasticsearch 提供的 Search API 可以分为 URI S ...
- Python Hacking Tools - Port Scanner
Socket Programming 1. Scan the target Vulnerable Server. And test it by telnet. 2. Write the scanne ...
- P1866 编号
大致题意: 求太郎的n只兔子的编号的排列种数 基本思路: 每只兔子都有一个范围,那么每只兔子的范围就是这只兔子选择编号的种数. 以5 8为例,第一只兔子有5种,第二只兔子有8种,而第一只兔子选择后,第 ...
- JAVA中JSON字符串格式转换
alibabab版本 即com.alibaba.fastjson *以下Node为实体类 //JSON字符串->Map Map map1 = (Map)JSON.parse(strJson); ...
- [jvm] -- 类加载器及双亲委派模板篇
类加载器 JVM 中内置了三个重要的 ClassLoader BootstrapClassLoader(启动类加载器):最顶层的加载类,由C++实现,负责加载 %JAVA_HOME%/lib目录下的j ...
- CSS过渡时间
CSS过渡时间 基础知识 在了解CSS过渡时间之前,你应该先了解一下CSS的变形动画,可以参考之前的一篇博客. 我们的元素在属性发生变化时,如果没有特地的为它设置过渡时间,整个变化过程其实是以毫秒级别 ...
- 小白在使用ISE编写verilog代码综合时犯得错误及我自己的解决办法
一:错误原因,顶层信号声明类别错误 错误前 更改后 二:综合时警告 更改前: 错误原因:调用子模块时 输出端口只能用wire类型变量进行映射 这是verilog语法规定的 tx_done在uart_t ...