如何仅更改现有 UIBezierPath 的起点?

问题描述 投票:0回答:1

如何仅更改现有 UIBezierPath 的起点? ..并让所有其他点保持不变?

我确实看过很多关于如何做到这一点的帖子..老实说,我只是不明白其中很多

我现有的路径有 one

.move(to:)
many
.addLine(to:)

我的目标是:

(1) 将

.move(to:)
替换为
.addLine(to:)
,然后 (2) 将
.addLine(to:)
之一更改为
.move(to:)

所有其他

CGPoints
保持不变。

为了获得起点,我设置:

savedPathPoint = myPath.currentPoint

我停止以下动作。

在某些时候,我想以新的起点=

savedPathPoint
通过

恢复以下动作
    myPath.move(to: savedPathPoint)

    let myAction = SKAction.follow(
                               myPath.cgPath,
                               asOffset: false,
                               orientToPath: true,
                               duration: 0.1)
    mySprite.run(myAction)

看了上面的问题描述,确实显得很简单。

尽管如此,它的实现还是让我感到兴奋。

提前感谢大家提供任何提示。

uibezierpath
1个回答
0
投票

一个

.move(to:)
8
.addLine(to:)
意味着总共 9 分。 “重建”路径几乎不需要时间。

假设你有:

var pts: [CGPoint]   // an array of 9 points
var startIDX: Int    // the index in the array where you want to "start"

你可以使用这样的函数:

private func generatePath(from ptsArray:[CGPoint], startingAt: Int) -> CGPath {

    var idx = startingAt
    
    if idx > pts.count {
        idx = 0
    }
    
    var ptsCopy = Array(ptsArray[idx...])
    ptsCopy.append(contentsOf: ptsArray[0..<idx])
    
    let pth = CGMutablePath()
    pth.move(to: ptsCopy.removeFirst())
    while !ptsCopy.isEmpty {
        pth.addLine(to: ptsCopy.removeFirst())
    }
    
    // pth is now the same path, but
    //  starting at pts[idx] and
    //  ending at pts[idx-1]

    return pth
    
}

并用以下方式调用它:

let newPath: CGPath = generatePath(from: pts, startingAt: startIDX)
© www.soinside.com 2019 - 2024. All rights reserved.