微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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上有很多例子,看看,祝你好运.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐