如何在swift中以编程方式调用手势点击UIView [英] How to call gesture tap on UIView programmatically in swift
本文介绍了如何在swift中以编程方式调用手势点击UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个UIView并且我已添加了点击手势:
I have a UIView and and I have added tap gesture to it:
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)
我试图在testfile中以编程方式调用它。
I am trying to call it programmatically in the testfile.
sendActionForEvent
我正在使用此功能,但它不起作用:
I am using this function, but it is not working:
myView.sendActionForEvent(UIEvents.touchUpDown)
它显示无法识别的选择器发送到实例。
It shows unrecognised selector sent to instance.
如何解决此问题?
提前致谢
推荐答案
您需要使用目标和操作初始化 UITapGestureRecognizer
,如下所示:
You need to initialize UITapGestureRecognizer
with a target and action, like so:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
tap.delegate = self // This is not required
myView.addGestureRecognizer(tap)
然后,你应该实现处理程序,每次执行时都会调用点击事件发生:
Then, you should implement the handler, which will be called each time when a tap event occurs:
func handleTap(sender: UITapGestureRecognizer? = nil) {
// handling code
}
所以现在打电话给你点击手势识别器事件处理程序就像调用方法一样简单:
So now calling your tap gesture recognizer event handler is as easy as calling a method:
handleTap()
这篇关于如何在swift中以编程方式调用手势点击UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文