UITapGestureRecognizer无法在自定义类(不是视图控制器)内部工作

时间:2020-11-27 19:36:37

I have a custom class Overlay where I added UIButton. When the button is clicked, a method should be called:

我有一个自定义类Overlay,我添加了UIButton。单击按钮时,应调用一个方法:

class Overlay {

func show(onView view: UIView, frame: CGRect) {
    let dismissButton = UIButton()
        dismissButton.frame = frame
        dismissButton.setTitle("Dismiss", for: .normal)
        dismissButton.setTitleColor(Project.Color.failure, for: .normal)
        dismissButton.titleLabel?.font = Project.Typography.lightFont.withSize(22)
        view.addSubview(dismissButton)

        dismissButton.isUserInteractionEnabled = true

        let tap = UITapGestureRecognizer(target: self, action: #selector(dismissBtnTapped(tap:)))
        dismissButton.addGestureRecognizer(tap)
}

@objc func dismissBtnTapped(tap: UITapGestureRecognizer) {
    print("TEST")
}

I call show(...) inside my ViewController, passing in its view and a frame.

我在我的ViewController中调用show(...),传入它的视图和框架。

But the tapGestrueRecognizer is not working. Any ideas?

但tapGestrueRecognizer无法正常工作。有任何想法吗?

Thank you.

谢谢。

Edit: I tried putting this code directly inside my ViewController. Then it works. I'm not sure why, though, and that's not a viable solution for me, unfortunately :/

编辑:我尝试将此代码直接放在我的ViewController中。然后它工作。不过,我不确定为什么,不幸的是,这对我来说不是一个可行的解决方案:/

Edit 2: That's how I call it:

编辑2:这就是我所说的:

let overlay = Overlay()
overlay.show(onView: self.view, frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 150))

3 个解决方案

#1


1  

You are already adding a button try adding a target to it instead of a gesture, and make your overlay variable global.

您已经添加了一个按钮,尝试向其添加目标而不是手势,并使您的叠加变量全局化。

class YourControllerClass: UIViewController {
let overlay = Overlay()
...
func show(onView: UIView, frame: CGRect) {
...
dismissButton.addTarget(self, action: #selector(dismissBtnTapped(_:)), forControlEvents: .TouchUpInside)
}

func dismissBtnTapped(sender:UIButton){

}
}

Hope this helps.

希望这可以帮助。

#2


1  

You not need to add tapgesturerecognizer on uibutton, you can directly add target on it something like,

您不需要在uibutton上添加tapgesturerecognizer,您可以直接在其上添加类似的目标,

   dismissButton.addTarget(self, action: #selector(dismissBtnTapped), forControlEvents: .TouchUpInside)

and remove parameter from dismissBtnTapped method!

并从dismissBtnTapped方法中删除参数!

#3


0  

Make sure your view which you are passing as onView in show(onView: UIView, frame: CGRect) method is enabled for user interactions.

确保为用户交互启用了您在onView(onView:UIView,frame:CGRect)方法中传递的视图。

#1


1  

You are already adding a button try adding a target to it instead of a gesture, and make your overlay variable global.

您已经添加了一个按钮,尝试向其添加目标而不是手势,并使您的叠加变量全局化。

class YourControllerClass: UIViewController {
let overlay = Overlay()
...
func show(onView: UIView, frame: CGRect) {
...
dismissButton.addTarget(self, action: #selector(dismissBtnTapped(_:)), forControlEvents: .TouchUpInside)
}

func dismissBtnTapped(sender:UIButton){

}
}

Hope this helps.

希望这可以帮助。

#2


1  

You not need to add tapgesturerecognizer on uibutton, you can directly add target on it something like,

您不需要在uibutton上添加tapgesturerecognizer,您可以直接在其上添加类似的目标,

   dismissButton.addTarget(self, action: #selector(dismissBtnTapped), forControlEvents: .TouchUpInside)

and remove parameter from dismissBtnTapped method!

并从dismissBtnTapped方法中删除参数!

#3


0  

Make sure your view which you are passing as onView in show(onView: UIView, frame: CGRect) method is enabled for user interactions.

确保为用户交互启用了您在onView(onView:UIView,frame:CGRect)方法中传递的视图。