Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
If you’ve ever used an iOS device, then, whether you know it or not, you’ve used tap gesture recognition on many occasions. like double tapping functionality, add action to non-clickable view objects etc. All of these actions are done by tap gesture recognizers.
The worst part about adding a UITapGestureRecognizer or any sort of gesture recognizer/target action is implementing a new function solely for the selector parameter. For this, I’d like to share a neat trick I threw together that lets you add gesture recognizers without selectors.
UITapGestureRecognizer
Let’s say we have anUIImageView in our View Controller and we want to add a UITapGestureRecognizer to it that prints out a statement whenever it’s tapped.
UIImageView
Normally we would create an instance of a UITapGestureRecognizer and set a target to the View Controller and its selector as some function we quickly threw together (myImageViewTapped(sender: UITapGestureRecognizer)). This can get a bit redundant and can lead to messy code with functions for every subview you want to add interactivity too.
myImageViewTapped(sender: UITapGestureRecognizer)
I got to thinking that I could just make a quick extension that adds tap gesture recognizers to my image view for me, but then I’d have to make a new function for each recognizer, right? Wrong! Using the power of associated objects, we can actually store closures as computed properties in our extension!
Thank You!!!
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.