英文:
Specify an Area in a View — Swift UI
问题
我正在创建一个游戏,允许用户点击特定区域,就像点击绿点一样。当用户点击该点时,不会显示任何反应。但如果用户点击点外的位置,会弹出警告。
我知道警告是如何工作的,但我不知道如何指定一个区域(绿点),以便我可以输出警告。
如果有人可以给我一些提示,提前感谢;p
英文:
I am creating a game that allow the user to tap on specific area like a green dot. When the user tap on the dot, no reaction is shown. But if the user tap on positions outside the dot, an alert pops up.
I know how alert works, but I don’t know how to specify an area (the green dot) so that I can output an alert.
Thanks for advance if anyone can give me some hints ;p
答案1
得分: 2
我还没有测试过,但为什么不使用ZStack
呢?
struct ExampleView: View {
@State private var showAlert = false
var body: some View {
ZStack {
Color.white
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
.onTapGesture {
showAlert.toggle()
}
Circle()
.foregroundColor(Color.green)
.frame(width: 120, height: 120)
}
.alert("Message", isPresented: $showAlert) {
Button("Ok", role: .cancel, action: { })
}
}
}
或者也可以考虑使用@jnpdx的想法?
struct OtherExampleView: View {
@State private var showAlert = false
var body: some View {
VStack {
Circle()
.foregroundColor(Color.green)
.frame(width: 120, height: 120)
.background {
Color.white
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
.onTapGesture {
showAlert.toggle()
}
}
}
.alert("Message", isPresented: $showAlert) {
Button("Ok", role: .cancel, action: { })
}
}
}
我明天会测试这两个选项。
英文:
I haven't tested but why not using a ZStack
?
struct ExampleView: View {
@State private var showAlert = false
var body: some View {
ZStack {
Color.white
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
.onTapGesture {
showAlert.toggle()
}
Circle()
.foregroundColor(Color.green)
.frame(width: 120, height: 120)
}
.alert("Message", isPresented: $showAlert) {
Button("Ok", role: .cancel, action: { })
}
}
}
Or maybe using the idea of @jnpdx?
struct OtherExampleView: View {
@State private var showAlert = false
var body: some View {
VStack {
Circle()
.foregroundColor(Color.green)
.frame(width: 120, height: 120)
.background {
Color.white
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
.onTapGesture {
showAlert.toggle()
}
}
}
.alert("Message", isPresented: $showAlert) {
Button("Ok", role: .cancel, action: { })
}
}
}
I'll test these two options tomorrow.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论