Say you have a FaceView
which is some sort of image. You're going to have many of them on screen (or, in a collection view, table, stack view or other list).
In the class FaceView
you will need a variable "index"
class FaceView: UIView {
var index: Int
so that each FaceView can be self-aware of "which" face it is on screen.
So you must add var index: Int
to the class in question.
So you are adding many FaceView to your screen ...
let f = FaceView()
f.index = 73
.. you add f to your stack view, screen, or whatever.
You now add a click to f
f.addGestureRecognizer(UITapGestureRecognizer(target: self,
action: #selector(tapOneOfTheFaces)))
@objc func tapOneOfTheFaces(_ sender: UITapGestureRecognizer) {
if let tapped = sender.view as? CirclePerson {
print("we got it: \(tapped.index)")
You now know "which" face was clicked in your table, screen, stack view or whatever.
It's that easy.