Long press of NavigationView only work on the left part, not all the NavigationLink?

自闭症网瘾萝莉.ら 提交于 2021-02-08 07:37:22

问题


Following is a NavigationView, the view pops to Destination2 when long press the NavigationLink and to Destination1 when normally tap it. But the right zone of the NavigationLink in the picture cannot be long pressed.

Does anyone know the reason? Thanks!

import SwiftUI

struct ContentView: View {

    @State private var isLongPressed = false
    @State var currentTag: Int?

    let lyrics = ["OutNotWorkA", "OutNotWorkB", "OutNotWorkC"]

    var body: some View {
        NavigationView {

            List {

                ForEach(0..<lyrics.count) { index in
                    VStack{
                        HStack(alignment: .top) {
                           NavigationLink(destination: Group
                                { if self.isLongPressed { Destination2() } else { Destination1() } }, tag: index, selection: self.$currentTag
                            ) {
                                Text(self.lyrics[index])
                            }


                        }
                    }.simultaneousGesture(LongPressGesture().onEnded { _ in
                        print("Got Long Press")
                        self.currentTag = index
                        self.isLongPressed = true
                            })
                    .simultaneousGesture(TapGesture().onEnded{
                        print("Got Tap")
                        self.currentTag = index
                        self.isLongPressed = false
                    })
                    .onAppear(){
                        self.isLongPressed = false
                    }

                }
            }
        }
    }
}

struct Destination1: View {
    var body: some View {
        Text("Destination1")
    }
}

struct Destination2: View {
    var body: some View {
        Text("Destination2")
    }
}


回答1:


Then how to handle the whole part?

Find below the fix

VStack{
    HStack(alignment: .top) {
       NavigationLink(destination: Group
            { if self.isLongPressed { Destination2() } else { Destination1() } }, tag: index, selection: self.$currentTag
        ) {
            Text(self.lyrics[index])
        }


    }
}
.contentShape(Rectangle())           // << here !!
.simultaneousGesture(LongPressGesture().onEnded { _ in



回答2:


LongPressGesture only works on the visualized part of the label.

The easiest way to handle this problem is a little workaround with a lot of spaces:

Text(self.lyrics[index]+"                                                                   ")

Because only using spaces doesn't create a line break this makes no visual problems in your App.



来源:https://stackoverflow.com/questions/61865269/long-press-of-navigationview-only-work-on-the-left-part-not-all-the-navigationl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!