簡體   English   中英

SwiftUI TapGesture 和 LongPressGesture 在 ScrollView 中,點擊指示不起作用

[英]SwiftUI TapGesture and LongPressGesture in ScrollView with tap indication not working

我正在努力在 ScrollView 中同時實現 TapGesture 和 LongPressGesture。 使用 .onTapGesture 和 .onLongPressGesture 一切正常,但我希望當用戶點擊按鈕時,按鈕的不透明度會降低,就像普通的 Button() 一樣。

但是,無論出於何種原因, Button() 都無法在長按時執行某些操作。 所以我嘗試使用.gesture(LongPressGesture()...)。 這種方法有效並顯示了點擊指示。 不幸的是,這不適用於 ScrollView:你不能再滾動它了!

所以我做了一些研究,發現在 LongPressGesture 之前必須有一個 TapGesture ,這樣 ScrollView 才能正常工作。 確實是這樣,但是我的 LongPressGesture 不再起作用了。

希望有人有解決方案...


struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal){
            HStack{
                ForEach(0..<5){ _ in
                    Button()
                }
            }
        }
    }
}

struct Button: View{
    
    @GestureState var isDetectingLongPress = false
    @State var completedLongPress = false
    
    var body: some View{
        
        Circle()
            .foregroundColor(.red)
            .frame(width: 100, height: 100)
            .opacity(self.isDetectingLongPress ? 0 : 1)
            
            // That works, but there is no indication for the user that the UI recognized the gesture
            //                        .onTapGesture {
            //                            print("Tapped!")
            //                       }
            //                        .onLongPressGesture(minimumDuration: 0.5){
            //                            print("Long pressed!")
            //                        }
            
            
            // The approach (*) shows the press indication, but the ScrollView is stuck because there is no TapGesture
            
            // If I add a dummy TapGesture, the LongPressGesture won't work anymore but now the ScrollView works as expected
            //.onTapGesture {}
            
            // (*)
            .gesture(LongPressGesture()
                .updating(self.$isDetectingLongPress) { currentstate, gestureState,
                    transaction in
                    gestureState = currentstate
            }
            .onEnded { finished in
                self.completedLongPress = finished
                }
        )
    }
}

我已經嘗試了許多嘗試 onTapGesture + LongPressGesture + 自定義時間和動畫的組合,並且許多工作/幾乎/但留下了一些小煩惱。 這就是我發現的完美工作。 在 iOS 13.6 上測試。

使用此解決方案,您的滾動視圖仍然滾動,您可以按下按鈕 animation,長按按鈕也可以。

struct MainView: View {
...
  Scrollview {
    RowView().highPriorityGesture(TapGesture()
      .onEnded { _ in
      // The function you would expect to call in a button tap here.
    })

}
}

struct RowView: View {

@State var longPress = false
var body: some View {
    Button(action: {
        if (self.longPress) {
            self.longPress.toggle()
        } else {
            // Normal button code here
        }
    }) {
// Buttons LaF here
}
// RowView code here.
.simultaneousGesture(LongPressGesture(minimumDuration: 0.5)
  .onEnded { _ in
    self.longPress = true
  })
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM