我需要在导航栏或视图上添加Tap Gesture。 我得到了以下解决方案,完全正常。
但是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)
}
或者
override func viewWillDisappear(animated: Bool)
{
print(navigationController!.view.gestureRecognizers!.count)
print(navigationController!.navigationBar.gestureRecognizers!.count)
}
当我尝试获取gestureRecognizers时,它说没有。然后添加手势在哪里?
var image = Convert.FromBase64String(base64);
string hex = BitConverter.ToString(image);
hex = hex.Replace("-", "");
hex = "0x" + hex;
答案 0 :(得分:2)
尝试使用此
将手势声明为
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")
}
}
}
更新了答案 - 手势计数打印为
控制台输出:
答案 1 :(得分:0)
在iOS Geek的帮助下,我发现,在ViewdDidLoad中,gestureRecognizers!.count是2,但在viewWillDisappear中是nil。
然后我挖了更多,发现我已经为我的后退按钮编写了自定义代码。
所以在这种情况下,我们应该在popToViewController
之前删除GestureRecognizer因此,对于我希望在使用自定义后退按钮时不会像我一样犯错的所有人。
func backBarBtnFnc(sender: UIBarButtonItem)
{
navigationController?.navigationBar.removeGestureRecognizer(taskTodoOnBar)
// CodTdo ...
self.navigationController!.popToViewController(VC2, animated: true)
}