Referring to Apple's UIResponder documentation , all UIView objects (including UIWindow), the UIApplication object, UIViewController objects are instances of the UIResponder. To handle a specific type of event, the respondent must override the appropriate methods.
In our case, touches is our type of event. Therefore, our respondent must implement the following methods. touchSegan (: c :), touchMoved (: c :), TouchEnded (: c :), and TouchCancelled (: c :)
Since we only want to know when the user has touched a certain view, we only need to implement touchesBegan(_:with:) . Since we do not override other methods, we must call super.touchesBegan(touches, with: event) . If we redefined ALL other methods, we would not have to call super .
Looking at touchesBegan(_:with:) the touches parameter of a collection of UITouch instances. Each instance represents touches for the initial phase of the event, which is represented by the event parameter.
For touches in a view, this set by default contains only one touch. Therefore, touches.first is the only UITouch instance in the collection. Then access to the real estate view , this is the view or window in which the touch occurred. Finally, we compare the view that has been affected with your desired view.
It should be noted that if you want to get several touches-, you must set the isMultipleTouchEnabled property of the isMultipleTouchEnabled view isMultipleTouchEnabled true . Then the touches will have more than one UITouch instance, and you will have to handle it accordingly.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) if let touch = touches.first, touch.view == myView {