gpt4 book ai didi

ios - 了解 UIBezierPath 的 moveToPoint 和 addLineToPoint 方法吗?

转载 作者:行者123 更新时间:2023-11-29 10:26:11 25 4
gpt4 key购买 nike

我的主要目的是创建一条线,其原点位于其他外观为圆形的 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.

这意味着:拿起“笔”,然后将它移动到指定点而不是绘制。该点将作为下一次绘图操作的起点。

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.

这将“绘制”一条线,从当前点到指定点。线的终点将成为新的起点。


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

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

层的位置是其左上角在父空间中的坐标。所以如果你像那样指定起点,你实际上是从它“外面”的某个地方开始的。此外,这些点在“此”图层坐标中指定,因此中心点应该是您将应用贝塞尔路径的图层的 (width/2, height/2)

关于ios - 了解 UIBezierPath 的 moveToPoint 和 addLineToPoint 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com