SoFunction
Updated on 2025-03-02

How to add click gestures to controls

Today, a colleague asked me how to add click methods to the headerview of the tableview. Today, let me briefly explain. The method to add click gestures in swift is:

Copy the codeThe code is as follows:

(UITapGestureRecognizer(target: self, action: "imagePathClick:"))

How about it? Does it look much simpler than OC? Let me briefly explain that imagePath is a UIImageView I defined. Someone may have added this gesture and found out why clicking this control does not work? In fact, the reason is that you may not open user interaction. Many controls do not accept user interaction by default, such as ImageView or Label. So we need to let the control receive clicks and enter the following code
Copy the codeThe code is as follows:

= true