了解 UIBezierPath 的 moveToPoint 和 addLineToPoint 方法吗?

Understand moveToPoint and addLineToPoint methods of UIBezierPath?

我的主要目的是创建一条线,其原点位于其他外观为圆形的 UIBezierPath 的中心。

let startAngle = CGFloat(-M_PI_2)
    let endAngle = CGFloat(M_PI + M_PI_2)

    println("Start Angle \(startAngle) , End Angle \(endAngle)")

    let centerPoint = CGPointMake(CGRectGetWidth(frame)/2 , CGRectGetHeight(frame)/2)

    self.staticLayer.path = UIBezierPath(arcCenter:centerPoint, radius: CGRectGetWidth(frame)/2 - 30.0, startAngle:startAngle, endAngle:endAngle, clockwise: true).CGPath

    self.staticLayer.fillColor = UIColor.clearColor().CGColor
    self.staticLayer.shouldRasterize = false

    self.staticLayer.strokeColor = UIColor.redColor().CGColor

    self.staticLayer.lineWidth = 08
    self.staticLayer.borderColor = UIColor.greenColor().CGColor
    self.staticLayer.borderWidth = 5.0

    layer.addSublayer(self.staticLayer)

在这个静态层之后,我想添加一行(与时钟中的秒相同)

我做到了

  var bezierPath: UIBezierPath = UIBezierPath()
    bezierPath.moveToPoint(CGPoint(x: self.staticLayer.position.x/2, y: self.staticLayer.position.y/2))
    bezierPath.addLineToPoint(CGPoint(x: 50, y: 10))


    self.lineLayer.path = bezierPath.CGPath

    self.lineLayer.fillColor = UIColor.clearColor().CGColor
    self.lineLayer.borderColor = UIColor.clearColor().CGColor
    self.lineLayer.strokeColor = UIColor.redColor().CGColor

    self.lineLayer.shouldRasterize = false
    self.lineLayer.lineWidth = 3.0
    self.lineLayer.lineJoin = kCALineJoinBevel

    layer.addSublayer(self.lineLayer)

现在,我无法理解 moveToPointaddLineToPoint 的确切传递点。

我想从另一层的中心开始,它是静止的圆形。

谁能解释一下,我如何控制线的起点和长度?

谢谢

来自docs :

moveToPoint:

This method implicitly ends the current subpath (if any) and sets the current point to the value in the point parameter. When ending the previous subpath, this method does not actually close the subpath. Therefore, the first and last points of the previous subpath are not connected to each other.

For many path operations, you must call this method before issuing any commands that cause a line or curve segment to be drawn.

意思是:把"pen"拿起来,不画画的移动到指定的点。该点将作为下一次绘图操作的起点。

addLineToPoint:

This method creates a straight line segment starting at the current point and ending at the point specified by the point parameter. After adding the line segment, this method updates the current point to the value in point.

You must set the path’s current point (using the moveToPoint: method or through the previous creation of a line or curve segment) before you call this method. If the path is empty, this method does nothing.

这将"draw"一条线,从当前点到指定点。行尾将成为新的起点。


看来你的定位问题是在这一行:

bezierPath.moveToPoint(CGPoint(x: self.staticLayer.position.x/2, y: self.staticLayer.position.y/2))

层的位置是它在父层中左上角的坐标space。因此,如果您指定这样的起点,那么您实际上是从 "outside" 的某处开始。此外,这些点在 "this" 图层坐标中指定,因此中心点应该是您将应用贝塞尔路径的图层的 (width / 2, height / 2)