In my viewController I have programmatically created several squares as sub-views.
Imagine it as a chessboard.
I would like to implement a method that executes some actions upon the sub-view (e.g., change the background color of a single square) when it gets touched by the user.
I have unsuccessfully tried with the UITapGestureRecognizer – I have read from Apple documentation that it applies only to one view at a time.
Do you have any suggestions?
Many thanks
2
Answers
Do it like this
or you can use this even in a extension of UIView if you’re going to use it in multiple types of UIViews.
And then use it like:
Notice that [weak self] is going to avoid getting a strong reference for closure to avoid a retain cycle there.
You can subclass UIWindow class and handle all the events in
- (void)sendEvent:(UIEvent *)event
there like sending notifications to all the views or communicate with some other way. Check the following repository for more info:https://github.com/mwinoto/TapDetectingWindow