ios - 使用 UITapGestureRecognizer 传递参数

标签 ios iphone xcode swift2 xcode7

有什么方法可以用 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 然后设置一个属性,我在下面发布了一个示例。您还可以对发件人进行一些检查,并检查是否等于某些标签、类、字符串等

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
    }
}

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

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

关于ios - 使用 UITapGestureRecognizer 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38445262/

相关文章:

swift - 协助 Swift 5 WebKit 并在 ViewController 中从 HTML 打开电话和电子邮件链接

objective-c - 如何在 View Controller 中获取对 UITableView 的引用?

iphone - 委托(delegate)不工作(与 Restkit 相关?)

ios - 使用 sendResourceAtURL 给出 "Unsupported resource type"

iphone - WebView :shouldStartLoadWithRequest:navigationType: not getting called on webkit with youtube video link iphone

xcode - ios4 - 单击按钮加载新 View

iphone - 如何在iOS SDK中获取崩溃日志

javascript - 滚动在 iPad 上卡住(javascript)

ios - 在决定要开发的 iOS 版本时应考虑哪些因素?

iOS 可设计按钮背景颜色不起作用