Recently do the project need to use gesture recognition, so took a little time to learn a bit, in fact gesture recognition this part of the content is not much, or very easy to get started.
There are six types of gestures in iOS: Tap (TAP), longpress (Long Press), swipe (swipe), pan (drag), Pich (zoom), rotation (rotate). These six gesture classes are inherited from Uigesturerecongnizer, so take a look at the Uigesturerecongnizer class first.
Uigesturerecongizer inherits from NSObject, is the parent class of the above six gestures, defines some common properties and methods of all gestures, some of which are more commonly used.
Initialization method:
-(Instancetype) Initwithtarget: (nullable ID) Target action: (nullable SEL) action Ns_designated_initializer; Designated initializer
-(void) AddTarget: (ID) Target action: (SEL) Action; Add a target/action pair. You can call this multiple times to specify multiple target/actions
-(void) Removetarget: (nullable ID) Target action: (nullable SEL) action;
-(Cgpoint) Locationinview: (nullable uiview*) view; A generic Single-point location for the gesture. Usually the centroid of the touches involved
-(Nsuinteger) numberoftouches; Number of touches involved for which locations can be queried
-(Cgpoint) Locationoftouch: (Nsuinteger) Touchindex InView: (nullable uiview*) view; The location of a particular touch
The above methods are relatively easy to understand, look at the method name to know what the meaning of, it is worth mentioning is the proxy method:
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer;
And instance method:-(void) Requiregesturerecognizertofail: (Uigesturerecognizer *) Othergesturerecognizer;
These two methods are used to resolve conflicts when two gestures conflict (such as when a colleague adds swipe and pan). First implement the above proxy method, return Yes, and then call Requiregesturerecognizertofail: The method is as follows:
[Pan Requiregesturerecognizertofail:swipe];
This allows the swipe gesture to be performed when the pan and swipe gestures conflict, and the pan gesture is performed only if the swipe gesture recognition fails.
Property:
@property (nonatomic,readonly) uigesturerecognizerstate state;//defines the current state of the gesture, whether it is the start, end, or failure, and so on.
@property (Nullable, nonatomic,readonly) UIView *view;//The parent view of the gesture
Tap tap gesture: UITapGestureRecognizer
The simplest gesture, only two properties
@property (nonatomic) Nsuinteger numberoftapsrequired;//need to be clicked to respond
@property (nonatomic) Nsuinteger numberoftouchesrequired;//need a few points to respond back
Longpress Long Press gesture: Uilongpressgesturerecognizer
@property (nonatomic) Nsuinteger numberoftouchesrequired//How many points you need to press
@property (nonatomic) Cftimeinterval minimumpressduration; How long do you need to press
@property (nonatomic) cgfloat allowablemovement;//The movement range of the finger when pressed
Swipe waving gesture Uiswipegesturerecognizer
After initializing a uiswipegesturerecognizer gesture, set its Direction property and set its orientation. A swipe gesture intelligently responds in one direction, and if you need more than one direction of wave action, you need to add multiple swipe gestures for long.
@property (nonatomic) uiswipegesturerecognizerdirection direction;//Direction
@property (nonatomic) Nsuinteger numberoftouchesrequired//need several points to respond back
Pan drag gesture Uipangesturerecognizer
-(Cgpoint) Translationinview: (Nullable UIView *) view; Where the finger moves on the view
-(void) Settranslation: (cgpoint) Translation InView: (Nullable UIView *) view;//set the position where the finger moves on the view
-(Cgpoint) Velocityinview: (Nullable UIView *) view; The rate at which the finger moves on the view
Pinch zoom gesture Uipinchgesturerecognizer
@property (nonatomic) cgfloat scale; Scaled scale
@property (nonatomic,readonly) cgfloat velocity; Rate of scaling
Rotation rotation gesture Uirotationgesturerecognizer
@property (nonatomic) cgfloat rotation; Rotation in radians radians rotated
@property (nonatomic,readonly) cgfloat velocity; Rate of rotation (radians/sec)
Gesture recognition in iOS