IOS gestures and ios gestures

Source: Internet
Author: User

IOS gestures and ios gestures

In IOS, gestures can give users a good experience. Therefore, it is necessary to understand gestures.

(There are many notable points in setting gestures)

* Yes. The click fails to respond when the value is set to Yes *

* Add the gesture to the View to be clicked. Otherwise, the response fails *

There are six types of gestures. I will introduce them separately below.

Click gesture

/// ViewController. m // CX-gesture details /// Created by ma c on 16/3/24. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController # pragma mark-set_and_get-(UIImageView *) imageView {if (! _ ImageView) {_ imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed: @ "nvshen.jpg"]; _ imageView. bounds = (CGRect) {CGPointZero, image. size}; _ imageView. center = self. view. center; // The interaction must be set to YES; otherwise, the gesture _ imageView cannot be implemented. userInteractionEnabled = YES; _ imageView. image = image;} return _ imageView;}-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageView]; // click the gesture UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; // click it to take effect. numberOfTapsRequired = 1; UITapGestureRecognizer * tapNew = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; // click twice to apply tapNew. numberOfTapsRequired = 2; // Add the gesture [self. imageView addGestureRecognizer: tap]; [self. imageView addGestureRecognizer: tapNew]; // when two clicks are valid, the click fails. [tap requireGestureRecognizerToFail: tapNew];}-(void) doAction :( UITapGestureRecognizer *) tap {if (tap. numberOfTapsRequired = 1) {NSLog (@ "click");} else if (tap. numberOfTapsRequired = 2) {NSLog (@ "click twice") ;}}@ end
Drag gesture

/// ViewController. m // CX-gesture details /// Created by ma c on 16/3/24. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController # pragma mark-set_and_get-(UIImageView *) imageView {if (! _ ImageView) {_ imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed: @ "nvshen.jpg"]; _ imageView. bounds = (CGRect) {CGPointZero, image. size}; _ imageView. center = self. view. center; // The interaction must be set to YES; otherwise, the gesture _ imageView cannot be implemented. userInteractionEnabled = YES; _ imageView. image = image;} return _ imageView;}-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageView]; // drag gesture U IPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; [self. view addGestureRecognizer: pan];}-(void) doAction :( UIPanGestureRecognizer *) pan {// get the offset CGPoint point = [pan translationInView: self. imageView]; // change self. ImageView Center to implement drag self. imageView. center = CGPointMake (self. imageView. center. x + point. x, self. imageView. center. y + point. y); // If the reset is not performed, it will be changed based on the change so that the effect is not correct. [pan setTranslation: CGPointZero inView: self. imageView];} @ end
Long-pressed gesture

/// ViewController. m // CX-gesture details /// Created by ma c on 16/3/24. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController # pragma mark-set_and_get-(UIImageView *) imageView {if (! _ ImageView) {_ imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed: @ "nvshen.jpg"]; _ imageView. bounds = (CGRect) {CGPointZero, image. size}; _ imageView. center = self. view. center; // The interaction must be set to YES; otherwise, the gesture _ imageView cannot be implemented. userInteractionEnabled = YES; _ imageView. image = image;} return _ imageView;}-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageView]; // long-pressed gesture UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; [self. imageView addGestureRecognizer: longPress];}-(void) doAction :( UILongPressGestureRecognizer *) longPress {if (longPress. state = UIGestureRecognizerStateBegan) {NSLog (@ "");} else if (longPress. state = UIGestureRecognizerStateEnded) {NSLog (@ "end") ;}} @ end
Swipe gesture
/// ViewController. m // CX-gesture details /// Created by ma c on 16/3/24. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController # pragma mark-set_and_get-(UIImageView *) imageView {if (! _ ImageView) {_ imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed: @ "nvshen.jpg"]; _ imageView. bounds = (CGRect) {CGPointZero, image. size}; _ imageView. center = self. view. center; // The interaction must be set to YES; otherwise, the gesture _ imageView cannot be implemented. userInteractionEnabled = YES; _ imageView. image = image;} return _ imageView;}-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageView]; // gesture U ISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; // you need to set the default value to right. Click the desired direction. You can specify whether multiple directions lead to the same behavior (for example, UITableView sliding deletion) */swipe. direction = uiswipegesturerecognizerdireleft; [self. imageView addGestureRecognizer: swipe];}-(void) doAction :( UISwipeGestureRecognizer *) swipe {if (swipe. direction = uiswipegesturerecognizerdireleft) {NSLog (@ "Left");} else if (swipe. direction = uiswipegesturerecognizerdireright) {NSLog (@ "right");} else if (swipe. direction = UISwipeGestureRecognizerDirectionDown) {NSLog (@ "");} else if (swipe. direction = uiswipegesturerecognizerdireup up) {NSLog (@ "") ;}} @ end
Kneading gesture

(Some operations are required in pinch and rotate gestures)

* When you press and hold option to touch the touchpad, two fingers that appear in the simulation will appear *

* If the view you are operating on is not in the position of two touch points, you can shift it *

* When kneading and rotating, you must press the touchpad to perform the operation *

/// ViewController. m // CX-gesture details /// Created by ma c on 16/3/24. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController # pragma mark-set_and_get-(UIImageView *) imageView {if (! _ ImageView) {_ imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed: @ "nvshen.jpg"]; _ imageView. bounds = (CGRect) {CGPointZero, image. size}; _ imageView. center = self. view. center; // The interaction must be set to YES; otherwise, the gesture _ imageView cannot be implemented. userInteractionEnabled = YES; _ imageView. image = image;} return _ imageView;}-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageView]; // pinch gesture UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; [self. imageView addGestureRecognizer: pinch];}-(void) doAction :( UIPinchGestureRecognizer *) pinch {// continuously change self. imageView. transform = CGAffineTransformScale (self. imageView. transform, pinch. scale, pinch. scale); // reset pinch. scale = 1 ;}@ end
Rotation gesture

/// ViewController. m // CX-gesture details /// Created by ma c on 16/3/24. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController # pragma mark-set_and_get-(UIImageView *) imageView {if (! _ ImageView) {_ imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed: @ "nvshen.jpg"]; _ imageView. bounds = (CGRect) {CGPointZero, image. size}; _ imageView. center = self. view. center; // The interaction must be set to YES; otherwise, the gesture _ imageView cannot be implemented. userInteractionEnabled = YES; _ imageView. image = image;} return _ imageView;}-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageView]; // rotation gesture UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (doAction :)]; [self. imageView addGestureRecognizer: rotation];}-(void) doAction :( UIRotationGestureRecognizer *) rotation {// continuously change self. imageView. transform = CGAffineTransformRotate (self. imageView. transform, rotation. rotation); // reset rotation. rotation = 0;} @ end

It is worth noting that the rotation and Kneading gestures cannot be operated at the same time. To operate simultaneously, You can implement them through the proxy, as shown below.

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

In the above Code implementation, return YES.

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.