CAShapeLayer是基于贝塞尔曲线而存在的, 如果没有贝塞尔曲线提供路径来画出图形, CAShapeLayer就没有存在的意义, CAShapeLayer可以使得不用在drawRect:方法中实现画图.

另外, CAShapeLayer是属于CoreAnimation框架的, 是基于GPU的来进行渲染的, 不比使用CoreGraphic框架, 是基于CPU来渲染的, 所以CAShapeLayer效率相对比较高一些

下面我简单的使用CAShapeLayer和贝塞尔曲线画了矩形和椭圆形, 代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad]; [self createOval];
} // 矩形(正方形)
- (void)createRect
{
// 创建矩形贝塞尔曲线路径
UIBezierPath *rect = [UIBezierPath bezierPathWithRect:CGRectMake(, , , )]; // 创建CAShapeLayer
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
// 设置尺寸
shapeLayer.frame = CGRectMake(, , , );
// 设置位置
shapeLayer.position = self.view.center;
// 填充颜色
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
// 路径颜色
shapeLayer.strokeColor = [UIColor blackColor].CGColor; // 关联
shapeLayer.path = rect.CGPath; // 显示
[self.view.layer addSublayer:shapeLayer];
} // 椭圆(圆)
- (void)createOval
{
// 创建椭圆形贝塞尔曲线路径
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; // 创建CAShapeLayer
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
// 设置尺寸,
shapeLayer.frame = CGRectMake(, , , );
// 设置位置(设置的是shapeLayer的中心点位置)
shapeLayer.position = self.view.center;
// 设置背景颜色
shapeLayer.backgroundColor = [UIColor greenColor].CGColor;
// 设置填充颜色(注意, 这里不是设置背景颜色)
shapeLayer.fillColor = [UIColor redColor].CGColor;
// 设置边框颜色(路径颜色)
shapeLayer.strokeColor = [UIColor blueColor].CGColor; // 关联ShapeLayer和贝塞尔曲线
shapeLayer.path = oval.CGPath; // 显示
[self.view.layer addSublayer:shapeLayer]; }

UIBezierPath和CAShapeLayer的关系的更多相关文章

  1. 放肆的使用UIBezierPath和CAShapeLayer画各种图形

    CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形,当然,你也可以使用其他方式来画,随你. 杂谈 在 CAShapeLayer 中,也可以像 CAL ...

  2. 使用UIBezierPath和CAShapeLayer画各种图形

    转载自:http://www.cocoachina.com/ios/20160214/15251.html CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画 ...

  3. UIBezierPath 和 CAShapeLayer 绘画图纸

    五角大楼画一个小圆圈戴: - (void)drawPentagon{ //(1)UIBezierPath对象 UIBezierPath *aPath = [UIBezierPath bezierPat ...

  4. 贝塞尔曲线与CAShapeLayer的关系以及Stroke动画

    1.贝塞尔曲线与CAShapeLayer的关系    1.1CAShapeLayer须要一个形状才干生效,贝塞尔曲线能够创建基于矢量的路径.进而能够给CAShapeLayer提供路径,路径会闭环.   ...

  5. iOS 使用UIBezierPath和CAShapeLayer画各种图形

    CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形,当然,你也可以使用其他方式来画,随你. 杂谈 在 CAShapeLayer 中,也可以像 CAL ...

  6. UIBezierPath与CAShapeLayer结合画扇形

    /*让半径等于期望半径的一半 lineWidth等于期望半径 就可以画圆*/ 可以看出layer的走势是从圆边的中间一半在圆外 一半在圆内 因此让半径等于期望半径的一半 lineWidth等于期望半径 ...

  7. UIBezierPath和CAShapeLayer配合肆意画图

    一.CAShapeLayer CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形   使用CAShapeLayer 绘制一个矩形 let layer ...

  8. iOS关于CAShapeLayer与UIBezierPath的知识内容

    使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...

  9. 使用CAShapeLayer与UIBezierPath画出想要的图形

    使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 步骤: 1.新建UIBezierPath对象bezierPath 2.新建CAS ...

随机推荐

  1. elasticsearch常用的插件

    deb 安装/usr/share/elasticsearch/   https://github.com/hangxin1940/elasticsearch-cn-out-of-box 包可以参考   ...

  2. android中的margin和padding

    Android的Margin和Padding跟Html的是一样的.如下图所示:黄色部分为Padding,灰色部分为Margin. 通俗的理解: Padding 为内边框,指该控件内部内容,如文本/图片 ...

  3. Microsoft Detours 2.1简介

    http://blog.163.com/qcb_163/blog/static/9545466420117851038971/ Microsoft Detours 2.1简介 2011-08-0817 ...

  4. struts2的记住账号密码的登录设计

    一个简单的基于struts2的登录功能,实现的额外功能有记住账号密码,登录错误提示.这里写上我在设计时的思路流程,希望大家能给点建设性的意见,帮助我改善设计. 登录功能的制作,首先将jsp界面搭建出来 ...

  5. 【HDOJ】1547 Bubble Shooter

    两次BFS,一次扫描关联点.一次扫描可能掉落的情况(即再次扫描所有非爆炸的联通点).余下未被扫描的点均爆炸. #include <cstdio> #include <cstring& ...

  6. Perl数据库DBI接口简介【转载】

    本文转载自:http://blog.csdn.net/like_zhz/article/details/5441946 ######################################## ...

  7. Foundation: Binary Search

    /* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...

  8. linux下安装mysql-community后起不来

    wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpmrpm -ivh http://repo.mysql.com/my ...

  9. 修改CKFinder上传路径

    一.CKFinder的若干问题 1.单独使用 ckfinder从原fckeditor分离出来以后可以单独使用,通常我习惯于在工具栏中添加ckfinder.dll,这样以后要使用ckfinder直接从工 ...

  10. php几个不起眼儿的小技巧

    说是不起眼儿的小技巧,其实应该说是不常用的常规应用吧.很多事情就是这样,知道是一马事儿,会用是一马事儿,精习又是另外一马事儿.而成为高手更是需要扎实的基本功. str_repeat 重复输出字符串就靠 ...