我在ViewController上创建了6个UIImageViews,稍后我将为所有这些添加TapGestureRecognizers。
我想这样做,以便根据点击的图像,另一个ViewController将打开并显示某些信息。
为此,我需要知道点击了哪个图像。我如何在Swift中做到这一点?
答案 0 :(得分:3)
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:"))