CGMutablePath.addArc not working in Swift 3?

In Xcode 8 beta 6, some functions for adding a path have changed, including those that add an arc:

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

Other than defining a feature, there is no documentation on the Apple website. I could not get the actual arc from this function and relied on the second version, which uses tangents. Can someone provide a working sample? Could this just be bugged?

Here is the function that is violated 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;
    }
+4
source share
1 answer

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

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

, 0 bool clockwise. false Swift, true:

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

Source: https://habr.com/ru/post/1651923/


All Articles