CALayer有一个属性叫做mask

这个属性本身就是个CALayer类型,有和其他图层一样的绘制和布局属性。

它类似于一个子图层,相对于父图层(即拥有该属性的图层)布局,但是它却不是一个普通的子图层。

不同于那些绘制在父图层中的子图层,mask图层定义了父图层的部分可见区域。

mask图层的Color属性是无关紧要的,真正重要的是图层的轮廓。mask属性就像是一个饼干切割机,mask图层实心的部分会被保留下来,其他的则会被抛弃

如果mask图层比父图层要小,只有在mask图层里面的内容才是它关心的,除此以外的一切都会被隐藏起来。

如下图

我们自己试试看

比如有两个Layer, 一个是红色的正方形,

一个小一点的绿色圆

如果把小一点的绿色圆当做红色正方形的maskLayer, 则最终只会在显示绿色圆范围内显示出原本红色正方形的内容

我们上代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //创建一个蓝色的Layer
CALayer *foregroundLayer = [CALayer layer];
foregroundLayer.bounds = CGRectMake(, , , );
foregroundLayer.backgroundColor = [UIColor redColor].CGColor; //创建一个路径
UIBezierPath *apath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; //创建maskLayer
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = apath.CGPath;
maskLayer.fillColor = [UIColor greenColor].CGColor;
maskLayer.fillRule = kCAFillRuleEvenOdd; //设置位置
foregroundLayer.position = self.view.center;
//设置mask
foregroundLayer.mask = maskLayer; [self.view.layer addSublayer:foregroundLayer]; } @end

我们再可以给遮罩层添加动画, 实现更加绚丽的效果

比如中间中间小圆逐渐变大

#import "ViewController.h"

static CGFloat num;

@interface ViewController ()

@property (nonatomic, strong) CAShapeLayer *circle;
@property (nonatomic, strong) CADisplayLink *link; @end @implementation ViewController @synthesize circle; - (void)viewDidLoad { [super viewDidLoad]; //创建一个CAShape
CALayer *bgLayer = [CALayer layer]; //设置大小颜色和位置
bgLayer.bounds = CGRectMake(, , , );
bgLayer.backgroundColor = [UIColor redColor].CGColor;
bgLayer.position = self.view.center; //创建一个CAShapeLayer作为MaskLayer
circle = [CAShapeLayer layer]; //设置路径
circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(, )
radius:
startAngle:
endAngle: * M_PI
clockwise:YES].CGPath;
circle.lineWidth = ;
circle.fillColor = [UIColor greenColor].CGColor;
circle.fillRule = kCAFillRuleEvenOdd; //设置maskLayer
bgLayer.mask = circle; [self.view.layer addSublayer:bgLayer]; //添加计时器
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(action)];
[self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
} - (void)action { num ++; circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(, )
radius: + num
startAngle:
endAngle: * M_PI
clockwise:YES].CGPath; if (num > ) {
[self.link invalidate];
}
}

CALayer之mask属性-遮罩的更多相关文章

  1. CALayer的mask属性

    可以对图层按path进行指定裁剪 //#import "ViewController.h" // //@interface ViewController () // //@end ...

  2. 关于使用 CALayer 中 mask 的一些技巧

    CALayer 拥有 mask 属性,Apple 的官方解释如下: An optional layer whose alpha channel is used to mask the layer’s ...

  3. 利用layer的mask属性实现逐渐揭示的动画效果

    github上又看到个不错的动画(https://github.com/rounak/RJImageLoader),如图: 所以就想来自己实现以下 不试不知道,这个动画还真不是看上去那么简单,我自己想 ...

  4. iOS—Mask属性的使用

    Mask属性介绍 Mask平时用的最多的是masksToBounds 吧. 其实除此以外Mask使用场景很多,看完之后你会发现好真是好用的不要不要的... 先来了解下Mask属性到底是什么? Mask ...

  5. Unity 摄像机Clear Flags和Culling Mask属性用途详解

    原文地址:http://blog.csdn.net/tanmengwen/article/details/8798231 1.简述两个属性 1.1 Clear Flags 清除标记 每个相机在渲染时会 ...

  6. iOS开发UI篇—CAlayer层的属性

    iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...

  7. CAlayer层的属性

    iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...

  8. iOS开发——UI篇&文字渐变效果:图层中的mask属性

    文字渐变效果:图层中的mask属性 本次文章,主要讲述的是图层中的mask属性,利用它,可以做出文字渐变效果! 一.文字渐变效果: 二.文字渐变实现思路: 1.创建一个颜色渐变层,渐变图层跟文字控件一 ...

  9. iOS开发UI 篇—CAlayer层的属性

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

随机推荐

  1. SQL点滴16—SQL分页语句总结

    原文:SQL点滴16-SQL分页语句总结 今天对分页语句做一个简单的总结,他们大同小异的,只要理解其中一个其他的就很好理解了. 使用top选项 *from Orders orderid from Or ...

  2. 编译安装gimp插件之Mathmap(流水记录)

    本文为在Fedora 20下编译安装Mathmap1.3.5的编译过程,如果你仅仅需要快速的安装Mathmap,那么请拉至文末的"快速安装" 其实,过程还是很有趣的,充满Error ...

  3. JavaScript中对数组的操作

    原文:JavaScript中对数组的操作 一:数组的使用 1.定义:JavaScript中对数组的定义有两种形式.如: .var arr = [12,3,5,8]; .var arr = new Ar ...

  4. ASP.NET MVC 5项目

    图文详解远程部署ASP.NET MVC 5项目   话外篇: 由于感觉自己的机器比较慢,配置不好,所以最近想把之前的项目部署到实验室的服务器上,但是由于常不在实验室,所以在想能不能远程部署.因此今天专 ...

  5. Map和List

    Map和List 当把Map中的key-value对当成单独的集合元素来对待时,Map和Set就统一起来了. Map集合是一个关联数组,它包含两组值:一组是所有key组成的集合,因为Map集合的key ...

  6. 使用typeof方法反射属性和方法

    Type type = typeof(System.Int32);//获得int类型的Type对象 foreach (MethodInfo method in type.GetMethods())// ...

  7. 【学习笔记】锋利的jQuery(三)事件和动画

    一.jQuery事件 1,加载事件 $(document).ready(function(){...}) //等同于$(function(){..}) $(window).load(function( ...

  8. UIWebView的探索

    UIWebView 说到iOS的UIWebView,应该会很快回忆起常用委托方法,异步loadRequest.stopLoading.reload方法等. 在此我总结一些容易忽略的属性和方法: 1.  ...

  9. Jumony Core 3,真正的HTML引擎

    Jumony Core 3,真正的HTML引擎,正式版发布 2013-11-28 17:22 by Ivony..., 778 阅读, 18 评论, 收藏, 编辑 Jumony是一个开源项目,已经有三 ...

  10. JS OffsetParent属性

       offsetParent 属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),并且是已进行过CSS定位的容器元素. 如果这个容器元素未进行CS ...