iOS绘制弧度有两个函数,一个设置中心点另外一个是设置点到点,
<pre><code>` public func addArc(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)
public func addArc(tangent1End: CGPoint, tangent2End: CGPoint, radius: CGFloat)`</code></pre>
我们设置起点弧度为0,终点弧度为1.5 * PI(等于270角度),最后的clockwise的设置为true.
<pre><code>` UIGraphicsBeginImageContext(self.view.bounds.size);
let context:CGContext = UIGraphicsGetCurrentContext()!
let path:CGMutablePath = CGMutablePath()
context.addArc(center:CGPoint(x: 150, y: 150), radius: 50, startAngle: 0, endAngle: 1.5*CGFloat(M_PI), clockwise: true)
context.move(to: CGPoint(x: 150, y: 100))
context.addLine(to: CGPoint(x: 150, y: 150))
context.addLine(to: CGPoint(x: 200, y: 150))
context.addPath(path)
UIColor.gray.setFill()
UIColor.red.setStroke()
context.drawPath(using: .fillStroke)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let imgView:UIImageView = UIImageView.init(image: image)
self.view.addSubview(imgView)`</code></pre>
效果图:
FlyElephant.png
顺时针设为false:
<pre><code>context.addArc(center:CGPoint(x: 150, y: 150), radius: 50, startAngle: 0, endAngle: 1.5*CGFloat(M_PI), clockwise: false)
</code></pre>
效果图:
圆角矩形
通过线和线之间的相切,我们也可以获取圆角,代码如下:
<pre><code>` UIGraphicsBeginImageContext(self.view.bounds.size);
let context:CGContext = UIGraphicsGetCurrentContext()!
let path:CGMutablePath = CGMutablePath()
context.move(to: CGPoint(x: 100, y: 300))
context.addArc(tangent1End: CGPoint(x: 200, y: 300), tangent2End: CGPoint(x: 200, y: 400), radius: 5.0)
context.addArc(tangent1End: CGPoint(x: 200, y: 400), tangent2End: CGPoint(x: 100, y: 400), radius: 5.0)
context.addArc(tangent1End: CGPoint(x: 100, y: 400), tangent2End: CGPoint(x: 100, y: 300), radius: 5.0)
context.addArc(tangent1End: CGPoint(x: 100, y: 300), tangent2End: CGPoint(x: 200, y: 300), radius: 5.0)
context.addPath(path)
UIColor.gray.setFill()
UIColor.red.setStroke()
context.drawPath(using: .fillStroke)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let imgView:UIImageView = UIImageView.init(image: image)
self.view.addSubview(imgView)`</code></pre>
效果图如下:
FlyElephant.png
网友评论