Swfit中CGContextAddArc的使用总结

Home / iOS MrLee 2016-3-10 3134

本博客偶尔借鉴农夫山泉的精神,我们不生产水,我们是大自然的搬运工!做为程序员,我们不可能所有的API都去试一遍,因为这是不可能的。如果需要的话,你才会去学习,研究下,对吧!原创固然好,毕竟精力有限!下面是一篇我看到的文章,总结的还不错。贴出来!
先看下申明
/* Add an arc of a circle to the context's path, possibly preceded by a
   straight line segment. `(x, y)' is the center of the arc; `radius' is its
   radius; `startAngle' is the angle to the first endpoint of the arc;
   `endAngle' is the angle to the second endpoint of the arc; and
   `clockwise' is 1 if the arc is to be drawn clockwise, 0 otherwise.
   `startAngle' and `endAngle' are measured in radians. */
@available(iOS 2.0, *)
public func CGContextAddArc(c: CGContext?, _ x: CGFloat, _ y: CGFloat, _ radius: CGFloat, _ startAngle: CGFloat, _ endAngle: CGFloat, _ clockwise: Int32)

 
CGContextRef不解释了,x,y为圆点坐标,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。
CGContextAddArc(context, 160, 200, 100, 0, 45*(M_PI/180), 0);
所以对上面这对代码的解释是这样的:
1)startAngle为0,绿色箭头的地方。
2)endAngle为45,黄色箭头的地方。
3)clockwise为0,按照红色箭头往下绘制图形。
4)所以效果就是红色的扇形。
补充:如果clockwise为1,则是蓝色部分区域。

0D826787916C7D216CCD2D012DE49C2E_B500_900_286_260


import UIKit
class CircleView: UIView {
    
    var offset:Float = 0.0
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        let context:CGContextRef =  UIGraphicsGetCurrentContext()!;//获取画笔上下文
        CGContextSetAllowsAntialiasing(context, true) //抗锯齿设置
        // Set the circle outerline-width
        CGContextSetLineWidth(context, 5.0)
        // Set the circle outerline-colour
        UIColor.grayColor().set()
        // Create Circle
        CGContextAddArc(context, (frame.size.width)/2, frame.size.height/2, (frame.size.height - 10)/2, converAngel(offset), converAngel(45+offset), 0)
        // Draw
        CGContextStrokePath(context)
    }
    
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        offset = offset+2
        setNeedsDisplay()
    }
    
    
    private func converAngel(angel:Float)->CGFloat{
        return CGFloat(angel)*CGFloat(M_PI/180)
    }
}

申明:本代码并不是上图的效果。

本文链接:https://www.it72.com/7946.htm

推荐阅读
最新回复 (0)
返回