加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:07:38 所属栏目:百科 来源:网络整理
导读:在 Xcode 8 beta 6中,添加路径的一些函数发生了变化,包括添加弧的函数: func addArc(center: CGPoint,radius: CGFloat,startAngle: CGFloat,endAngle: CGFloat,clockwise: Bool,transform: CGAffineTransform = default) 除了函数的定义之外,Apple的网站上
在 Xcode 8 beta 6中,添加路径的一些函数发生了变化,包括添加弧的函数:

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

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

这是一个由变化打破的功能:

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

解决方法

您的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.这应该在Swift中被翻译为false,而不是真的:

path.addArc(center: arcCenter,clockwise: false)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读