IOS development-Gesture Recognition (UIGestureRecognizer)

Source: Internet
Author: User

IOS development-Gesture Recognition (UIGestureRecognizer)
UIGestureRecognizer

The UIGestureRecognizer must be used to complete gesture recognition.

Using UIGestureRecognizer, you can easily identify some common gestures you make on a view.

UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures. You can use its subclass to process specific gestures.

Merge // (strike) Merge // (pinch, used for scaling) UIPanGestureRecognizer // (drag and drop) Merge // (swipe) rotate // (rotate) UILongPressGestureRecognizer // (long press)
Hitting UITapGestureRecognizer

The usage of each gesture reader is similar. For example, the steps for using UITapGestureRecognizer are as follows:

Create a gesture identification object UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; set the attributes of the gesture recognition object // tap twice in a row. numberOfTapsRequired = 2; // you need two fingers to strike the tap together. numberOfTouchesRequired = 2; Add the gesture reader to the corresponding view [self. iconView addGestureRecognizer: tap]; listener [tap addTarget: self action: @ selector (tapIconView :)];
Hitting an instance
@ Interface GRViewController ()
  
   
@ Property (weak, nonatomic) IBOutlet UIImageView * iconView; @ end @ implementation GRViewController-(void) viewDidLoad {[super viewDidLoad]; [self testTap2];}-(void) testTap2 {UIGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapView)]; tap. delegate = self; [self. iconView addGestureRecognizer: tap] ;}# pragma mark-proxy method/*** when you click view, this method is called first */-(BOOL) gest UreRecognizer :( UIGestureRecognizer *) gestureRecognizer shouldReceiveTouch :( UITouch *) touch {CGPoint pos = [touch locationInView: touch. view]; if (pos. x <= self. iconView. frame. size. width * 0.5) {return YES;} return NO;}-(void) testTap {// 1. create a gesture identification object UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; // The gesture can be recognized successfully after two consecutive strokes. numberOfTapsRequired = 2; tap. numberOfTouchesRequired = 2; // 2. add the gesture reader object to the corresponding view [self. iconView addGestureRecognizer: tap]; // 3. add a listening method (the corresponding gesture is recognized and the listening method is called) [tap addTarget: self action: @ selector (tapView)];}-(void) tapView {NSLog (@ "----- I tapped the screen !! ");} @ End
  
Long-pressed and lightly scanned instances
@ Interface GRViewController () @ property (weak, nonatomic) IBOutlet UIView * redView; @ end @ implementation GRViewController-(void) viewDidLoad {[super viewDidLoad]; UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipeView)]; swipe. direction = uiswipegesturerecognizerdireup up; [self. redView addGestureRecognizer: swipe];}-(void) swipeView {NSLog (@ "swipeView");}-(void) testLongPress {login * longPress = [[using alloc] init]; [longPress addTarget: self action: @ selector (longPressView)]; // press longPress for at least 2 seconds. minimumPressDuration = 2; // before the gesture is triggered, press the valid longPress within the 50px range. allowableMovement = 50; [self. redView addGestureRecognizer: longPress];}-(void) longPressView {NSLog (@ "long pressed Red view");} @ end
Scaling and rotating instances
@ Interface GRViewController ()
  
   
@ Property (weak, nonatomic) IBOutlet UIImageView * iconView; @ end @ implementation GRViewController-(void) viewDidLoad {[super viewDidLoad]; [self testPinchAndRotate];} # pragma mark-proxy method of the gesture Reader/*** whether multiple gesture identifiers are allowed to work Simultaneously * Simultaneously: Simultaneously */-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer :( UIGestureRecognizer *) otherGestureRecognizer {Return YES;} # pragma mark-zoom + rotate-(void) testPinchAndRotate {[self testPinch]; [self testRotate];} # pragma mark-zoom gesture (pinch gesture) -(void) testPinch {UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinchView :)]; pinch. delegate = self; [self. iconView addGestureRecognizer: pinch];}-(void) pinchView :( UIPinchGestureRecognizer *) pinch {pinch. view. transfo Rm = CGAffineTransformScale (pinch. view. transform, pinch. scale, pinch. scale); pinch. scale = 1; // This is really important !!!!!} # Pragma mark-rotation gesture-(void) testRotate {UIRotationGestureRecognizer * recognizer = [[externalloc] initWithTarget: self action: @ selector (rotateView :)]; recognizer. delegate = self; [self. iconView addGestureRecognizer: recognizer];}-(void) rotateView :( UIRotationGestureRecognizer *) recognizer {recognizer. view. transform = CGAffineTransformRotate (recognizer. view. transform, recognizer. ro Tation); recognizer. rotation = 0; // This is very important !!!!!} @ End
  
Drag an instance
@ Interface GRViewController () @ property (weak, nonatomic) IBOutlet UIView * purpleView; @ end @ implementation GRViewController-(void) viewDidLoad {[super viewDidLoad]; UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panView :)]; [self. purpleView addGestureRecognizer: pan];}-(void) panView :( UIPanGestureRecognizer *) pan {switch (pan. state) {case UIGestureRecognizerStateBegan: // start to trigger the gesture break; case UIGestureRecognizerStateEnded: // stop the gesture break; default: break;} // 1. the distance to CGPoint translation = [pan translationInView: pan. view]; CGPoint center = pan. view. center; center. x + = translation. x; center. y + = translation. y; pan. view. center = center; // 2. clear the distance of the Movement [pan setTranslation: CGPointZero inView: pan. view];} @ end
Gesture Recognition Status
Typedef NS_ENUM (NSInteger, UIGestureRecognizerState) {// no touch event occurs, default status of all Gesture Recognition is unknown, // when a gesture has started but has not been changed or completed, UIGestureRecognizerStateBegan, // The gesture status changes to UIGestureRecognizerStateChanged, // The gesture completes UIGestureRecognizerStateEnded, // The gesture is canceled and restored to the Possible status failed, // The gesture fails and is restored to the Possible status failed, // recognize the gesture recognition UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded };

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.