IOS gesture operation instances

Source: Internet
Author: User

IOS gesture operation instances
First look at the effect

Introduction to gestures

In IOS, gesture operations are generally implemented by several gesture sub-classes of the UIGestureRecognizer class. Generally, five types of gestures are used:

1. Click UITapGestureRecognizer.

2. Translate UIPanGestureRecognizer

3. zooming UIPinchGestureRecognizer

4. Rotate UIRotationGestureRecognizer

5. Scan UISwipeGestureRecognizer

The above five gestures are used in the instance, but the click and sweep operations are not shown, but the logs are output. I will check the code later.

Next we will introduce these gestures respectively.

1. UITapGestureRecognizer click gesture
UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapGes :)]; // The number of clicks. The default value is 1, 1, and 2. numberOfTapsRequired = 2;

This click gesture class has a property numberOfTapsRequired used to set the number of clicks. This event is triggered only after several clicks.

2. UIPanGestureRecognizer translation gesture
// Translation gesture-(void) initPanGes {UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGes :)]; [self. imgView addGestureRecognizer: panGes];}-(void) panGes :( UIPanGestureRecognizer *) ges {// get the coordinate point CGPoint transPoint = [ges translationInView: self. imgView];}

The translation gesture itself does not have many configurable attributes. In the translation event trigger, you can use the translationInView method to obtain the current translation coordinate point.

3. UIPinchGestureRecognizer scaling gesture
// Scaling gesture-(void) initPinGes {UIPinchGestureRecognizer * pinGes = [[using alloc] initWithTarget: self action: @ selector (pinGes :)]; [self. imgView addGestureRecognizer: pinGes];}-(void) pinGes :( UIPinchGestureRecognizer *) ges {// zooming self. imgView. transform = CGAffineTransformScale (self. imgView. transform, ges. scale, ges. scale );}

The scaling gesture can get the scale attribute in the event to indicate the current scaling value.

4. UIRotationGestureRecognizer rotation gesture
// Rotate gesture-(void) initRotation {UIRotationGestureRecognizer * rotationGes = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotationGes :)]; [self. imgView addGestureRecognizer: rotationGes];}-(void) rotationGes :( UIRotationGestureRecognizer *) ges {// rotate the image self. imgView. transform = CGAffineTransformRotate (self. imgView. transform, ges. rotation );}

In the event, you can get the rotation attribute to get the current rotation angle.

5. UISwipeGestureRecognizer swipe gesture
// Swipe gesture-(void) initSwipeGes {// create a swipe gesture from right to left UISwipeGestureRecognizer * swipeLeftGes = [UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipeGes :)]; // direction. By default, only one gesture can be enabled from left to right, if you want to enable multiple gestures, you have to create multiple gestures // listen to swipeLeftGes from the right to the left. direction = uiswipegesturerecognizerdireleft; [self. imgView addGestureRecognizer: swipeLeftGes];}-(void) swipeGes :( UISwipeGestureRecognizer *) ges {// ges. direction value NSLog (@ "% s diection: % lu" ,__ func __, (unsigned long) ges. direction );}

You need to set the direction attribute for the lightly scanned gesture object. The default value is to listen from left to right, which is an enumerated value UISwipeGestureRecognizerDirection.

UISwipeGestureRecognizerDirectionRight from left to right (default)

UISwipeGestureRecognizerDirectionLeft from right to left

UISwipeGestureRecognizerDirectionUp from bottom to top

UISwipeGestureRecognizerDirectionDown from top to bottom

 

Let's take a look at the above implementation code.

/// ViewController. m // various gesture operations /// Created by xgao on 16/3/24. // Copyright©Xgao. all rights reserved. // # import "ViewController. h "@ interface ViewController () <strong> @ property (weak, nonatomic) IBOutlet UIImageView * imgView; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self initTapGes]; [self initPanGes]; [self initPinGes]; [self initRotation]; [self initSwipeGes];} // click gesture-(void) initTapGes {custom * tapGes = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapGes :)]; // Number of clicks. The default value is 1, 1, and 2. Double-click tapGes. numberOfTapsRequired = 2; tapGes. delegate = self; [self. imgView addGestureRecognizer: tapGes];}-(void) tapGes :( UITapGestureRecognizer *) ges {NSLog (@ "% s" ,__ func __);} // translation gesture-(void) initPanGes {UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGes :)]; panGes. delegate = self; [self. imgView addGestureRecognizer: panGes];}-(void) panGes :( UIPanGestureRecognizer *) ges {// get the coordinate point CGPoint transPoint = [ges translationInView: self. imgView]; // move the image self. imgView. transform = CGAffineTransformTranslate (self. imgView. transform, transPoint. x, transPoint. y); // restore, must restore // clear each time to eliminate the coordinate superposition [ges setTranslation: CGPointZero inView: self. imgView]; NSLog (@ "% s" ,__ func _);} // scaling gesture-(void) initPinGes {UIPinchGestureRecognizer * pinGes = [UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinGes :)]; pinGes. delegate = self; [self. imgView addGestureRecognizer: pinGes];}-(void) pinGes :( UIPinchGestureRecognizer *) ges {// zooming self. imgView. transform = CGAffineTransformScale (self. imgView. transform, ges. scale, ges. scale); // restore // clear each time to eliminate overlapping ges. scale = 1 ;}// rotate gesture-(void) initRotation {UIRotationGestureRecognizer * rotationGes = [[externalloc] initWithTarget: self action: @ selector (rotationGes :)]; rotationGes. delegate = self; [self. imgView addGestureRecognizer: rotationGes];}-(void) rotationGes :( UIRotationGestureRecognizer *) ges {// rotate the image self. imgView. transform = CGAffineTransformRotate (self. imgView. transform, ges. rotation); // restore // clear each time to eliminate overlapping ges. rotation = 0; NSLog (@ "% s" ,__ func _);} // swipe gesture-(void) initSwipeGes {// create a gesture UISwipeGestureRecognizer * swipeLeftGes = [[delealloc] initWithTarget: self action: @ selector (swipeGes :)]; // direction, by default, only one gesture can be enabled from left to right. If you want to enable multiple gestures, you must create multiple gestures. // listen to swipeLeftGes from right to left. direction = uiswipegesturerecognizerdireleft; swipeLeftGes. delegate = self; [self. imgView addGestureRecognizer: swipeLeftGes]; // listen to swipeUpGes from bottom to top. direction = uiswipegesturerecognizerdireup up; swipeUpGes. delegate = self; [self. imgView addGestureRecognizer: swipeUpGes];}-(void) swipeGes :( UISwipeGestureRecognizer *) ges {// ges. direction value NSLog (@ "% s diection: % lu" ,__ func __, (unsigned long) ges. direction) ;}# pragma mark-alert // determines whether a gesture can be triggered-(BOOL) Alert :( UITapGestureRecognizer *) gestureRecognizer {return YES;} // whether multiple gesture operations are allowed, not multiple touch points-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer restart :( UIGestureRecognizer *) otherGestureRecognizer {return YES;} @ end

 

Note the following two points:

1. For the three gestures of translation, scaling, and rotation, remember to restore them if we want to use their values! Restore! Restore! This is important! Important: 3 times ~~

In the translation gesture, we need to set setTranslation: CGPointZero to restore its coordinate value. Otherwise, this coordinate value will be superimposed upon the next event.
Set ges. scale = 1 in the scaling gesture to restore its scaling value.
Set ges. rotation = 0 in the rotation gesture to restore its angle value.

2. If we need to use multiple gestures together, we need to set a method for returning parameters in delegate.

  

// Whether multi-gesture operations are allowed, not multi-touch point-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer handler :( UIGestureRecognizer *) handle {return YES ;}

This is the sharing ~~ Leave a message if you have any questions. ^_^

 

 

 

 

 

 

 

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.