ios CAShapeLayer和UIBezierPath


 CAShapeLayer继承CALayer,因此,可使用CALayer的所有属性,但是,CAShapelayer需要和贝塞尔曲线配合使用才有意义。


现在简单的画一个圆形

<span style="font-size:14px;">- (void)test
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    // 指定frame,只是为了设置宽度和高度
    layer.frame = CGRectMake(0, 0, 200, 200);
    // 设置居中显示
    layer.position = self.view.center;
    // 设置填充颜色
    layer.fillColor = [UIColor redColor].CGColor;
    // 设置线宽
    layer.lineWidth = 2.3;
    // 设置线的颜色
    layer.strokeColor = [UIColor redColor].CGColor;
    
    // 使用UIBezierPath创建路径,并在layer上进行渲染
    CGRect frame = CGRectMake(0, 0, 200, 200);
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:frame];
    // 设置CAShapeLayer与UIBezierPath关联
    layer.path = circlePath.CGPath;
    // 将CAShaperLayer放到某个层上显示
    [self.view.layer addSublayer:layer];

}
</span>

上面的代码简单的绘制了一个圆形

现在简单的介绍一下UIBezierPath,UIBezierPath可以简单的创建矢量的路径,此类Core Graphics框架关于路径的封装,使用此类可以定义简单的形状,入椭圆,矩形等

我们一般使用UIBezierPath画图的步骤(一般在drawRect方法中绘图)
1> 创建UIBezierPath对象,

2> 使用moveToPoint:设置初始线段的起点

3> 添加线或者曲线去定义或者多个子路径

4> 改变UIBezierPath对象跟绘图相关的属性


<span style="font-size:14px;">- (void)drawRect:(CGRect)rect {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, 20)];
    [path addLineToPoint:CGPointMake(self.frame.size.width - 40, 20)];
    [path addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20)];
    
    // 最后的闭合线是可以通过调用closePath方法来自动生成的,也可以调用-addLineToPoint:方法来添加
    //  [path addLineToPoint:CGPointMake(20, 20)];
    
    [path closePath];
    // 设置线宽
    path.lineWidth = 1.5;
    // 设置填充颜色
    UIColor *fillColor = [UIColor yellowColor];
    [fillColor set];
    [path fill];
    
    // 设置画笔颜色
    UIColor *strokeColor = [UIColor blueColor];
    [strokeColor set];
    
    // 根据我们设置的各个点连线
    [path stroke];
}
</span>


再次声明:一般CAShapeLayer和UIBezierPath配合使用,附上本人的一个关于仿AppStore里的下载样式的按钮

下载地址:https://github.com/likelin/LKProgressButton.git


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值