简体   繁体   English

图像上的点击手势 SwiftUI

[英]On tap gesture on an image SwiftUI

So I am trying to change between views when someone clicks on the image, but the action after the ontapgesture is always "expression is unused".因此,当有人单击图像时,我试图在视图之间进行更改,但是ontapgesture之后的动作始终是“未使用表达式”。 I tried changing it to a navigation view as well as other things but I feel like nothing seems to be working.我尝试将其更改为导航视图以及其他内容,但我觉得似乎没有任何效果。

Code below:下面的代码:

    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
                }
            }
            
        }
    }
}

Thank you.谢谢你。

The error you get is "correct" in that menuu2 does not do anything, it is just there.你得到的错误是“正确的”,因为 menuu2 没有做任何事情,它就在那里。

There are a number of ways to change view on tap, this is just one way:有多种方法可以随时更改视图,这只是一种方法:

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
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM