UITapGestureRecognizer没有在Swift 3中触发 [英] UITapGestureRecognizer not firing in Swift 3

查看:106
本文介绍了UITapGestureRecognizer没有在Swift 3中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 UIViewController 中有以下代码:

I have the following code in a custom UIViewController:

let tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap))

func dismissTap() {

    print("Tap!")
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addGestureRecognizer(tapDismissTextField)
    self.friendsView.addGestureRecognizer(tapDismissTextField)
}

否点击!正在打印线条,因此手势识别器不会触发。可能出现什么问题?

No "Tap!" lines are being printed, so the gesture recogniser isn't firing. What could be going wrong?

在此控制器的所有视图中启用的用户交互 true

User Interaction Enabled is true accross all the views in this controller.

如果有任何帮助,这个视图控制器嵌入在 UINavigationController 中。

This view controller is embedded within a UINavigationController if that is of any help.

推荐答案

您需要分配一次你的看法被载入自

You need to assign self only once your view is loaded.

var tapDismissTextField: UITapGestureRecognizer!

并在你的viewDidLoad方法中:

and in your viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()
    tapDismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismissTap))
    view.addGestureRecognizer(tapDismissTextField)
}

这篇关于UITapGestureRecognizer没有在Swift 3中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆