first, the practice of listening to touch events If you want to listen to a touch event on a view, the previous practice is to customize a view first, and then implement the touches method of the view to implement the specific processing code inside the methodThere are a few obvious drawbacks to monitoring the view touch event through the touches method(1) Custom view required(2) because the touch event is monitored in the touches method inside the view, it is not possible to allow other external objects to listen to the touch events of the view (via proxy) by default(3) not easy to distinguish user's specific gesture behaviorafter IOS 3.2, Apple introduced the gesture recognition feature (Gesture recognizer), which greatly simplifies developer development in touch event handling
second, gesture recognizerIn order to complete gesture recognition, the gesture recognizer must be used----Uigesturerecognizerwith Uigesturerecognizer, you can easily identify some common gestures that users make on a viewUigesturerecognizer is an abstract class that defines the basic behavior of all gestures and uses its subclasses to handle specific gestures UITapGestureRecognizer (PAT)Uipinchgesturerecognizer (pinch, for zooming)Uipangesturerecognizer (drag)Uiswipegesturerecognizer (swipe)uirotationgesturerecognizer (swivel)Uilongpressgesturerecognizer (Long Press)Uiscreenedgepangesturerecognizer (screen edge swipe)
Code Description: The common status of gestures is as follows
Start: Uigesturerecognizerstatebegan
Change: uigesturerecognizerstatechanged
Ends: uigesturerecognizerstateended
Cancel: uigesturerecognizerstatecancelled
Failure: uigesturerecognizerstatefailed
(i), Pat gesture each gesture recognizer uses a similar set of steps, such as the following uitapgesturerecognizer:(1) Creating a gesture Recognizer ObjectUITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];(2) Setting the specific properties of the gesture Recognizer Object//TAP 2 times in a rowtap.numberoftapsrequired = 2;//requires 2 fingers to touchtap.numberoftouchesrequired = 2;(3) Add gesture recognizer to the corresponding view[Self.iconview Addgesturerecognizer:tap];(4) trigger of the listening gesture[Tap addtarget:self Action: @selector (Tapiconview:)];Property Description:numberoftouchesrequired//How many fingers need to be tapped together (default is 1)numberoftapsrequired//How many taps are required (default is 1)Implementation method
(ii), long press gesture Implementation Method
(iii), rotation gesture
The uirotationgesturerecognizer gesture recognizer, like the name, can be used to listen to and capture rotating gestures, which can help you create a more intuitive graphical user interface, such as a scene where you have a view of the display image in your app, The user needs to adjust the orientation of the picture by rotating the picture.
Uirotationgesturerecognizer This class has a rotation attribute that can be used to set the direction of rotation and the radian of rotation. Rotation is determined from the initial position of the finger (Uigesturerecognizerstatebegan) to the final position (Uigesturerecognizerstatebegan).
To rotate a UI element that inherits from UIView, you can pass the rotation property of the rotation gesture recognizer to the Cgaffinetransformmakerotation method to make an affine transition
Implementation method
Tap, hold, and rotate the gesture recognizer method in the IOS development UI