我有一组按钮,我用顺时针旋转的方式显示它们,但我无法正确地点击它们:
我认为偏移量存在问题,但我不知道如何解决,有什么建议吗?
这是代码:
这是代码:
struct CategoryView: View {
// Circle Radius
@State private var radius: Double = 150
let circleSize: Double = 350
// Degree of circle
@State private var degree = -90.0
let cards = ["John", "Mark", "Alex", "Kevin", "Jimmy"]
var body: some View {
ZStack {
let anglePerCount = Double.pi * 2.0 / Double(cards.count)
ForEach(0..<cards.count, id: \.self) { index in
let angle = Double(index) * anglePerCount
let xOffset = CGFloat(radius * cos(angle))
let yOffset = CGFloat(radius * sin(angle))
Button {
} label: {
Text(cards[index])
.font(.title)
.fontWeight(.bold)
.rotationEffect(Angle(radians: angle + Double.pi/2))
.offset(x: xOffset, y: yOffset)
}
}
}
.rotationEffect(Angle(degrees: degree))
.onAppear() {
radius = circleSize/2 - 47 // 47 is for padding
}
}
}