How to detect which image has been tapped in swift

岁酱吖の 提交于 2019-12-01 07:24:42

问题


I have created 6 UIImageViews on a ViewController, and I am later going to add TapGestureRecognizers to all of them.

I want to make it so that depending on what image has been clicked, another ViewController will open and display certain information.

For this to happen, I need to know which image has been clicked. How would I do this in Swift?


回答1:


UIGestureRecognizer has property 'view' this property is the view you add it to. For this example the 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:"))


来源:https://stackoverflow.com/questions/30958745/how-to-detect-which-image-has-been-tapped-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!