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 简单介绍的更多相关文章

  1. iOS开发简单介绍

    概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的iOS程序.但是这里我想强调一下,前面的 ...

  2. ios GCD简单介绍 后台运行~

    本从实践出发简单说明: 首先,gcd是Grand Central Dispatch的缩写,意为多线程优化技术,是苹果为多核处理优化的技术.使用简单.清晰. 多线程就分同步.异步方法如下: //异步线程 ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. iOS开发多线程篇—多线程简单介绍

    iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...

  5. iOS开发UI篇—UITabBarController简单介绍

    iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...

  6. iOS开发UI篇—Modal简单介绍

    iOS开发UI篇—Modal简单介绍 一.简单介绍 除了push之外,还有另外一种控制器的切换方式,那就是Modal 任何控制器都能通过Modal的形式展⽰出来 Modal的默认效果:新控制器从屏幕的 ...

  7. iOS开发网络篇—简单介绍ASI框架的使用

    iOS开发网络篇—简单介绍ASI框架的使用 说明:本文主要介绍网络编程中常用框架ASI的简单使用. 一.ASI简单介绍 ASI:全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大 ...

  8. iOS开发数据库篇—SQLite简单介绍

    iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1 ...

  9. iOS开发UI篇—Kvc简单介绍

    ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...

随机推荐

  1. JavaScript学习 Ⅲ

    六. 面向对象 对象属于一种复合的数据类型,在对象中可以保存多个不同的数据类型的属性. 对象分类 内建对象 由ES标准种定义的对象.比如:Math String Number 宿主对象 由JS的运行环 ...

  2. vue 仿掘金评论列表

    先来个最终效果 代码: template代码: <template> <div class="main"> <div class="titl ...

  3. Unity- 小“东西”

    菜单栏遍历处理预制体工具 public class GameEditor : Editor { private static void ProcessPrefabs(Action<GameObj ...

  4. vscode切换虚拟环境报错无法加载文件 E:\Python_project\shop_env\Scripts\Activate.ps1,因为在此系统上禁止运行 脚本。

    在使用vscode切换python的虚拟环境时报错 解决方法如下: Windows+x打开面板,选择以管理员身份运行PowerShell,输入: set-executionpolicy remotes ...

  5. redis的集群化方案

    关于 目前有三种 (1)Twitter开发的twemproxy (2)豌豆荚开发的codis (3)redis官方的redis-cluster Twemproxy 架构简单 就是用proxy对后端re ...

  6. Burp Suite Target Module - 目标模块

    模块目的之一:获取网站分析 1.从Proxy - HTTP history界面选中需要加入Target Scope的Host 地址,右击,选中Add to Scope. 2.打开Target - Sc ...

  7. CDQ分治 & 整体分治

    Part 1:CDQ分治 CDQ分治讲解博客 可以把CDQ分治理解为类似与归并排序求逆序对个数的一种分治算法(至少我现在是这么想的).先处理完左右两边各自对答案的贡献,在处理跨越左右两边的对答案的贡献 ...

  8. python如何编写win程序

    python可以编写win程序.win程序的格式是exe,下面我们就来看一下使用python编写exe程序的方法. 编写好python程序后py2exe模块即可将其打包为exe程序. 实际操作过程: ...

  9. wpf中实现快捷键

    <Window.InputBindings> <KeyBinding Gesture="Ctrl+Alt+Q" Command="{Binding Yo ...

  10. springcloud之Eureka注册中心

    参考博客:https://www.cnblogs.com/ityouknow/p/6854805.html 背景: Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Ser ...