gpt4 book ai didi

ios - 如何指定或更改按钮 SwiftUI 的可点击区域

转载 作者:行者123 更新时间:2023-12-01 21:27:45 28 4
gpt4 key购买 nike

我刚刚进入 SwiftUI 的世界,所以我不知道这个问题是否有一个非常明显的答案,但基本上我已经创建了一个按钮,并试图让按钮成为两个同心圆。然而,一旦我在模拟器上运行我的代码,我发现我可以点击屏幕上的任何地方,并且按钮将被“按下”(我使用打印语句对其进行了测试)。我想将按钮的可点击区域限制在圆圈的区域或者可能是两个圆圈的正方形区域。
这是我的代码:

    Button(action: {
print("Do Something")

}) {

Circle().stroke(Color.white, lineWidth: 3)
.frame(width: 65, height: 65)
.position(x: x, y: y)
.overlay(
Circle().stroke(Color.white, lineWidth: 3)
.frame(width: 55, height: 55)
.position(x: x, y: y)
)

}
这是我在实时预览窗口中单击它时的样子:
enter image description here

最佳答案

您应该删除 .position修饰符,因为它将 View 位置(在您的情况下为按钮内容)切换到全局坐标。
而是使用堆栈布局,如下所示

    VStack {
Spacer() // << pushes button down
Button(action: {
print("Do Something")

}) {

Circle().stroke(Color.white, lineWidth: 3)
.frame(width: 65, height: 65)
.overlay(
Circle().stroke(Color.white, lineWidth: 3)
.frame(width: 55, height: 55)
)

}
}

关于ios - 如何指定或更改按钮 SwiftUI 的可点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63286695/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com