SwiftUI: Scroll to Hide Tab Bar - iOS 16 & 17 - Xcode 15

Поділитися
Вставка
  • Опубліковано 11 жов 2024

КОМЕНТАРІ • 12

  • @matthew_rodriguez
    @matthew_rodriguez Рік тому +4

    I think you can avoid using pan gesture and use only a scroll position observer to check if is going down or up. Thanks for your amazing work

    • @Y-raise
      @Y-raise Рік тому

      Which one is better?

    • @Kavsoft
      @Kavsoft  Рік тому +5

      Using the scroll position, we can do this by comparing previous and current positions, but I wanted to make it simple as the drag velocity instantly changes when the direction of the swipe changes, and there is no need to have previous values as well. Also, in SwiftUI, there is no way to directly read scroll positions, and we can use Preference keys, but use of those inside Lazy Stacks results in unpredicted offsets since the layout changes when the content goes outside the visible region.

  • @yevhen_san
    @yevhen_san 10 місяців тому

    Thank you! I'm able to follow your video and it works but man how you achieve this knowledge originally? Bravo!

  • @andrejkling3886
    @andrejkling3886 Рік тому +1

    Excellent improvement 👋

  • @user32352
    @user32352 Рік тому +1

    can you hide the nav bar with this same set up? similar to new twitter

  • @ChanchalRajKhatri
    @ChanchalRajKhatri 11 місяців тому

    Possible to do the same in UIKit?

  • @zardashtjaza1343
    @zardashtjaza1343 5 місяців тому

    the performance of the ScrollView is really bad when I add the panGesture !

    • @Kavsoft
      @Kavsoft  5 місяців тому +2

      Hi, I just noticed that the UUID used on the gesture ID is updating when scrolling, which in terms adds multiple gestures to the view, so simply change the UUID().uuidString to “CUSTOMGESTRUE”.

  • @CaganDURGUN
    @CaganDURGUN Рік тому

    hi

  • @traizoo
    @traizoo Рік тому

    Brooo, i've been waiting for this

  • @TolyanFikalis
    @TolyanFikalis Рік тому +2

    struct ProductsScrollDirectionCustomGesture: UIViewRepresentable {
    // MARK: - Nested Types
    typealias GestureHandler = (UIPanGestureRecognizer) -> Void
    class Coordinator: NSObject, UIGestureRecognizerDelegate {
    // MARK: - Properties
    var onChange: GestureHandler
    // MARK: - Init
    init(onChange: @escaping GestureHandler) {
    self.onChange = onChange
    }
    // MARK: - Functions
    @objc
    func gestureChange(gesture: UIPanGestureRecognizer) {
    onChange(gesture)
    }
    func gestureRecognizer(
    _ gestureRecognizer: UIGestureRecognizer,
    shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
    ) -> Bool {
    true
    }
    }
    // MARK: - Properties
    var onChange: GestureHandler
    private let gestureId = UUID().uuidString
    // MARK: - Functions
    func makeCoordinator() -> Coordinator {
    Coordinator(onChange: onChange)
    }
    func makeUIView(context: Context) -> some UIView {
    UIView()
    }
    func updateUIView(_ uiView: UIViewType, context: Context) {
    DispatchQueue.main.async {
    guard
    let superview = uiView.superview?.superview,
    !(superview.gestureRecognizers?.contains(where: { $0.name == gestureId }) ?? false) else { return }
    let gesture = UIPanGestureRecognizer(
    target: context.coordinator,
    action: #selector(context.coordinator.gestureChange(gesture:))
    )
    gesture.name = gestureId
    gesture.delegate = context.coordinator
    superview.addGestureRecognizer(gesture)
    }
    }
    }