繁体   English   中英

SwiftUI 带有点击和拖动手势的 ScrollView

[英]SwiftUI ScrollView with Tap and Drag gesture

我正在尝试实现一个带有可以点击和拖动的元素的 ScrollView。 它应该按以下方式工作:

  1. ScrollView 应该可以正常工作,因此向上/向下滑动不应干扰手势。
  2. 点击一个条目应该运行一些代码。 如果有一个“tap indicator”就好了,这样用户就知道点击已经被注册了(困难在于点击指示器应该在 touch down时触发,而不是在 touch up时触发,并且应该是活动的直到松开手指)。
  3. 长按一个条目应该激活拖动手势,这样项目就可以四处移动。

下面的代码涵盖了所有这些要求(点击指示器除外)。 但是,我不确定它为什么有效,具体来说,为什么我需要使用 .highPriorityGesture,例如不能使用.sequenced(before: ...)对 Tap Gesture 和 DragGesture 进行排序(这会阻止滚动)。

另外,我想收到触地得分事件的通知(不是触地得分,参见 2.)。 我尝试使用 LongPressGesture() 而不是 TapGesture(),但这也会阻止 ScrollView 滚动并且之后甚至不会触发 DragGesture。

有人知道如何实现吗? 或者这是SwiftUI的限制? 如果是这样,是否可以将 UIKit 的东西移植过来以实现此目的(我也已经尝试过,但没有成功,ScrollView 的内容也应该是动态的,因此移植整个 ScrollView 可能很困难)?

谢谢你的协助!

struct ContentView: View {
   
    var body: some View {
        ScrollView() {
            ForEach(0..<5, id: \.self) { i in
                ListElem()
                    .highPriorityGesture(TapGesture().onEnded({print("tapped!")}))
                    .frame(maxWidth: .infinity)
            }
        }
    }
}

struct ListElem: View {
    @GestureState var dragging = CGSize.zero
    
    var body: some View {
        Circle()
        .frame(width: 100, height: 100)
            .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .global)
                .updating($dragging, body: {t, state, _ in
                    state = t.translation
            }))
        .offset(dragging)

    }
}

我尝试了几个选项,我认为sequencedsimultaneously的组合允许两个手势同时运行。 为了实现 onTouchDown,我使用了最小距离为 0 的DragGesture

struct ContentView: View {
    
    var body: some View {
        ScrollView() {
            ForEach(0..<5, id: \.self) { i in
                ListElem()
                    .frame(maxWidth: .infinity)
            }
        }
    }
}

struct ListElem: View {
    
    @State private var offset = CGSize.zero
    @State private var isDragging = false
    @GestureState var isTapping = false
    
    var body: some View {
        
        // Gets triggered immediately because a drag of 0 distance starts already when touching down.  
        let tapGesture = DragGesture(minimumDistance: 0)
            .updating($isTapping) {_, isTapping, _ in
                isTapping = true
            }

        // minimumDistance here is mainly relevant to change to red before the drag
        let dragGesture = DragGesture(minimumDistance: 0)
            .onChanged { offset = $0.translation }
            .onEnded { _ in
                withAnimation {
                    offset = .zero
                    isDragging = false
                }
            }
        
        let pressGesture = LongPressGesture(minimumDuration: 1.0)
            .onEnded { value in
                withAnimation {
                    isDragging = true
                }
            }
        
        // The dragGesture will wait until the pressGesture has triggered after minimumDuration 1.0 seconds.
        let combined = pressGesture.sequenced(before: dragGesture)
        
        // The new combined gesture is set to run together with the tapGesture.
        let simultaneously = tapGesture.simultaneously(with: combined)
        
        return Circle()
            .overlay(isTapping ? Circle().stroke(Color.red, lineWidth: 5) : nil) //listening to the isTapping state
            .frame(width: 100, height: 100)
            .foregroundColor(isDragging ? Color.red : Color.black) // listening to the isDragging state.
            .offset(offset)
            .gesture(simultaneously)
        
    }
}

对于此处感兴趣的任何人来说,自定义滚动视图不会被其中一条评论中提到的其他手势所阻止。 因为这不可能用标准的 ScrollView 来解决。

Github 上 SwiftUI 的 OpenScrollView

归功于

https://stackoverflow.com/a/59897987/12764795 http://developer.apple.com/documentation/swiftui/composing-swiftui-gestures https://www.hackingwithswift.com/books/ios-swiftui/how在 swiftui 中使用手势

暂无
暂无

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

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