CAShapeLayer与UIBezierPath

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

1.背景知识:

  • UIBezierPath: UIBezierPath是在 UIKit中的一个类,继承于NSObject,可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个OC封装。使用此类可以定义常见的圆形、多边形等形状。我们使用直线、弧(arc)来创建复杂的曲线形状。每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。
  • CAShapeLayer: CAShapeLayer顾名思义,继承于CALayer。 每个CAShapeLayer对象都代表着将要被渲染到屏幕上的一个任意的形状(shape)。具体的形状由其path(类型为CGPathRef)属性指定。普通的CALayer是矩形,所以需要frame属性。CAShapeLayer初始化时也需要指定frame值,但 它本身没有形状,它的形状来源于其属性path 。CAShapeLayer有不同于CALayer的属性,它从CALayer继承而来的属性在绘制时是不起作用的。

2.开始绘制:

这里绘制三个图形,圆形,环形,不规则多边形。

关键代码如下:

#<span class="keyword">import</span> <span class="string">"ViewController.h"</span>
<span class="annotation">@interface</span> ViewController ()
{
<span class="indent">  </span><span class="comment">//三个CAShapeLayer</span>
<span class="indent">  </span>CAShapeLayer*circleLayer;
<span class="indent">  </span>CAShapeLayer*loopLayer;
<span class="indent">  </span>CAShapeLayer*polygonLayer;
}
<span class="annotation">@end</span>

<span class="annotation">@implementation</span> ViewController

- (<span class="keyword">void</span>)viewDidLoad
{
<span class="indent">  </span>[<span class="keyword">super</span> viewDidLoad];
<span class="indent">  </span><span class="comment">//对三个CAShapeLayer进行初始化</span>
<span class="indent">  </span>circleLayer=[CAShapeLayer <span class="keyword">new</span>];
<span class="indent">  </span>circleLayer.frame=CGRectMake(<span class="number">0</span>, <span class="number">0</span>, <span class="number">160</span>, <span class="number">200</span>);
<span class="indent">  </span><span class="comment">//fillColor和strokeColor的区别是 一个为填充色,一个为描边色</span>
<span class="indent">  </span>circleLayer.fillColor=[UIColor blueColor].CGColor;
<span class="indent">  </span>circleLayer.strokeColor=[UIColor redColor].CGColor;
<span class="indent">  </span>
<span class="indent">  </span>loopLayer=[CAShapeLayer <span class="keyword">new</span>];
<span class="indent">  </span>loopLayer.frame=CGRectMake(<span class="number">160</span>, <span class="number">0</span>, <span class="number">160</span>, <span class="number">200</span>);
<span class="indent">  </span>loopLayer.fillColor=nil;
<span class="indent">  </span>loopLayer.lineCap = kCALineCapRound;
<span class="indent">  </span>loopLayer.strokeColor=[UIColor cyanColor].CGColor;
<span class="indent">  </span>loopLayer.lineWidth=<span class="number">10</span>;
<span class="indent">  </span>
<span class="indent">  </span>polygonLayer=[CAShapeLayer <span class="keyword">new</span>];
<span class="indent">  </span>polygonLayer.frame=CGRectMake(<span class="number">0</span>, <span class="number">200</span>, <span class="number">320</span>, <span class="number">200</span>);
<span class="indent">  </span>polygonLayer.fillColor=nil;
<span class="indent">  </span>polygonLayer.strokeColor=[UIColor greenColor].CGColor
<span class="indent">  </span>;
<span class="indent">  </span><span class="comment">//添加到主视图的layer中</span>
<span class="indent">  </span>[self.view.layer addSublayer:circleLayer];
<span class="indent">  </span>[self.view.layer addSublayer:loopLayer];
<span class="indent">  </span>[self.view.layer addSublayer:polygonLayer];

<span class="indent">  </span>
}
<span class="comment">//环形按钮触发的事件</span>
- (IBAction)buttonAction:(id)sender {
<span class="indent">  </span>UIBezierPath*path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(<span class="number">80</span>, <span class="number">80</span>) radius:<span class="number">80</span> startAngle:<span class="number">0</span> endAngle:M_PI*<span class="number">2</span> clockwise:YES];
<span class="comment">//	[[UIColor redColor]setStroke];</span>
<span class="indent">  </span>loopLayer.path=path.CGPath;
}
<span class="comment">//圆</span>
- (IBAction)circle:(id)sender {
<span class="indent">  </span>UIBezierPath*path=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(<span class="number">0</span>, <span class="number">0</span>, <span class="number">160</span>, <span class="number">160</span>) cornerRadius:<span class="number">80</span>];
<span class="indent">  </span>circleLayer.path=path.CGPath;
}
<span class="comment">//多边形</span>
- (IBAction)polygon:(id)sender {
<span class="indent">  </span>UIBezierPath*path=[UIBezierPath <span class="keyword">new</span>];
<span class="indent">  </span>[path moveToPoint:CGPointMake(<span class="number">0</span>, <span class="number">0</span>)];
<span class="indent">  </span>[path addLineToPoint:CGPointMake(<span class="number">0</span>, <span class="number">50</span>)];
<span class="indent">  </span>[path addLineToPoint:CGPointMake(<span class="number">50</span>, <span class="number">50</span>)];
<span class="indent">  </span>[path addLineToPoint:CGPointMake(<span class="number">150</span>, <span class="number">50</span>)];
<span class="indent">  </span>[path addLineToPoint:CGPointMake(<span class="number">200</span>, <span class="number">100</span>)];
<span class="indent">  </span>[path closePath];<span class="comment">//将起点与结束点相连接</span>
<span class="indent">  </span>polygonLayer.path=path.CGPath;

}

<span class="annotation">@end</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍如何使用UIBezierPath画聊天气泡。 首先,我们需要创建一个UIView作为聊天气泡的容器,并设置它的背景色和圆角。 然后,我们可以创建一个UIBezierPath对象,使用move(to:)方法将笔触移动到气泡的左下角,然后使用addLine(to:)方法画出气泡的左侧、顶部和右侧的线条。接着,我们可以使用addArc(withCenter:radius:startAngle:endAngle:clockwise:)方法在气泡的右下角添加一个圆角。 最后,我们可以使用CAShapeLayerUIBezierPath对象渲染到UIView上,实现聊天气泡的绘制。 以下是示例代码: ```swift let bubbleView = UIView() bubbleView.backgroundColor = UIColor(red: 0.93, green: 0.93, blue: 0.93, alpha: 1) bubbleView.layer.cornerRadius = 10 bubbleView.clipsToBounds = true let bubblePath = UIBezierPath() bubblePath.move(to: CGPoint(x: 10, y: bubbleView.bounds.height - 10)) bubblePath.addLine(to: CGPoint(x: 10, y: 10)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 30, y: 10)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 20, y: 0)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 10, y: 10)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 10, y: bubbleView.bounds.height - 10)) bubblePath.addArc(withCenter: CGPoint(x: bubbleView.bounds.width - 20, y: bubbleView.bounds.height - 10), radius: 10, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: true) bubblePath.addArc(withCenter: CGPoint(x: bubbleView.bounds.width - 20, y: bubbleView.bounds.height - 20), radius: 10, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: true) bubblePath.addArc(withCenter: CGPoint(x: bubbleView.bounds.width - 30, y: bubbleView.bounds.height - 10), radius: 10, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: true) bubblePath.close() let shapeLayer = CAShapeLayer() shapeLayer.path = bubblePath.cgPath bubbleView.layer.mask = shapeLayer ``` 以上代码将创建一个灰色的聊天气泡,带有圆角和三角形尾巴。您可以根据需要调整气泡的颜色、形状和大小。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值