ios – 使用UITapGestureRecognizer传递参数

有什么办法可以用UITapGestureRecognizer传递参数吗?
我已经看到这回答了 Objective-c但是找不到swift的答案
test.userInteractionEnabled = true
let tapRecognizer = UITapGestureRecognizer(target: self,action: Selector("imageTapped4:"))
// Something like text.myParamater
test.addGestureRecognizer(tapRecognizer)

然后在func imageTapped4(){}下接收myParameter

解决方法

一种方法是将UITapGestureRecognizer子类化,然后设置属性,我在下面发布了一个例子.您还可以检查发件人并检查是否等于某个标签,类,字符串,e.t.c
class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var image: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        image.userInteractionEnabled = true;
        let tappy = MyTapGesture(target: self,action: #selector(self.tapped(_:)))
        image.addGestureRecognizer(tappy)
        tappy.title = "val"
    }

    func tapped(sender : MyTapGesture) {
        print(sender.title)
        label1.text = sender.title
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }
}

class MyTapGesture: UITapGestureRecognizer {
    var title = String()
}

SO上有很多例子,看看,祝你好运.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...