【问题标题】:UISwipeGestureRecognizer causes jerkinessUISwipeGestureRecognizer 导致抖动
【发布时间】:2019-08-19 10:22:11
【问题描述】:

我在UITabBarController 中使用UISwipeGestureRecogniser:

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.selectedIndex = Values.menuSelectedIndex
        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
        leftSwipe.direction = .left
        rightSwipe.direction = .right
        view.addGestureRecognizer(leftSwipe)
        view.addGestureRecognizer(rightSwipe)
    }

    @objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
        /*if let topController = UIApplication.topViewController() {
            if (topController is HomeVC) {
                if (sender.direction == .left) {
                    self.selectedIndex += 1
                }
                else if (sender.direction == .right) {
                    self.selectedIndex -= 1
                }
            }
        }*/
    }
}

当 topController 不是 HomeVC 时,滑动手势应该什么都不做。不幸的是,它在左右滚动时会导致抖动。

编辑

UIApplication.topViewController() 是获取当前UIViewController的扩展:

extension UIApplication {
    class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }
}

【问题讨论】:

  • 什么是UIApplication.topViewController()。它是UIApplication 扩展中的静态函数吗?你能也展示一下它的实现吗?
  • @TalCohen 我在if (topController is HomeVC) 中放置了一个断点,它没有被调用。如果我注释掉handleSwipes 中的所有代码,抖动问题仍然存在。
  • 手势之间可能存在冲突。您可以实现手势委托方法gestureRecognizer:shouldRecognizeSimultaniouslyWith:otherGesture 并返回true。看看这篇文章 -> developer.apple.com/documentation/uikit/…

标签: swift xcode swipe gesture swift5


【解决方案1】:

gestureRecognizer:shouldRecognizeSimultaniouslyWith:otherGesture 对我不起作用,因为我使用的是 NMAMapViewDelegate 和 NMAMapGestureDelegate。

只要在不应调用 handleSwipes 的 UIViewController 上移除手势,我就可以完成这项工作。

在TabBarController我加了:

lazy var leftSwipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
lazy var rightSwipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))

public func addGestures() {
    view.addGestureRecognizer(leftSwipe)
    view.addGestureRecognizer(rightSwipe)
}

public func removeGestures() {
    view.removeGestureRecognizer(leftSwipe)
    view.removeGestureRecognizer(rightSwipe)
}

以及在任何不应调用handleSwipes 的 UIViewControllers 中:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    (navigationController?.tabBarController as? TabBarController)?.removeGestures()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    (navigationController?.tabBarController as? TabBarController)?.addGestures()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2021-06-22
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多