在弧

时间:2017-10-08 22:11:49

标签: ios swift

我试图制作一个按钮,当按下时会形成一个选项弧。我能够在对角线上绘制选项(见下图),但无法弄清楚如何以弧形显示它们。有什么建议吗?

我得到了什么

enter image description here

代码

        let resourceObjects = ["sprite", "shape", "text", "function", "sound", "variable", "list"]

        let radius : CGFloat = 120
        var shiftX : CGFloat = 0
        var shiftY : CGFloat = radius


        // x^2 + y^2 = radius

        for o in resourceObjects {


            let cell = UIButton()
            cell.frame = CGRect(x: sender.frame.origin.x + shiftX, y: sender.frame.origin.y + 50 - shiftY, width: screenWidth, height: 30)
            self.view.addSubview(cell)

            let img = UIImageView()
            img.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
            img.image = UIImage(named: String(o) + ".png")
            cell.addSubview(img)

            let lbl = UILabel()
            lbl.frame = CGRect(x: 27, y: 0, width: screenWidth - 60, height: 30)
            lbl.font = UIFont.systemFont(ofSize: 12)
            lbl.text = o
            lbl.sizeToFit()
            lbl.frame = CGRect(x: 27, y: 0, width: lbl.frame.width, height: lbl.frame.height)
            cell.addSubview(lbl)

            cell.frame = CGRect(x: sender.frame.origin.x + shiftX, y: sender.frame.origin.y + 50 - shiftY, width: lbl.frame.width + 27, height: 30)

            shiftX += radius / CGFloat(resourceObjects.count)
            shiftY -= radius / CGFloat(resourceObjects.count)

            options.append(cell)

        }

1 个答案:

答案 0 :(得分:1)

使用trig计算位置:将整个圆圈的角度从0变为2 pi。将该范围的一小部分用于圆的一部分。

x = cos(angle) * radius + origin.x
y = sin(angle) * radius + origin.y