Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would you consider using "Introspect" to further fix the glitters of #40 ? #53

Closed
VansonLeung opened this issue Mar 1, 2022 · 1 comment

Comments

@VansonLeung
Copy link

VansonLeung commented Mar 1, 2022

I completely removed all scrolling / scroll-bouncing glitters by using Introspect:

Prev. issue: #40

//
//  BSScrollView.swift
//
//  Created by Lucas Zischka.
//  Copyright © 2021-2022 Lucas Zischka. All rights reserved.
//

import UIKit
import SwiftUI
import Introspect


class BSScrollViewDelegate: NSObject, UIScrollViewDelegate
{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y == 0.0 {
            scrollView.bounces = false
        } else {
            scrollView.bounces = true
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset.y)
        if scrollView.contentOffset.y > 0.0 {
            scrollView.bounces = true
        }
    }
}


internal struct BSScrollView<Content: View>: View {
    
    @Binding private var isScrollEnabled: Bool
    
    private let scrollViewDelegate = BSScrollViewDelegate()
    private let defaultAxes: Axis.Set
    private let showsIndicators: Bool
    private let onChanged: (DragGestureView.Value) -> Void
    private let onEnded: (DragGestureView.Value) -> Void
    private let content: Content
    
    private var axes: Axis.Set {
        return self.isScrollEnabled ? self.defaultAxes : []
    }
    
    
    var body: some View {
        ScrollView(self.axes, showsIndicators: self.showsIndicators) {
            self.content
                .dragGesture(onChanged: self.onChanged, onEnded: self.onEnded)
        }
        .introspectScrollView { scrollView in
            scrollView.delegate = scrollViewDelegate
            scrollView.bounces = false
        }
    }
    
    
    init(axes: Axis.Set = .vertical, showsIndicators: Bool = true, isScrollEnabled: Binding<Bool> = .constant(true), onChanged: @escaping (DragGestureView.Value) -> Void = { _ in}, onEnded: @escaping (DragGestureView.Value) -> Void = { _ in}, @ViewBuilder content: () -> Content) {
        self._isScrollEnabled = isScrollEnabled
        
        self.defaultAxes = axes
        self.showsIndicators = showsIndicators
        self.onChanged = onChanged
        self.onEnded = onEnded
        self.content = content()
    }
}

struct BSScrollView_Previews: PreviewProvider {
    static var previews: some View {
        BSScrollView {
            ForEach(0...100, id: \.self) { i in
                Text("\(i)")
            }
        }
    }
}
@lucaszischka
Copy link
Owner

Thank you, I will test it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants