|
| 1 | +import SwiftUI |
| 2 | +import SwiftUIIntrospect |
| 3 | + |
| 4 | +private final class TabBarDelegate: NSObject, UITabBarControllerDelegate { |
| 5 | + var onClick: ((_ index: Int) -> Void)? = nil |
| 6 | + |
| 7 | + func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { |
| 8 | + if let index = tabBarController.viewControllers?.firstIndex(of: viewController) { |
| 9 | + onClick?(index) |
| 10 | + } |
| 11 | + return false |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +struct TabItemEventModifier: ViewModifier { |
| 16 | + let onTabEvent: (_ key: Int, _ isLongPress: Bool) -> Void |
| 17 | + private let delegate = TabBarDelegate() |
| 18 | + |
| 19 | + func body(content: Content) -> some View { |
| 20 | + content |
| 21 | + .introspect(.tabView, on: .iOS(.v14, .v15, .v16, .v17, .v18)) { tabController in |
| 22 | + handle(tabController: tabController) |
| 23 | + } |
| 24 | + .introspect(.tabView, on: .tvOS(.v14, .v15, .v16, .v17, .v18)) { tabController in |
| 25 | + handle(tabController: tabController) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + func handle(tabController: UITabBarController) { |
| 30 | + delegate.onClick = { index in |
| 31 | + onTabEvent(index, false) |
| 32 | + } |
| 33 | + tabController.delegate = delegate |
| 34 | + |
| 35 | + // Don't register gesutre recognizer more than one time |
| 36 | + if objc_getAssociatedObject(tabController.tabBar, &AssociatedKeys.gestureHandler) != nil { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // Remove existing long press gestures |
| 41 | + if let existingGestures = tabController.tabBar.gestureRecognizers { |
| 42 | + for gesture in existingGestures where gesture is UILongPressGestureRecognizer { |
| 43 | + tabController.tabBar.removeGestureRecognizer(gesture) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Create gesture handler |
| 48 | + let handler = LongPressGestureHandler(tabBar: tabController.tabBar, handler: onTabEvent) |
| 49 | + let gesture = UILongPressGestureRecognizer(target: handler, action: #selector(LongPressGestureHandler.handleLongPress(_:))) |
| 50 | + gesture.minimumPressDuration = 0.5 |
| 51 | + |
| 52 | + objc_setAssociatedObject(tabController.tabBar, &AssociatedKeys.gestureHandler, handler, .OBJC_ASSOCIATION_RETAIN) |
| 53 | + |
| 54 | + tabController.tabBar.addGestureRecognizer(gesture) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +private struct AssociatedKeys { |
| 59 | + static var gestureHandler: UInt8 = 0 |
| 60 | +} |
| 61 | + |
| 62 | +private class LongPressGestureHandler: NSObject { |
| 63 | + private weak var tabBar: UITabBar? |
| 64 | + private let handler: (Int, Bool) -> Void |
| 65 | + |
| 66 | + init(tabBar: UITabBar, handler: @escaping (Int, Bool) -> Void) { |
| 67 | + self.tabBar = tabBar |
| 68 | + self.handler = handler |
| 69 | + super.init() |
| 70 | + } |
| 71 | + |
| 72 | + @objc func handleLongPress(_ recognizer: UILongPressGestureRecognizer) { |
| 73 | + guard recognizer.state == .began, |
| 74 | + let tabBar = tabBar else { return } |
| 75 | + |
| 76 | + let location = recognizer.location(in: tabBar) |
| 77 | + |
| 78 | + // Get buttons and sort them by frames |
| 79 | + let tabBarButtons = tabBar.subviews.filter { String(describing: type(of: $0)).contains("UITabBarButton") }.sorted(by: { $0.frame.minX < $1.frame.minX }) |
| 80 | + |
| 81 | + for (index, button) in tabBarButtons.enumerated() { |
| 82 | + if button.frame.contains(location) { |
| 83 | + handler(index, true) |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + deinit { |
| 90 | + if let tabBar { |
| 91 | + objc_setAssociatedObject(tabBar, &AssociatedKeys.gestureHandler, nil, .OBJC_ASSOCIATION_RETAIN) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +extension View { |
| 97 | + func onTabItemEvent(_ handler: @escaping (Int, Bool) -> Void) -> some View { |
| 98 | + modifier(TabItemEventModifier(onTabEvent: handler)) |
| 99 | + } |
| 100 | +} |
0 commit comments