有什么办法可以用UITapGestureRecognizer传递参数吗?
我已经看到这回答了 Objective-c但是找不到swift的答案
我已经看到这回答了 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上有很多例子,看看,祝你好运.