Swift 3 中 CGMutablePath.addArc 函数无法工作?

4
在Xcode 8 beta 6中,一些添加路径的函数发生了变化,包括添加弧线的函数:
func addArc(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool, transform: CGAffineTransform = default)

除了函数定义之外,在苹果网站上没有其他文档。我无法从该函数获取实际弧形,并一直依赖于使用切线的第二个版本。有人能提供一个可用的示例吗?这可能只是一个错误吗?

以下是受此更改破坏的函数:

public class func createHorizontalArcPath(_ startPoint:CGPoint, width:CGFloat, arcHeight:CGFloat, closed:Bool = false) -> CGMutablePath
    {
        // http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths

        let arcRect = CGRect(x: startPoint.x, y: startPoint.y-arcHeight, width: width, height: arcHeight)

        let arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2) / (8*arcRect.size.height));
        let arcCenter = CGPoint(x: arcRect.origin.x + arcRect.size.width/2, y: arcRect.origin.y + arcRadius);

        let angle = acos(arcRect.size.width / (2*arcRadius));
        let startAngle = CGFloat(M_PI)+angle // (180 degrees + angle)
        let endAngle = CGFloat(M_PI*2)-angle // (360 degrees - angle)

        let path = CGMutablePath();
        path.addArc(center: arcCenter, radius: arcRadius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        if(closed == true)
        {path.addLine(to: startPoint)}
        return path;
    }

3
它为什么“不工作”?你得到了什么结果,又期望得到什么? - Martin R
“创建弧形”的方法只有一种可能行不通,你认为呢?我甚至包含了一个演示函数。 - GoldenJoe
它无法编译。它可能在运行时崩溃。它可能会为某些输入数据产生“错误”的路径。它可能会为所有输入数据产生错误的路径。 - Martin R
1个回答

9

您的Swift代码基于来自http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths的Objective-C代码,其中弧形路径的创建如下:

CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius,
             startAngle, endAngle, 0);

具体来说,0被传递给最后一个参数bool clockwise。在Swift中应该将其翻译为false而不是true

path.addArc(center: arcCenter, radius: arcRadius,
            startAngle: startAngle, endAngle: endAngle, clockwise: false) 

哈哈哈,我怎么会错过那个。唉,那种凌晨三点的感觉真糟糕。你发现得好。 - GoldenJoe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接