如何为swift中绘制的曲线找到beizerpath的endangle?

时间:2017-11-14 06:17:58

标签: ios swift uibezierpath cashapelayer

我画了一个带有start&的beizer路径。结束角度,创造一个整圆

circle = UIView(frame: CGRect(x: 100, y: 100, width: 200, height:200))
        circle.layoutIfNeeded()
        self.view.addSubview(circle)
        let centerPoint = CGPoint (x: circle.bounds.width / 2, y: circle.bounds.width / 2)
        let circleRadius : CGFloat = circle.bounds.width / 2 * 0.83
        circlePath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true)

之后我在那个圆圈画了一条曲线&没有把它填满圆圈。

progressCircle = CAShapeLayer ()
        progressCircle.path = circlePath?.cgPath
        progressCircle.strokeColor = UIColor.red.cgColor
        progressCircle.fillColor = UIColor.clear.cgColor
        progressCircle.lineWidth = 4.0
        progressCircle.strokeStart = 0
        progressCircle.strokeEnd = 0.7
        circle.layer.addSublayer(progressCircle)

之后我想在曲线的末尾添加图像以便这样做我创建了另一个biezer路径。但是这里的问题是它应该以曲线结束结束。所以我无法找到结束角度基于CASHAPELAYER的endStroke的beizer路径。请告诉我如何根据曲线终点找到新的beizer路径的结束角度。

let centerPoint = CGPoint (x: circle.bounds.width / 2, y: circle.bounds.width / 2)
        let circleRadius : CGFloat = circle.bounds.width / 2 * 0.83
        let arcStartAngle: Double = 0.0


        let rotationDiff = 360 - abs((0.0 - 270))
        let startAngle: CGFloat = -1.57
        let endAngle: CGFloat = CGFloat(Double.pi * 0.7)
        let bpath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

enter image description here

1 个答案:

答案 0 :(得分:0)

class ViewController: UIViewController {

    var array = [[CGFloat:CGFloat]]()
    var midX : CGFloat = CGFloat()
    var midY : CGFloat = CGFloat()
    let radius : CGFloat = 100.0

    override func viewDidLoad() {
        super.viewDidLoad()

        midX = self.view.frame.midX
        midY = self.view.frame.midY

        let path = UIBezierPath()
        path.addArc(withCenter: CGPoint.init(x: midX, y: midY), radius: radius, startAngle: 0, endAngle:2 * .pi, clockwise: true)
        let shape = CAShapeLayer()
        shape.path = path.cgPath
        shape.lineWidth = 5.0
        shape.fillColor = UIColor.clear.cgColor
        shape.strokeColor = UIColor.lightGray.cgColor
        self.view.layer.addSublayer(shape)


        var temp090x = [CGFloat:CGFloat]()
        var temp90180x = [CGFloat:CGFloat]()
        var temp180270x = [CGFloat:CGFloat]()
        var temp270360x = [CGFloat:CGFloat]()



        for i in 1...360 {
            let path3 = UIBezierPath()
            path3.move(to: CGPoint.init(x: midX, y: midY))
            path3.addLine(to:CGPoint.init(x: midX + radius * sin(CGFloat(i)), y:midY + radius*cos(CGFloat(i))))
            //design path in layer
            if midX + radius * sin(CGFloat(i)) > midX && midY + radius*cos(CGFloat(i)) < midY {
                temp090x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
            }else if midX + radius * sin(CGFloat(i)) > midX && midY + radius*cos(CGFloat(i)) > midY {
                temp90180x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
            }else if midX + radius * sin(CGFloat(i)) < midX && midY + radius*cos(CGFloat(i)) > midY {
                temp180270x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
            }else if midX + radius * sin(CGFloat(i)) < midX && midY + radius*cos(CGFloat(i)) < midY {
                temp270360x[midX + radius * sin(CGFloat(i))] = midY + radius*cos(CGFloat(i))
            }
            let shapeLayer3 = CAShapeLayer()
            shapeLayer3.path = path3.cgPath
            shapeLayer3.strokeColor = UIColor.clear.cgColor
            shapeLayer3.lineWidth = 1.0
            self.view.layer.addSublayer(shapeLayer3)
        }

        let tempSorted090x = BubbleAsceSort(array: temp090x)
        let tempSorted90180x = BubbleDescSort(array: temp90180x)
        let tempSorted180270x = BubbleDescSort(array: temp180270x)
        let tempSorted270360x = BubbleAsceSort(array: temp270360x)

        for item in tempSorted090x {
            array.append(item)
        }
        for item in tempSorted90180x {
            array.append(item)
        }
        for item in tempSorted180270x {
            array.append(item)
        }
        for item in tempSorted270360x {
            array.append(item)
        }

        let lbl = UILabel()
        lbl.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
        lbl.backgroundColor = UIColor.black
        lbl.layer.cornerRadius = 15


        for i in 1...180 {
            let xpos = Array(array[i-1].keys)[0]
            let ypos = Array(array[i-1].values)[0]

            let xpos1 = Array(array[i].keys)[0]
            let ypos1 = Array(array[i].values)[0]

            let path3 = UIBezierPath()
            path3.move(to: CGPoint.init(x: xpos, y: ypos))
            path3.addLine(to:CGPoint.init(x: xpos1, y:ypos1))
            let shapeLayer3 = CAShapeLayer()
            shapeLayer3.path = path3.cgPath
            shapeLayer3.strokeColor = UIColor.red.cgColor
            shapeLayer3.lineWidth = 5.0
            self.view.layer.addSublayer(shapeLayer3)


        }
        lbl.center = CGPoint.init(x: Array(array[180].keys)[0], y: Array(array[180].values)[0])
        self.view.addSubview(lbl)
    }

    func BubbleDescSort(array : [CGFloat:CGFloat]) -> [[CGFloat:CGFloat]] {
        var sortedArray = Array(array.keys)
        var sortedvalueArray = Array(array.values)
        var sortedAboveIndex = sortedArray.count-1 // Assume all values are not in order
        repeat {
            var lastSwapIndex = 0

            for i in 1...sortedAboveIndex{
                if (sortedArray[i] as AnyObject) as! CGFloat > (sortedArray[i - 1] as AnyObject) as! CGFloat  {
                    sortedArray.swapAt(i, i-1)
                    sortedvalueArray.swapAt(i, i-1)
                    lastSwapIndex = i
                }
            }
            sortedAboveIndex = lastSwapIndex

        } while (sortedAboveIndex != 0)
        var index = 0
        var arr = [[CGFloat:CGFloat]]()
        for item in sortedArray {
            var dic = [CGFloat:CGFloat]()
            dic[item] = sortedvalueArray[index]
            arr.append(dic)
            index += 1
        }
        return arr
    }

    func BubbleAsceSort(array : [CGFloat:CGFloat]) -> [[CGFloat:CGFloat]] {
        var sortedArray = Array(array.keys)
        var sortedvalueArray = Array(array.values)
        var sortedAboveIndex = sortedArray.count-1 // Assume all values are not in order
        repeat {
            var lastSwapIndex = 0

            for i in 1...sortedAboveIndex{
                if (sortedArray[i - 1] as AnyObject) as! CGFloat > (sortedArray[i] as AnyObject) as! CGFloat {
                    sortedArray.swapAt(i, i-1)
                    sortedvalueArray.swapAt(i, i-1)
                    lastSwapIndex = i
                }
            }
            sortedAboveIndex = lastSwapIndex

        } while (sortedAboveIndex != 0)
        var index = 0
        var arr = [[CGFloat:CGFloat]]()
        for item in sortedArray {
            var dic = [CGFloat:CGFloat]()
            dic[item] = sortedvalueArray[index]
            arr.append(dic)
            index += 1
        }
        return arr
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

在此代码中,您需要根据360度设置进度百分比。在for循环和圆形lbl中心点x和y pos的上限。这里只需要180替换您的进度百分比