iOS gesture-UIGestureRecognizer

Original link

1. UIGestureRecognizer

  • The parent class is NSObject
    • Using the gesture recognizer---UIGestureRecognizer, it can easily recognize some common gestures the user makes on a certain view
    • UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures. Only by using its subclasses can specific gestures be processed. See its subclass introduction for details.
    • A gesture can only support one direction
    • By default, only one gesture is supported. If you want to implement multiple gestures, you need to set up a gesture proxy, comply with <UIGestureRecognizerDelegate>, and implement it shouldRecognizeSimultaneouslyWithGestureRecognizer, and return YES.

    2. The status of gesture recognition

    Gesture state meaning
    UIGestureRecognizerStatePossible No touch event occurs, the default state of all gesture recognition
    UIGestureRecognizerStateBegan When a gesture has been started but not changed or completed
    UIGestureRecognizerStateChanged Gesture state change
    UIGestureRecognizerStateEnded Gesture done
    UIGestureRecognizerStateCancelled Gesture canceled, return to Possible state
    UIGestureRecognizerStateFailed Gesture failed, return to Possible state
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded Gesture recognition

    3. UIGestureRecognizer properties

    // 手势状态
    @property(nonatomic,readonly) UIGestureRecognizerState state;
    

    // gesture agent
    @Property ( Nullable , nonatomic , weak ) ID < UIGestureRecognizerDelegate > the delegate;

    // gesture is available (available by default)
    // not modify this gesture is available when the gesture recognition
    @Property ( nonatomic , getters = isEnabled) BOOL Enabled;

    // gesture view
    the @Property ( Nullable , nonatomic , Readonly ) UIView * View;

    @property(nonatomic) BOOL cancelsTouchesInView;
    @property(nonatomic) BOOL delaysTouchesBegan;
    @property(nonatomic) BOOL delaysTouchesEnded;

    @property(nonatomic, copy) NSArray<NSNumber *> *allowedTouchTypes
    @property(nonatomic, copy) NSArray<NSNumber *> *allowedPressTypes

    4. UIGestureRecognizer method

    
    // 初始化手势,直接用initWithTarget就好,另外两个忽略
    //     -(void)handleGesture;
    //     -(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer;
    - (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action
    

    // Add monitor and remove monitor

    • (void)addTarget:(id)target action:(SEL)action;
    • ( void )removeTarget:( nullable id )target action:( nullable SEL)action; //If target is nil, remove all methods with the same method name

    // Get the position of touch

    • (CGPoint)locationInView:(nullable UIView*)view;

    // Get the number of touches in locations

    • (NSUInteger)numberOfTouches;

    // Get the touch position of a specific corner mark

    • (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;

    • (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
      @end

    5. UIGestureRecognizer's proxy--UIGestureRecognizerDelegate

    
    // 是否允许触发当前手势
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
    

    // Simultaneously: sɪml'teɪnɪəslɪ at the same time
    // Is it allowed to support multiple gestures at the same time

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

    // Whether to receive touch gestures

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

    // Whether to receive the press gesture

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

    • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

    @end

    6. Subclass of UIGestureRecognizer

    6.1. Tap gesture
    6.2. Pinch gesture -for zoom
    6.3. Drag gesture
    6.4. Swipe gestures
    6.5. Rotation gesture
    6.6. Long press gesture

Guess you like

Origin blog.csdn.net/Draven__/article/details/90576924
ios