Disable ScrollView until Drag Threshold is passed all in one gesture

i am trying to implement a custom gesture on this view that makes it so that only when a certain drag threshold is passed, does the underlying scrollview receive the drag touch events and begin scrolling all in one gesture. trying to mimic what i assume to be the gesture logic in the new ios 18 photos app

in my head i am envisioning it like the gesture blocks touch events from passing through until a certain threshold then it allows recognizing simultaneously with


enum DragState {
    case dragging
    case draggedUp
    case draggedDown
    case idle
}

struct Home: View {
    // MARK: - Environment Variables

    @Environment(.viewSize) private var viewSize
    @Environment(.safeAreaInsets) private var safeAreaInsets
    @EnvironmentObject private var windowState: UIState

    // MARK: - State Variables

    @State var isExpanded = false
    @State var dragState: DragState = .idle

    @State var scrollOffset: CGFloat = 0
    @State var topScrollOffset: CGFloat = 0

    @State var bottomOffset: CGFloat = 0
    @State var topOffset: CGFloat = 0

    // MARK: - Constants

    let maxTranslation: CGFloat = 124

    var body: some View {
        let offset: CGFloat = viewSize.height * 0.4

        ScrollView {
            VStack(spacing: 0) {
                ScrollView(.vertical) {
                    LazyVGrid(columns: [GridItem(.adaptive(minimum: 120), spacing: 4)], spacing: 4) {
                        ForEach(1 ... 50, id: .self) { _ in
                            Rectangle()
                                .fill(Color(.systemGray5))
                                .frame(height: 120)
                        }
                    }
                    .offset(y: topOffset)
                }
                .onScrollGeometryChange(for: CGFloat.self, of: {
                    /// This will be zero when the content is placed at the bottom
                    $0.contentOffset.y - $0.contentSize.height + $0.containerSize.height
                }, action: { oldValue, newValue in
                    guard oldValue != newValue else { return }
                    topScrollOffset = newValue

                    if isExpanded, newValue >= 0 {
                        //
                    }
                })
                .scrollClipDisabled()
                .defaultScrollAnchor(.bottom)
                .frame(height: viewSize.height)
                .border(.blue, width: 2)

                VStack(spacing: 4) {
                    ForEach(1 ... 12, id: .self) { _ in
                        Rectangle()
                            .fill(Color(.systemGray6))
                            .frame(height: 120)
                    }
                }
                .border(.red, width: 2)
                .offset(y: bottomOffset)
            }
        }
        .onScrollGeometryChange(for: CGFloat.self, of: {
            /// This will be zero when the content is placed at the bottom
            $0.contentOffset.y
        }, action: { oldValue, newValue in

            guard oldValue != newValue else { return }
            scrollOffset = newValue

            if !isExpanded, newValue <= 0 {
                //
            }

        })
        .defaultScrollAnchor(.top)
        .scrollClipDisabled()
        .frame(height: viewSize.height, alignment: .top)
        .clipped()
        .scaleEffect(0.65)
        .gesture(
            CustomGesture(isEnabled: true) { recognizer in
                let translation = recognizer.translation(in: recognizer.view).y
                let state = recognizer.state

                // MARK: - Gesture Began

                if state == .began {
                    dragState = .dragging
                }

                // MARK: - Gesture Changed

                if state == .changed {
                }

                // MARK: - Gesture Ended

                if state == .ended {
                }
            }
        )
        .onAppear {
            DispatchQueue.main.async {
                topOffset = -offset
                bottomOffset = -offset
            }
        }
    }
}

since SwiftUI gestures don’t work simultaneously with scrollView


struct CustomGesture: UIGestureRecognizerRepresentable {
    var isEnabled: Bool
    var handle: (UIPanGestureRecognizer) -> ()

    func makeCoordinator(converter: CoordinateSpaceConverter) -> Coordinator {
        Coordinator()
    }

    func makeUIGestureRecognizer(context: Context) -> UIPanGestureRecognizer {
        let gesture = UIPanGestureRecognizer()
        gesture.delegate = context.coordinator
        return gesture
    }

    func updateUIGestureRecognizer(_ recognizer: UIPanGestureRecognizer, context: Context) {
        recognizer.isEnabled = isEnabled
    }

    func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) {
        handle(recognizer)
    }

    class Coordinator: NSObject, UIGestureRecognizerDelegate {
        func gestureRecognizer(
            _ gestureRecognizer: UIGestureRecognizer,
            shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
        ) -> Bool {
            return true
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Disable ScrollView until Drag Threshold is passed all in one gesture

i am trying to implement a custom gesture on this view that makes it so that only when a certain drag threshold is passed, does the underlying scrollview receive the drag touch events and begin scrolling all in one gesture. trying to mimic what i assume to be the gesture logic in the new ios 18 photos app

in my head i am envisioning it like the gesture blocks touch events from passing through until a certain threshold then it allows recognizing simultaneously with


enum DragState {
    case dragging
    case draggedUp
    case draggedDown
    case idle
}

struct Home: View {
    // MARK: - Environment Variables

    @Environment(.viewSize) private var viewSize
    @Environment(.safeAreaInsets) private var safeAreaInsets
    @EnvironmentObject private var windowState: UIState

    // MARK: - State Variables

    @State var isExpanded = false
    @State var dragState: DragState = .idle

    @State var scrollOffset: CGFloat = 0
    @State var topScrollOffset: CGFloat = 0

    @State var bottomOffset: CGFloat = 0
    @State var topOffset: CGFloat = 0

    // MARK: - Constants

    let maxTranslation: CGFloat = 124

    var body: some View {
        let offset: CGFloat = viewSize.height * 0.4

        ScrollView {
            VStack(spacing: 0) {
                ScrollView(.vertical) {
                    LazyVGrid(columns: [GridItem(.adaptive(minimum: 120), spacing: 4)], spacing: 4) {
                        ForEach(1 ... 50, id: .self) { _ in
                            Rectangle()
                                .fill(Color(.systemGray5))
                                .frame(height: 120)
                        }
                    }
                    .offset(y: topOffset)
                }
                .onScrollGeometryChange(for: CGFloat.self, of: {
                    /// This will be zero when the content is placed at the bottom
                    $0.contentOffset.y - $0.contentSize.height + $0.containerSize.height
                }, action: { oldValue, newValue in
                    guard oldValue != newValue else { return }
                    topScrollOffset = newValue

                    if isExpanded, newValue >= 0 {
                        //
                    }
                })
                .scrollClipDisabled()
                .defaultScrollAnchor(.bottom)
                .frame(height: viewSize.height)
                .border(.blue, width: 2)

                VStack(spacing: 4) {
                    ForEach(1 ... 12, id: .self) { _ in
                        Rectangle()
                            .fill(Color(.systemGray6))
                            .frame(height: 120)
                    }
                }
                .border(.red, width: 2)
                .offset(y: bottomOffset)
            }
        }
        .onScrollGeometryChange(for: CGFloat.self, of: {
            /// This will be zero when the content is placed at the bottom
            $0.contentOffset.y
        }, action: { oldValue, newValue in

            guard oldValue != newValue else { return }
            scrollOffset = newValue

            if !isExpanded, newValue <= 0 {
                //
            }

        })
        .defaultScrollAnchor(.top)
        .scrollClipDisabled()
        .frame(height: viewSize.height, alignment: .top)
        .clipped()
        .scaleEffect(0.65)
        .gesture(
            CustomGesture(isEnabled: true) { recognizer in
                let translation = recognizer.translation(in: recognizer.view).y
                let state = recognizer.state

                // MARK: - Gesture Began

                if state == .began {
                    dragState = .dragging
                }

                // MARK: - Gesture Changed

                if state == .changed {
                }

                // MARK: - Gesture Ended

                if state == .ended {
                }
            }
        )
        .onAppear {
            DispatchQueue.main.async {
                topOffset = -offset
                bottomOffset = -offset
            }
        }
    }
}

since SwiftUI gestures don’t work simultaneously with scrollView


struct CustomGesture: UIGestureRecognizerRepresentable {
    var isEnabled: Bool
    var handle: (UIPanGestureRecognizer) -> ()

    func makeCoordinator(converter: CoordinateSpaceConverter) -> Coordinator {
        Coordinator()
    }

    func makeUIGestureRecognizer(context: Context) -> UIPanGestureRecognizer {
        let gesture = UIPanGestureRecognizer()
        gesture.delegate = context.coordinator
        return gesture
    }

    func updateUIGestureRecognizer(_ recognizer: UIPanGestureRecognizer, context: Context) {
        recognizer.isEnabled = isEnabled
    }

    func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) {
        handle(recognizer)
    }

    class Coordinator: NSObject, UIGestureRecognizerDelegate {
        func gestureRecognizer(
            _ gestureRecognizer: UIGestureRecognizer,
            shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
        ) -> Bool {
            return true
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật