UISwipeGestureRecognizer 导致抖动
UISwipeGestureRecognizer causes jerkiness
我在 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
对我不起作用,因为我使用的是 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()
}
我在 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
对我不起作用,因为我使用的是 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
:
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()
}