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

Поделиться
HTML-код
  • Опубликовано: 15 сен 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.

  • @andrejkling3886
    @andrejkling3886 Год назад +1

    Excellent improvement 👋

  • @yevhen_san
    @yevhen_san 9 месяцев назад

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

  • @user32352
    @user32352 Год назад +1

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

  • @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”.

  • @ChanchalRajKhatri
    @ChanchalRajKhatri 10 месяцев назад

    Possible to do the same in UIKit?

  • @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)
    }
    }
    }