指定视图中的区域 — Swift UI

huangapple go评论69阅读模式
英文:

Specify an Area in a View — Swift UI

问题

我正在创建一个游戏,允许用户点击特定区域,就像点击绿点一样。当用户点击该点时,不会显示任何反应。但如果用户点击点外的位置,会弹出警告。

指定视图中的区域 — 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.

指定视图中的区域 — Swift UI

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.

huangapple
  • 本文由 发表于 2023年7月18日 02:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定