繁体   English   中英

SwiftUI ScrollView手势识别器

[英]SwiftUI ScrollView gesture recogniser

如何检测何时拖动 ScrollView?

在我的 ScrollView 中,我有一个@Binding scrollPositionOffset变量,我使用.onChange(of:)观看,然后使用ScrollViewReader.scrollTo()以编程方式滚动到 position 。 这很好用,但是当我直接滚动 ScrollView 时,我还需要更新scrollPositionOffset 我正在努力做到这一点,因为这会触发.onChange(of:)闭包并进入循环。

我的解决方案是仅在我将localScrolling变量设置为 false 时有条件地调用ScrollViewReader.scrollTo() 我尝试使用DragGesture.onChanged.onEnded进行设置,但这与导致滚动的拖动手势不同,因此.onEnded永远不会触发。

我认为我需要的是一个类似于 UIScrollView 的isDraggingisTracking的 ScrollView 的 @GestureRecognizer(我知道我可以使用 UIScrollView,但我不知道怎么做,而且这似乎可能更有效!!我会接受一个向我展示如何将其放入 SwiftUIView 的答案)

上下文(如果有人对我的实际情况有更清洁的解决方案):

我有一个 ScrollView,我正在以编程方式滚动以创建类似于 Xcode 中的 Minimap 视图的效果(即,我有一个与 ScrollView 相邻的缩小视图,并且拖动 minimap 会导致 ScrollView 滚动)。

这在我使用小地图时效果很好,但我正在努力让相反的事情发生:移动 ScrollView 的 position 以更新小地图视图。

代码


@Binding var scrollPositionOffset: CGFloat
let zoomMultiplier:CGFloat = 1.5

 var body: some View{
        
        ScrollViewReader { scrollViewProxy in
            GeometryReader{ geometry in
                ScrollView {
                    ZStack(alignment:.top){

         //The content of my ScrollView

                    MagnifierView()
                        .frame(height: geometry.size.height * zoomMultiplier)
                    
         //I'm using this as my offset reference

                        Rectangle()
                            .frame(height:10)
                            .alignmentGuide(.top) { _ in
                                geometry.size.height * zoomMultiplier * -scrollPositionOffset
                            }
                            .id("scrollOffset")    
                    }
                }
                .onAppear(){
                    scrollViewProxy.scrollTo("scrollOffset", anchor: .top)
                }
                
                .onChange(of: scrollPositionOffset, perform: { _ in
            
        //Only call .scrollTo() if the view isn't already being scrolled by the user

                    if !localScrolling {
                    scrollViewProxy.scrollTo("scrollOffset", anchor: .top)
                    }
                    
                })
                
                .gesture(
                    DragGesture()
                        .onChanged{gesture in
                            localScrolling = true
                            
                            let offset = gesture.location.y/(zoomMultiplier * geometry.size.height)

                            scrollPositionOffset = offset
                        }
        
                        .onEnded({gesture in

     //Doesn't ever fire when scrolling

                            localScrolling = false
                        })
                )
            }
        }
    }

使用ScrollViewStyle

struct CustomScrollView: ScrollViewStyle {
    @Binding var isDragging: Bool
    func make(body: AnyView, context: Context) -> some View {
        body
    }
    func makeCoordinator() -> Coordinator {
        return Coordinator(parent: self)
    }
    class Coordinator: ScrollViewCoordinator {
        var parent: CustomScrollView
        init(parent: CustomScrollView) {
            self.parent = parent
        }
        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
            parent.isDragging = false
        }
        func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
            parent.isDragging = true
        }
    }
}

struct TestView: View {
    @State var isDragging = false
    var body: some View {
        ScrollView {
            
        }.scrollViewStyle(CustomScrollView(isDragging: $isDragging))
    }
}

暂无
暂无

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

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