导航栏 removeGestureRecognizer 没有删除 Gesture
Navigation Bar removeGestureRecognizer is not removing the Guesture
我需要在导航栏或视图上添加点击手势。
我得到了以下解决方案,效果非常好。
但是 removeGestureRecognizer
并没有删除手势,而是破坏了其他视图控制器中其他后退按钮的功能。
如何解决这个问题?
var taskTodoOnBar : UITapGestureRecognizer!
override func viewWillAppear(animated: Bool)
{
navigationController?.view.addGestureRecognizer(taskTodoOnBar)
}
override func viewWillDisappear(animated: Bool)
{
navigationController?.view.removeGestureRecognizer(taskTodoOnBar)
}
或者
override func viewWillAppear(animated: Bool)
{
navigationController?.navigationBar.addGestureRecognizer(taskTodoOnBar)
}
override func viewWillDisappear(animated: Bool)
{
navigationController?.navigationBar.removeGestureRecognizer(taskTodoOnBar)
}
当我尝试获取 gestureRecognizers 计数时,它显示为零。那么手势添加到哪里呢?
override func viewWillDisappear(animated: Bool)
{
print(navigationController!.view.gestureRecognizers!.count)
print(navigationController!.navigationBar.gestureRecognizers!.count)
}
试试这个
声明手势为
let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer()
手势处理程序
@objc func tapHandler(handler: UITapGestureRecognizer){
print("gesture Added")
}
在导航栏中添加为
override func viewDidLoad()
{
super.viewDidLoad()
tapGesture.numberOfTapsRequired = 1
tapGesture.addTarget(self, action: #selector(VC2.tapHandler(handler:)))
self.navigationController?.view.addGestureRecognizer(tapGesture)
}
删除为
override func viewWillDisappear(_ animated: Bool) {
for gesture in (navigationController?.view.gestureRecognizers)! {
if gesture == tapGesture {
navigationController?.view.removeGestureRecognizer(tapGesture)
print("removed")
}
}
}
更新的答案 - 手势计数打印为零
控制台输出:
在 iOS Geek 的帮助下,我发现 gestureRecognizers!.count 在 ViewdDidLoad 中为 2,但在 viewWillDisappear 中为 nil。
然后我深入挖掘,发现我已经为后退按钮编写了自定义代码。
所以在这种情况下,我们应该在 popToViewController 之前移除 GestureRecognizer
所以这适用于所有我不希望在使用自定义后退按钮时像我一样犯错误的人。
func backBarBtnFnc(sender: UIBarButtonItem)
{
navigationController?.navigationBar.removeGestureRecognizer(taskTodoOnBar)
// CodTdo ...
self.navigationController!.popToViewController(VC2, animated: true)
}
我需要在导航栏或视图上添加点击手势。 我得到了以下解决方案,效果非常好。
但是 removeGestureRecognizer
并没有删除手势,而是破坏了其他视图控制器中其他后退按钮的功能。
如何解决这个问题?
var taskTodoOnBar : UITapGestureRecognizer!
override func viewWillAppear(animated: Bool)
{
navigationController?.view.addGestureRecognizer(taskTodoOnBar)
}
override func viewWillDisappear(animated: Bool)
{
navigationController?.view.removeGestureRecognizer(taskTodoOnBar)
}
或者
override func viewWillAppear(animated: Bool)
{
navigationController?.navigationBar.addGestureRecognizer(taskTodoOnBar)
}
override func viewWillDisappear(animated: Bool)
{
navigationController?.navigationBar.removeGestureRecognizer(taskTodoOnBar)
}
当我尝试获取 gestureRecognizers 计数时,它显示为零。那么手势添加到哪里呢?
override func viewWillDisappear(animated: Bool)
{
print(navigationController!.view.gestureRecognizers!.count)
print(navigationController!.navigationBar.gestureRecognizers!.count)
}
试试这个
声明手势为
let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer()
手势处理程序
@objc func tapHandler(handler: UITapGestureRecognizer){
print("gesture Added")
}
在导航栏中添加为
override func viewDidLoad()
{
super.viewDidLoad()
tapGesture.numberOfTapsRequired = 1
tapGesture.addTarget(self, action: #selector(VC2.tapHandler(handler:)))
self.navigationController?.view.addGestureRecognizer(tapGesture)
}
删除为
override func viewWillDisappear(_ animated: Bool) {
for gesture in (navigationController?.view.gestureRecognizers)! {
if gesture == tapGesture {
navigationController?.view.removeGestureRecognizer(tapGesture)
print("removed")
}
}
}
更新的答案 - 手势计数打印为零
控制台输出:
在 iOS Geek 的帮助下,我发现 gestureRecognizers!.count 在 ViewdDidLoad 中为 2,但在 viewWillDisappear 中为 nil。
然后我深入挖掘,发现我已经为后退按钮编写了自定义代码。
所以在这种情况下,我们应该在 popToViewController 之前移除 GestureRecognizer
所以这适用于所有我不希望在使用自定义后退按钮时像我一样犯错误的人。
func backBarBtnFnc(sender: UIBarButtonItem)
{
navigationController?.navigationBar.removeGestureRecognizer(taskTodoOnBar)
// CodTdo ...
self.navigationController!.popToViewController(VC2, animated: true)
}