有没有办法以圆形排列布局按钮?

问题描述 投票:0回答:1

我正在尝试在角落制作一个圆形按钮,当您按下它时,它周围会弹出3个按钮。按住大头针时,类似Pinterest的菜单。我使用UIBezierPath进行了查找,但这不是我想要的。我知道有GitHub库,但我正试图从头开始。

这是我要从头开始制作的示例:

“

我想出了如何通过使用拐角半径来制作圆形按钮。而且我有一个特定的功能,它将对按钮进行动画处理,但是我不知道如何正确地将它们圈成一圈。我使用了约束,但是这使正方形布局多于圆形布局。

ios swift menu uibutton nslayoutconstraint
1个回答
1
投票

您说:

但是我不知道如何正确地将它们围成一圈。

这只是一个小三角函数,用于确定新按钮的中心应与现有按钮之间的关系。如果r是距现有圆的中心xy的距离,并且圆i的角度为θᵢ,则xᵢ = x + r * cos(θᵢ)yᵢ= y + r * sin(θᵢ)。 (在这种情况下,角度是从3点开始以弧度为单位,顺时针进行,或者为负以逆时针进行。)

例如,如果使用约束:

let r: CGFloat = 150               // let's say you want center of the circles to be 150 points away from center of existing button
let range = -CGFloat.pi / 2 ... 0  // and we want the angles to range from -π/2 to 0
let howMany = 5                    // and you want five of them

for i in 0 ..< howMany {
    let angle = range.lowerBound + CGFloat(i) / CGFloat(howMany - 1) * (range.upperBound - range.lowerBound)
    let offset = CGPoint(x: r * cos(angle), y: r * sin(angle))
    let button = ...
    button.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(button)
    NSLayoutConstraint.activate([
        button.centerXAnchor.constraint(equalTo: mainButton.centerXAnchor, constant: offset.x),
        button.centerYAnchor.constraint(equalTo: mainButton.centerYAnchor, constant: offset.y),
        button.widthAnchor.constraint(equalToConstant: 40),
        button.heightAnchor.constraint(equalToConstant: 40)
    ])
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.