了解 UIBezierPath 的 moveToPoint 和 addLineToPoint 方法? [英] Understand moveToPoint and addLineToPoint methods of UIBezierPath?

查看:118
本文介绍了了解 UIBezierPath 的 moveToPoint 和 addLineToPoint 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要目的是创建一条线,其原点位于其他 UIBezierPath 的中心,看起来是圆形的.

让 startAngle = CGFloat(-M_PI_2)让 endAngle = CGFloat(M_PI + M_PI_2)println("起始角 \(startAngle) , 结束角 \(endAngle)")让 centerPoint = CGPointMake(CGRectGetWidth(frame)/2 , CGRectGetHeight(frame)/2)self.staticLayer.path = UIBezierPath(arcCenter:centerPoint, radius: CGRectGetWidth(frame)/2 - 30.0, startAngle:startAngle, endAngle:endAngle, 顺时针: true).CGPathself.staticLayer.fillColor = UIColor.clearColor().CGColorself.staticLayer.shouldRasterize = falseself.staticLayer.strokeColor = UIColor.redColor().CGColorself.staticLayer.lineWidth = 08self.staticLayer.borderColor = UIColor.greenColor().CGColorself.staticLayer.borderWidth = 5.0layer.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.CGPathself.lineLayer.fillColor = UIColor.clearColor().CGColorself.lineLayer.borderColor = UIColor.clearColor().CGColorself.lineLayer.strokeColor = UIColor.redColor().CGColorself.lineLayer.shouldRasterize = falseself.lineLayer.lineWidth = 3.0self.lineLayer.lineJoin = kCALineJoinBevellayer.addSublayer(self.lineLayer)

现在,我无法理解moveToPointaddLineToPoint 的确切通过点.

我想从其他层的中心开始,这是静态的圆形.

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

谢谢

解决方案

来自 文档 :

moveToPoint:

<块引用>

此方法隐式结束当前子路径(如果有)并设置当前点到点参数中的值.结束时之前的子路径,这个方法实际上并没有关闭子路径.因此,前一个子路径的第一个和最后一个点不是彼此相连.

对于许多路径操作,您必须在发出任何导致绘制直线或曲线段的命令.

这意味着:拿起笔",将它移动到指定的点而不画.此点将作为下一次绘图操作的起点.

addLineToPoint:

<块引用>

此方法创建一条直线段,从当前位置开始point 并在 point 参数指定的点处结束.后添加线段,此方法将当前点更新为点值.

您必须设置路径的当前点(使用 moveToPoint: 方法或通过先前创建的直线或曲线段)之前你调用这个方法.如果路径为空,则此方法不执行任何操作.

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

<小时>

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

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

图层的位置是它在父空间中左上角的坐标.所以如果你这样指定起点,你实际上是从它的外部"开始.此外,这些点在这个"图层坐标中指定,因此中心点应该是您将应用贝塞尔曲线路径的图层的 (width/2, height/2).

My main purpose is of creating a line which has origin point at center of other UIBezierPath which is ROUND in look.

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)

After this static layer, I want to add a line (same as seconds in clock)

I made it

  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)

Now, I am unable to understand exact passing points of moveToPoint and addLineToPoint.

I want to start this from center of other layer, which is static and round in shape.

Can any one explain me, how can I control starting point and length of line?

Thanks

解决方案

From the 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.

This means : take the "pen" up, and move it without drawing to a specified point. This point will be a starting point for next drawing operation.

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.

This will "draw" a line, from current point to a specified point. The end of line will become the new starting point.


It seems that your positioning problem is in this line :

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

The position of a layer are the coordinates of its top-left corner in parent space. So if you specify the starting point like that, you are actually starting somewhere "outside" of it. Also the points are specfied in "this" layers coordinates, so the center point should be (width / 2, height / 2) of the layer to which you will be applying your bezier path.

这篇关于了解 UIBezierPath 的 moveToPoint 和 addLineToPoint 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆