gpt4 book ai didi

swift - UISwipeGestureRecognizer 导致抖动

转载 作者:行者123 更新时间:2023-11-30 10:37:47 30 4
gpt4 key购买 nike

我在我的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
}
}

最佳答案

gestureRecognizer:shouldRecognizeSimultaniouslyWith:otherGesture 对我不起作用,因为我正在使用 NMAMapViewDelegateNMAMapGestureDelegate

我只需在不应调用 handleSwipesUIViewController 上删除手势即可实现此目的。

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的 UIViewController 中:

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()
}

关于swift - UISwipeGestureRecognizer 导致抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57554817/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com