如何在swift中以编程方式调用UIView的手势点击
我有一个UIView和我已经添加了点击手势:
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) tap.delegate = self myView.addGesture(tap)
我想在testing文件中以编程方式调用它。
sendActionForEvent
我正在使用这个function,但它不工作:
myView.sendActionForEvent(UIEvents.touchUpDown)
它显示发送给实例的无法识别的select器。
我怎么解决这个问题?
提前致谢
你需要用目标和动作来初始化UITapGestureRecognizer
,如下所示:
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) tap.delegate = self myView.addGestureRecognizer(tap)
然后,您应该实现处理程序,每次发生轻击事件时都会调用该处理程序:
func handleTap(sender: UITapGestureRecognizer? = nil) { // handling code }
所以现在调用你的轻敲手势识别器事件处理程序就像调用方法一样简单:
handleTap()
对于任何正在寻找Swift 3解决scheme的人
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") }
请注意 – 不要忘记在视图上启用交互:
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap)) view.addGestureRecognizer(tap) // view.userInteractionEnabled = true self.view.addSubview(view)
这是它在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") }
SWIFT 3.0 UITapGesture
步骤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
我在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 }
实现点击手势
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "touchHappen") view.userInteractionEnabled = true view.addGestureRecognizer(tap)
点击被识别时调用此function。
func touchHappen() { //Causes the view (or one of its embedded text fields) to resign the first responder status. self.view.endEditing(true) }
更新为Swift 3
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchHappen(_:))) view.addGestureRecognizer(tap) view.userInteractionEnabled = true func touchHappen(_ sender: UITapGestureRecognizer) { print("Hello Museer") }
对于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调用,因此您还需要在您的handleTap函数中添加@objc。
见@Ali Beadle的答案在这里: Swift 4添加手势:覆盖vs @objc
let tap = UITapGestureRecognizer(target: self, action: Selector("handleFrontTap:")) frontView.addGestureRecognizer(tap) //Make sure this must not be private func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) { print("tap working") }
你需要用目标和动作来初始化UITapGestureRecognizer,如下所示:
let tap = UITapGestureRecognizer(target: self, action: "handleTap:") tap.delegate = self myView.addGestureRecognizer(tap)
然后,您应该实现处理程序,每次发生轻击事件时都会调用该处理程序:
func handleTap(sender: UITapGestureRecognizer) { // handling code }
如果你想要Objective 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"); }
或者你想在下面给出swift代码,
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") } }
尝试下面的swift代码(在Xcode 6.3.1中testing):
import UIKit class KEUITapGesture150427 : UIViewController { var _myTap: UITapGestureRecognizer? var _myView: UIView? override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.whiteColor(); _myTap = UITapGestureRecognizer(target: self , action: Selector("_myHandleTap:")) _myTap!.numberOfTapsRequired = 1 _myView = UIView(frame: CGRectMake(100, 200, 100, 100)) _myView!.backgroundColor=UIColor.blueColor() _myView!.layer.cornerRadius = 20 _myView!.layer.borderWidth = 1 _myView!.addGestureRecognizer(_myTap!) view.addSubview(_myView!) } func _myHandleTap(sender: UITapGestureRecognizer) { if sender.state == .Ended { println("_myHandleTap(sender.state == .Ended)") sender.view!.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0); } } }
请注意,您的目标可能是UIResponder的任何子类,请参阅(在Xcode 6.3.1中testing):
import UIKit class MyTapTarget : UIResponder { func _myHandleTap2(sender: UITapGestureRecognizer) { if sender.state == .Ended { println("_myHandleTap2(sender.state == .Ended)") sender.view!.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0); } } } class KEUITapGesture150427b : UIViewController { var _myTap: UITapGestureRecognizer? var _myView: UIView? var _myTapTarget: MyTapTarget? override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.whiteColor(); _myTapTarget = MyTapTarget() _myTap = UITapGestureRecognizer(target: _myTapTarget! , action: Selector("_myHandleTap2:")) _myTap!.numberOfTapsRequired = 1 _myView = UIView(frame: CGRectMake(100, 200, 100, 100)) _myView!.backgroundColor=UIColor.blueColor() _myView!.layer.cornerRadius = 20 _myView!.layer.borderWidth = 1 _myView!.addGestureRecognizer(_myTap!) view.addSubview(_myView!) } }
我在Swift 2.2上编译了Xcode 7.3.1。 见下文。
func addTapGesture() { let tap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.handleTap)) tap.numberOfTapsRequired = 1 self.myView.addGestureRecognizer(tap) } func handleTap() { // Your code here... }
而不是调用myView的UITapGestureRecognizer,你可以直接调用handleTap
函数,