NavigationView 长按只作用于左侧部分,不是所有的 NavigationLink? [英] Long press of NavigationView only work on the left part, not all the NavigationLink?

查看:28
本文介绍了NavigationView 长按只作用于左侧部分,不是所有的 NavigationLink?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个NavigationView,当长按NavigationLink 时,视图弹出到Destination2,正常点击时弹出到Destination1.但是图片中NavigationLink的右侧区域不能长按.

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

推荐答案

那整个部分怎么处理呢?

Then how to handle the whole part?

在修复下方找到

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

这篇关于NavigationView 长按只作用于左侧部分,不是所有的 NavigationLink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆