码迷,mamicode.com
首页 > 其他好文 > 详细

UIBezierPath和CAShapeLayer的关系

时间:2016-01-21 15:36:24      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

CAShapeLayer是基于贝塞尔曲线而存在的, 如果没有贝塞尔曲线提供路径来画出图形, CAShapeLayer就没有存在的意义

下面我画了矩形和椭圆形, 代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createOval];
}

// 矩形(正方形)
- (void)createRect
{
    // 创建矩形贝塞尔曲线路径
    UIBezierPath *rect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 30)];
    
    // 创建CAShapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    // 设置尺寸
    shapeLayer.frame         = CGRectMake(0, 0, 100, 200);
    // 设置位置
    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(0, 0, 200, 200)];
    
    // 创建CAShapeLayer
    CAShapeLayer *shapeLayer    = [CAShapeLayer layer];
    // 设置尺寸,
    shapeLayer.frame            = CGRectMake(0, 0, 200, 200);
    // 设置位置(设置的是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的关系

标签:

原文地址:http://www.cnblogs.com/Rinpe/p/5148417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!