Quick way to add action to your UIView

Samrith Yoeun
1 min readJul 4, 2020

There is no mystery or super magical code to add the action to any UIView. We simply add the UITapRecognizer to the view and assign the Selector to the UIView.

However, one most annoying thing about adding Selector is that we have to think about naming the Selector.

To solve this problem, I have searched for a way that can convert a Closure to the Selector.

By going through this thread, I kinda find a way to do it. Below is an extension that you can use to archive the mentioned purpose.

and here is how you are going to use it:

YAY, finally you did it. I hope this can save you a lot of time, and you will use it a lot. I will also upload the full project to demonstrate how we do it here.

--

--