博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CAShapeLayer
阅读量:4584 次
发布时间:2019-06-09

本文共 3267 字,大约阅读时间需要 10 分钟。

一、CAShapeLayer的作用和优点

  可以通过矢量图形而不是bitmap来绘制的图层子类。你指定诸如颜色和线宽等属性,用CGPath来定义想要绘制的图形

  你也可以用Core Graphics直接向原始的CALyer的内容中绘制一个路径,相比直下,使用CAShapeLayer有以下一些优点:

  • 渲染快速。CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多。
  • 高效使用内存。一个CAShapeLayer不需要像普通CALayer一样创建一个寄宿图形,所以无论有多大,都不会占用太多的内存。
  • 不会被图层边界剪裁掉。一个CAShapeLayer可以在边界之外绘制。你的图层路径不会像在使用Core Graphics的普通CALayer一样被剪裁掉(如我们在第二章所见)。
  • 不会出现像素化。当你给CAShapeLayer做3D变换时,它不像一个有寄宿图的普通图层一样变得像素化。

二、实际例子

 

  

 

三、上面的例子是二维码扫描界面,界面中需要一个中间透明的提示框,以及四个直角的标记,可以通过CASharpLayer来实现

  中间的透明可以通过UIview的maskLayer来进行掩码实现

  四个角的黄线通过CASharpLayer来实现

  

- (void)addOverlay{
  //innerrect就是中空的frame CGRect innerRect = [self getInnerRect]; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) cornerRadius:0]; UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:innerRect cornerRadius:0]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; CAShapeLayer *fillLayer = [CAShapeLayer layer]; fillLayer.path = path.CGPath; fillLayer.fillRule = kCAFillRuleEvenOdd; fillLayer.fillColor = [UIColor blackColor].CGColor; fillLayer.opacity = 0.5; [self.layer addSublayer:fillLayer]; _maskLayer = fillLayer; _overlay = [[CAShapeLayer alloc] init]; _overlay.backgroundColor = [UIColor clearColor].CGColor; _overlay.fillColor = [UIColor clearColor].CGColor; _overlay.strokeColor = [UIColor yellowColor].CGColor; _overlay.lineWidth = 3; innerRect = CGRectInset(innerRect, -_overlay.lineWidth/2, -_overlay.lineWidth/2); CGFloat lineLength = 20; UIBezierPath *cornerPath = [UIBezierPath bezierPath]; //left-top corner CGPoint leftTop = CGPointMake(innerRect.origin.x, innerRect.origin.y); [cornerPath moveToPoint:CGPointMake(leftTop.x, leftTop.y + lineLength)]; [cornerPath addLineToPoint:leftTop]; [cornerPath addLineToPoint:CGPointMake(leftTop.x + lineLength, leftTop.y)]; //right-top corner CGPoint rightTop = CGPointMake(innerRect.origin.x + innerRect.size.width, innerRect.origin.y); [cornerPath moveToPoint:CGPointMake(rightTop.x - lineLength, rightTop.y)]; [cornerPath addLineToPoint:rightTop]; [cornerPath addLineToPoint:CGPointMake(rightTop.x, rightTop.y + lineLength)]; //right-bottom corner CGPoint rightBottom = CGPointMake(innerRect.origin.x + innerRect.size.width, innerRect.origin.y + innerRect.size.height); [cornerPath moveToPoint:CGPointMake(rightBottom.x, rightBottom.y - lineLength)]; [cornerPath addLineToPoint:rightBottom]; [cornerPath addLineToPoint:CGPointMake(rightBottom.x - lineLength, rightBottom.y)]; //left-bottom corner CGPoint leftBottom = CGPointMake(innerRect.origin.x, innerRect.origin.y + innerRect.size.height); [cornerPath moveToPoint:CGPointMake(leftBottom.x + lineLength, leftBottom.y)]; [cornerPath addLineToPoint:leftBottom]; [cornerPath addLineToPoint:CGPointMake(leftBottom.x, leftBottom.y - lineLength)]; _overlay.path = cornerPath.CGPath; [self.layer addSublayer:_overlay];}

  

 

转载于:https://www.cnblogs.com/doudouyoutang/p/8137670.html

你可能感兴趣的文章
Flask框架web开发
查看>>
ios github网址
查看>>
Django 数据库操作之数据库连接
查看>>
写log
查看>>
Python基础 ----- 流程控制
查看>>
选择语言之switch case
查看>>
实现斐波那契神兔
查看>>
【linux就该这么学】-08
查看>>
JavaScript基础知识汇总
查看>>
PyQt4网格布局
查看>>
PHP学习笔记 - 进阶篇(3)
查看>>
极角排序那些事
查看>>
Ganglia+nagios 监控hadoop资源与报警
查看>>
博客园主题样式修改教程
查看>>
TextView实现多个TextView对象的走马灯效果
查看>>
感悟成功
查看>>
学员管理示例:Ajax删除学生
查看>>
线程组和未处理的异常
查看>>
Oracle管理监控之为11g asm磁盘组添加磁盘
查看>>
javasrcipt中的for in 循环
查看>>