我有一个UIView,并且我已添加了点击手势:
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)
我试图在testfile中以编程方式调用它.
sendActionForEvent
我正在使用此功能,但它无法正常工作:
myView.sendActionForEvent(UIEvents.touchUpDown)
它显示发送到实例的无法识别的选择器.
我怎么解决这个问题?
提前致谢
1> Salavat Khan..:
您需要UITapGestureRecognizer
使用目标和操作进行初始化,如下所示:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
然后,您应该实现处理程序,每次发生tap事件时都会调用该处理程序:
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
// handling code
}
因此,现在调用您的tap手势识别器事件处理程序就像调用方法一样简单:
handleTap()
因为您已经设置了目标和操作,所以不需要"tap.delegate = self"
在Swift3中引发错误,你的手柄点击功能应该是:func handleTap(_ sender:UITapGestureRecognizer)
@muhasturk不要改变upvoted答案的含义.您的编辑使旧Swift版本的答案无效.如果您想发布Swift 2.2的更新,请发布您自己的答案.谢谢.
2> John Lim..:
对于任何正在寻找Swift 3解决方案的人
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(tap)
view.isUserInteractiOnEnabled= true
self.view.addSubview(view)
// function which is triggered when handleTap is called
func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
3> RNHTTR..:
对于Swift 4:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(tap)
view.isUserInteractiOnEnabled= true
self.view.addSubview(view)
// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
在Swift 4中,你需要明确指出触发函数可以从Objective-C调用,所以你需要添加@objc你的handleTap函数.
请参阅@Ali Beadle的回答:Swift 4添加手势:覆盖vs @objc
4> 小智..:
只需注意 - 不要忘记在视图上启用交互:
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tap)
// view.userInteractiOnEnabled= true
self.view.addSubview(view)
5> Kirit Modi..:
步骤1
@IBOutlet var viewTap: UIView!
第2步
var tapGesture = UITapGestureRecognizer()
STEP:3
override func viewDidLoad() {
super.viewDidLoad()
// TAP Gesture
tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
viewTap.addGestureRecognizer(tapGesture)
viewTap.isUserInteractiOnEnabled= true
}
第4步
func myviewTapped(_ sender: UITapGestureRecognizer) {
if self.viewTap.backgroundColor == UIColor.yellow {
self.viewTap.backgroundColor = UIColor.green
}else{
self.viewTap.backgroundColor = UIColor.yellow
}
}
OUTPUT
6> Museer Ahama..:
实现轻击手势
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "touchHappen")
view.userInteractiOnEnabled= true
view.addGestureRecognizer(tap)
识别出水龙头时调用此功能.
func touchHappen() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
self.view.endEditing(true)
}
For Swift 3的更新
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchHappen(_:)))
view.addGestureRecognizer(tap)
view.userInteractiOnEnabled= true
func touchHappen(_ sender: UITapGestureRecognizer) {
print("Hello Museer")
}
7> Giang..:
斯威夫特4
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchTapped(_:)))
self.view.addGestureRecognizer(tap)
@objc func touchTapped(_ sender: UITapGestureRecognizer) {
}
8> 小智..:
这是它在Swift 3中的工作原理:
@IBOutlet var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action:#selector(handleTap))
myView.addGestureRecognizer(tap)
}
func handleTap() {
print("tapped")
}
9> A.G..:
我在swift上研究了Xcode 6.4.见下文.
var view1: UIView!
func assignTapToView1() {
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap"))
// tap.delegate = self
view1.addGestureRecognizer(tap)
self.view .addSubview(view1)
...
}
func handleTap() {
print("tap working")
view1.removeFromSuperview()
// view1.alpha = 0.1
}
10> 小智..:
Swift 4的完整答案
步骤1:为视图创建出口
@IBOutlet weak var rightViewOutlet: UIView!
步骤2:定义轻击手势
var tapGesture = UITapGestureRecognizer()
步骤3:创建ObjC函数(在点击视图时调用)
@objc func rightViewTapped(_ recognizer: UIGestureRecognizer) {
print("Right button is tapped")
}
步骤4:在viewDidLoad()中添加以下内容
let rightTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.rightViewTapped(_:)))
rightViewOutlet.addGestureRecognizer(rightTap)
11> 小智..:
您需要使用目标和操作初始化UITapGestureRecognizer,如下所示:
let tap = UITapGestureRecognizer(target: self, action: "handleTap:")
tap.delegate = self
myView.addGestureRecognizer(tap)
然后,您应该实现处理程序,每次发生tap事件时都会调用该处理程序:
func handleTap(sender: UITapGestureRecognizer) {
// handling code
}
12> Iyyappan Rav..:
如果要在下面给出目标C代码,
UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; // Declare the Gesture.
gesRecognizer.delegate = self;
[yourView addGestureRecognizer:gesRecognizer]; // Add Gesture to your view.
// Declare the Gesture Recognizer handler method.
- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(@"Tapped");
}
或者您想在下面给出快速代码,
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Add tap gesture recognizer to view
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
myView.addGestureRecognizer(tapGesture)
}
// this method is called when a tap is recognized
func handleTap(sender: UITapGestureRecognizer) {
print("tap")
}
}