Swift3 iOS - 如何使 UITapGestureRecognizer 触发功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38829151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift3 iOS - How to make UITapGestureRecognizer trigger function
提问by iSebbeYT
I am trying to add a UITapGesture
to a UIButton so it will trigger a function when tapped. I am using Swift 3
and is getting some error:
我正在尝试将 a 添加UITapGesture
到 UIButton 以便在点击时触发一个功能。我正在使用Swift 3
并且出现一些错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwiftRunner.ViewController tapBlurButton]: unrecognized selector sent to instance 0x149e07610'
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[SwiftRunner.ViewController tapBlurButton]:无法识别的选择器发送到实例 0x149e07610”
This is roughly what I have:
这大致是我所拥有的:
// Swift 3
import UIKit
class ViewController {
@IBOutlet weak var qsBlurButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("tapBlurButton")))
qsBlurButton.addGestureRecognizer(tapGesture)
}
func tapBlurButton(sender: UITapGestureRecognizer) {
print("Please Help!")
}
}
回答by Nirav D
From your code you are using swift 3.0 so change your selector
syntax like this
从您的代码中,您使用的是 swift 3.0,因此请selector
像这样更改您的语法
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
and Your function like this
你的功能是这样的
func tapBlurButton(_ sender: UITapGestureRecognizer) {
print("Please Help!")
}
Edit:
编辑:
Not idea that you are using button with tap gesture, instead of that use inbuilt method addTarget
for button no need to create tap gesture for it like this
不知道您正在使用带有点击手势的按钮,而不是使用按钮的内置方法addTarget
,无需像这样为其创建点击手势
qsBlurButton.addTarget(self, action: #selector(self.tapBlurButton(_:)), forControlEvents: .TouchUpInside)
func tapBlurButton(_ sender: UIButton) {
print("Please Help!")
}
回答by skymook
Swift 3
斯威夫特 3
In the case where the func
for the selector looks like this (note: it doesn't have a _):
在func
for 选择器看起来像这样的情况下(注意:它没有 _):
func tapBlurButton(sender: UITapGestureRecognizer) {
print("Please Help!")
}
Then the selector looks like this:
然后选择器看起来像这样:
#selector(self.tapBlurButton(sender:))
So the final code for the selector is this:
所以选择器的最终代码是这样的:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(sender:)))
If you do not specify that the first parameter has _
, then you need to use the full name of the first parameter.
如果没有指定第一个参数有_
,那么就需要使用第一个参数的全名。
回答by Pochi
To add a gesture recognizer you have to create one indicating which function it will call:
要添加手势识别器,您必须创建一个指示它将调用哪个函数的手势识别器:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapFrom(recognizer:)))
Then add it to the element you need. (You are using a button, and as others have explained buttons have their native 'addTarget' methods)
然后将其添加到您需要的元素中。(您正在使用按钮,并且正如其他人所解释的那样,按钮具有其原生的“addTarget”方法)
For explanation purposes imagine you want to add it to an UIView:
出于解释目的,假设您想将其添加到 UIView:
self.someView.addGestureRecognizer(tapGestureRecognizer)
And don't forget that some elements are "not user interactive" by default so you might have to change that property too:
并且不要忘记某些元素默认情况下是“非用户交互的”,因此您可能也必须更改该属性:
self.someView.isUserInteractionEnabled = true
In Swift 4the function needs the @objc declaration:
在Swift 4 中,该函数需要 @objc 声明:
@objc func handleTapFrom(recognizer : UITapGestureRecognizer)
{
// Some code...
}