如何快速检测哪个图像被点击

3 uiimageview ios uitapgesturerecognizer swift

我在 ViewController 上创建了 6 个 UIImageView,稍后我将向所有这些视图添加 TapGestureRecognizer。

我想让它根据单击的图像,另一个 ViewController 将打开并显示某些信息。

为此,我需要知道单击了哪个图像。我该如何在 Swift 中做到这一点?

Arb*_*tur 6

UIGestureRecognizer 有属性“view”,该属性是您将其添加到的视图。对于这个例子,imageView.

func tap(gesture: UIGestureRecognizer) {
    println(gesture.view!.tag) // You can check for their tag and do different things based on tag
}

let img = UIImageView()
img.userInteraction = true
img.tag = 0
img.addGestureRecognizer(UITapGestureRecognizer(self, action: "tap:"))
Run Code Online (Sandbox Code Playgroud)