【问题标题】:On tap gesture on an image SwiftUI图像 SwiftUI 上的点击手势
【发布时间】:2021-04-30 04:24:13
【问题描述】:

因此,当有人单击图像时,我试图在视图之间进行更改,但 ontapgesture 之后的操作始终是“未使用表达式”。我尝试将其更改为导航视图以及其他内容,但我觉得似乎没有任何效果。

代码如下:

    struct MenuView2: View {
    @State private var menuu2 = menu2()
    
    var body: some View {
        ScrollView(){
            VStack {
                ZStack {
                    Rectangle().frame(height:40).opacity(0.25).blur(radius: 10).onTapGesture {
                        print("breakfast tapped ")
                    }
                    HStack {
                        VStack(alignment: .leading, spacing: 8, content: {
                            Text("Breakfast").font(.largeTitle)
                        })
                    }
                }
                Image("breakfast").resizable().scaledToFill().onTapGesture {
                    menuu2
                }
            }
            
        }
    }
}

谢谢。

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    你得到的错误是“正确的”,因为 menuu2 没有做任何事情,它就在那里。

    有多种方法可以在点击时更改视图,这只是一种方法:

    struct MenuView2: View {
        
        @State private var menuu2 = menuu2()
        @State private var changeView = false
        
        var body: some View {
            changeView ? AnyView(theOtherView) : AnyView(theScrollView)
        }
        
        var theOtherView: some View {
            // menuu2 presumably
            // just for testing
            Text("theOtherView").onTapGesture {
                self.changeView = false
            }
        }
        
        var theScrollView: some View {
            ScrollView() {
                VStack {
                    ZStack {
                        Rectangle().frame(height:40).opacity(0.25).blur(radius: 10).onTapGesture {
                            print("breakfast tapped ")
                        }
                        HStack {
                            VStack(alignment: .leading, spacing: 8, content: {
                                Text("Breakfast").font(.largeTitle)
                            })
                        }
                    }
                    Image("breakfast").resizable().scaledToFill().onTapGesture {
                        self.changeView = true
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 非常感谢您的快速回复。那行得通!
    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多