xcode CGMutablePath.addArc 在 Swift 3 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39061883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:09:38  来源:igfitidea点击:

CGMutablePath.addArc not working in Swift 3?

iosswiftxcodecgpath

提问by GoldenJoe

In Xcode 8 beta 6, some of the functions to add a path changed, including those that add an arc:

在 Xcode 8 beta 6 中,一些添加路径的函数发生了变化,包括添加弧的函数:

func addArc(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool, transform: CGAffineTransform = default)

Beyond a definition of the function, there is no documentation on Apple's site. I've been unable to get an actual arc from this function, and have been relying on a second version that uses tangents. Can anyone provide a working sample? Could it just be bugged?

除了函数的定义之外,Apple 的网站上没有任何文档。我一直无法从这个函数中得到实际的弧线,一直依赖于使用切线的第二个版本。任何人都可以提供工作样本吗?可能只是被窃听了吗?

Here is a function that is broken by the change:

这是一个被更改破坏的函数:

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;
    }

回答by Martin R

Your Swift code is based on the Objective-C code from http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths, where the arc path is created as

您的 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);

In particular, 0is passed as argument to the last parameter bool clockwise. That should be translated to falsein Swift, not true:

特别是,0作为参数传递给最后一个参数bool clockwise。这应该false在 Swift 中转换为,而不是true

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