IOS_38 _ gesture and ios_38 gesture

Source: Internet
Author: User

IOS_38 _ gesture and ios_38 gesture
Events are classified into three categories: Touch, accelerator, and remote control.
Only the subcategory of the responder can receive and process events.
The event processing interface defined in the parent class responder is as follows:
The following are four methods for processing a touch event: (as long as the event is implemented, the system automatically calls the event)
A UITouch object represents a finger moving, and the UITouch object is updated in real time.
A UITouch object corresponds to a finger and records all the information during touch.
Important ~ The UITouch method is often used to obtain information (such as location and object) during touch)
The UIEvent of the event object. common attributes are: event type.
The four methods of touch (process) are described in detail: Pay attention to the situation of simultaneously touching the first and last touch.
You must first find the most appropriate event Responder (from parent to child location)
The following is an example of finding the most appropriate event responder:
Special cases: pay special attention
After finding the best handler of the event, it is the responder chain.[Super touchesXXX] is called by default. This super is the last responder.That is, next responder in the official document
Next Responder
To sum up, the view has a controller, which is passed to the Controller. Otherwise, the view is passed to the parent view.
Conclusion: The chain transmission mechanism of the RESPONDER (the last RESPONDER is next responder)
Traditional event listening practices: (not recommended)
The six gesture identifiers are described as follows:To listen for more than two types of gestures at the same time:Set delegate for each gesture and implement the following method:

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



Standard Practices for Gesture Recognition: three steps (creation, setting, and binding)
Focus on the status of the three types of Gesture Recognition: Start, end, and cancel
Status changes of Gesture Recognition in official documents
Proxy method of the gesture reader < UIGestureRecognizerDelegate> ShouldReceiveTouch can specify that the gesture is valid under specific conditions.

ShouldRecognizeSimultaneouslyWithGestureRecognizer

If YES is returned, different gestures, such as rotation and scaling, can be recognized at the same time.



Pan translation gesture Finally:
//// PanController. m // 38 _ gesture /// Created by beyond on 14-9-16. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "PanController. h "@ interface PanController () // View of the nana Avatar @ property (weak, nonatomic) IBOutlet UIView * nanaView;-(IBAction) dismiss; @ end @ implementation PanController-(void) viewDidLoad {[super viewDidLoad]; // create a pan gesture and bind the listener method ingress * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panView :)]; [self. nanaView 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] ;}# pragma mark-link-(IBAction) dismiss {[self dismissViewControllerAnimated: YES completion: nil] ;}@ end


Tap gesture
//// TapController. m // 38 _ gesture /// Created by beyond on 14-9-16. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "TapController. h "// gesture proxy @ interface TapController () <UIGestureRecognizerDelegate> @ property (weak, nonatomic) IBOutlet UIImageView * nanaImgView;-(IBAction) dismiss; @ end @ implementation TapController-(void) viewDidLoad {[super viewDidLoad]; _ nanaImgView. userInteractionEnabled = YES; _ nanaImgView. multipleTouchEnabled = YES; // [self testTap]; [self testTap2];}-(void) testTap {// 1. create a Tap gesture identification object UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; // two gestures can be recognized only when two consecutive strokes are performed. numberOfTapsRequired = 2; tap. numberOfTouchesRequired = 2; // 2. add a listening method (the corresponding gesture is recognized and the listening method is called) [tap addTarget: self action: @ selector (taping)]; // 3. add the Tap gesture identification object [self. nanaImgView addGestureRecognizer: tap];}-(void) testTap2 {// 1. create a Tap gesture identification object and bind the listening method (the corresponding gesture is recognized and the listening method is called) UIGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (taping)]; // 2. set the proxy of the gesture to determine that the gesture will be recognized (the method used to trigger the listener) tap only in a specific situation. delegate = self; // 3. add the Tap gesture identification object [self. nanaImgView addGestureRecognizer: tap] ;}# define kRandomColor [UIColor colorWithRed: arc4random () % 255/255. 0 green: arc4random () % 255/255. 0 blue: arc4random () % 255/255. 0 alpha: 1.0] // method of listening-(void) taping {// the background color is randomly changed for each tap. view. backgroundColor = kRandomColor; NSLog (@ "----- taping") ;}# proxy method of pragma mark-gestureRecognizer // when you click view, you will first ask for this method, whether to receive this tap click (that is, whether it is a valid tap)-(BOOL) gestureRecognizer :( optional *) gestureRecognizer shouldReceiveTouch :( UITouch *) touch {CGPoint pos = [touch locationInView. view]; // if (pos. x <= self. nanaImgView. frame. size. width * 0.5) {return YES;} return NO;} # pragma mark-connection method-(IBAction) dismiss {[self dismissViewControllerAnimated: YES completion: nil];} @ end

Swipe gesture


LongPress long-pressed gesture




Main attribute parameters of Long-pressed gestures



//// SwipeLongPressController. m // 38 _ gesture /// Created by beyond on 14-9-17. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "SwipeLongPressController. h "@ interface SwipeLongPressController () @ property (weak, nonatomic) IBOutlet UIImageView * nanaImgView;-(IBAction) dismiss; @ end @ implementation SwipeLongPressController-(void) viewDidLoad {[super viewDidLoad]; // 1. allow interaction _ nanaImgView. userInteractionEnabled = YES; // 2. custom method: add the swipe gesture [self addSwipe]; // 3. custom method: add the longPress gesture [self addLongPress];} // 2. custom method, add swipe gesture-(void) addSwipe {// 1. create a Swipe gesture identification object and bind the listening method (the corresponding gesture is recognized and the listening method is called) UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swiping)]; // sets the attribute: swipe In the light direction. direction = uiswipegesturerecognizerdireup up; // 2. add the Swipe gesture identification object [self. nanaImgView addGestureRecognizer: swipe];} // 3. custom method, add longPress gesture-(void) addLongPress {// 1. create a LongPress gesture identification object and bind the listening method (the corresponding gesture is recognized and the listening method is called). UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] init]; [longPress addTarget: self action: @ selector (longPressing)]; // set the attribute: at least 2 seconds long. The default value is 0.5 seconds longPress. minimumPressDuration = 2; // after pressing, do not relax. Before the gesture can be triggered, the range that can be moved is allowed. Long-pressed within the 50px range is valid. The default value is 10px longPress. allowableMovement = 50; // 2. add the Swipe gesture identification object [self. nanaImgView addGestureRecognizer: longPress] ;}# define kRandomColor [UIColor colorWithRed: arc4random () % 255/255. 0 green: arc4random () % 255/255. 0 blue: arc4random () % 255/255. 0 alpha: 1.0] // method of gesture monitoring-(void) swiping {self. view. backgroundColor = kRandomColor; NSLog (@ "----- swiping");} // method of gesture listening-(void) longPressing {self. view. backgroundColor = kRandomColor; NSLog (@ "------- long-pressed nanaImgView");}-(IBAction) dismiss {[self dismissViewControllerAnimated: YES completion: nil];} @ end


Pinch and Rotation gestures


Kneading (scaling) and rotating


Finally:




//// PinchRotationController. m // 38 _ gesture /// Created by beyond on 14-9-17. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "PinchRotationController. h "// The proxy method of the gesture reader. The purpose is: @ interface PinchRotationController () <UIGestureRecognizerDelegate> @ property (weak, nonatomic) IBOutlet UIImageView * nanaImgView; @ end @ implementation PinchRotationController-(void) viewDidLoad {[super ViewDidLoad]; // Add both the Pinch gesture and the rotation gesture [self addPinchAndRotate];} # pragma mark-zoom + rotate-(void) addPinchAndRotate {// 1. add Pinch gesture (zoom) [self addPinch]; // 2. add a rotation gesture [self addRotate];} // 1. add Pinch gesture (zoom), zoom gesture (Pinch gesture)-(void) addPinch {// 1. create a Pinch gesture identification object and bind the listening method (the corresponding gesture is recognized and the listening method is called) UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinching: )]; // 2. Set proxy ??? Pinch. delegate = self; // 3. add the Pinch gesture identification object [self. nanaImgView addGestureRecognizer: pinch];} // 2. add a rotation gesture-(void) addRotate {// 1. create a Rotation gesture identification object and bind the listening method (the corresponding gesture is recognized and the listening method is called). UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotating :)]; // 2. set proxy ??? Rotate. delegate = self; // 3. Add the Rotation gesture reader object to nanaImgView [self. nanaImgView addGestureRecognizer: rotate];} # pragma mark-important ~~~~ Gesture monitoring method // Pinch pinch (zoom)-(void) pinching :( UIPinchGestureRecognizer *) pinch {// x y scales Pinch proportionally. view. transform = CGAffineTransformScale (pinch. view. transform, pinch. scale, pinch. scale); // returns to zero every time ~~~ Pinch. scale = 1; // This is really important !!!!!} // Rotate gesture-(void) rotating :( UIRotationGestureRecognizer *) rotate {// rotate according to the Rotation Angle of the gesture reader. view. transform = CGAffineTransformRotate (rotate. view. transform, rotate. rotation); // returns to zero every time ~~~~ Rotate. rotation = 0; // This is very important !!!!!} # Pragma mark-proxy method of the gesture Reader/*** whether multiple gesture identifiers are allowed to work Simultaneously * Simultaneously: Simultaneously */-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer failed :( UIGestureRecognizer *) failed {return YES;} # pragma mark-connection method-(IBAction) dismiss {[self dismissViewControllerAnimated: YES completion: nil];} @ end





Graffiti

Finally:




Controller

//// PaintController. m // 38 _ gesture _ graffiti /// Created by beyond on 14-9-17. // Copyright (c) 2014 com. beyond. all rights reserved. // graffiti controller # import "PaintController. h "// Canvas # import" Canvas. h "@ interface PaintController () // Canvas @ property (weak, nonatomic) IBOutlet Canvas * canvasView; // clear the Canvas-(IBAction) clear; // revoke-(IBAction) undo; // save to album-(IBAction) save; @ end @ implementation PaintController // clear artboard-(IBAction) clear {[self. canvasView clear];} // undo-(IBAction) undo {[self. canvasView undo];} // save to album-(IBAction) save {// 1. UIImage * image = [UIImage captureWithView: self. canvasView]; // 2. save to image. We recommend that you use the recommended callback method UIImageWriteToSavedPhotosAlbum (image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil );} // after saving the image, this method will be called-(void) image :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo {if (error) {// failed to save [MBProgressHUD showError: @ "failed to save"];} else {// saved successfully [MBProgressHUD showSuccess: @ "saved successfully"];} @ end


Custom View: canvas

//// Canvas. h // 38 _ gesture _ graffiti /// Created by beyond on 14-9-17. // Copyright (c) 2014 com. beyond. all rights reserved. // canvas # import <UIKit/UIKit. h> @ interface Canvas: UIView // clear the Canvas-(void) clear; // undo the previous stroke-(void) undo; @ end


//// Canvas. m // 38 _ gesture _ graffiti /// Created by beyond on 14-9-17. // Copyright (c) 2014 com. beyond. all rights reserved. // Canvas, core code # import "Canvas. h "@ interface Canvas () // stores the beisel path object array. Each touchBegin corresponds to a new path @ property (nonatomic, strong) NSMutableArray * pathArr; @ end @ implementation Canvas # pragma mark-lazy loading-(NSMutableArray *) pathArr {if (_ pathArr = nil) {_ pathArr = [NSMutableArray array];} return _ pathArr;} // clear the canvas-(void) clear {[self. pathArr removeAllObjects]; [self setNeedsDisplay];} // undo the previous stroke-(void) undo {[self. pathArr removeLastObject]; [self setNeedsDisplay];} # pragma mark-Touch method // each touchBegin corresponds to a new path object-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// 1. obtain the current touch point UITouch * touch = [touches anyObject]; CGPoint startPos = [touch locationInView: touch. view]; // 2. create a new path UIBezierPath * currenPath = [UIBezierPath bezierPath]; currenPath. lineCapStyle = kCGLineCapRound; currenPath. lineJoinStyle = kCGLineJoinRound; // set the starting point [currenPath moveToPoint: startPos]; // 3. add the path to the array [self. pathArr addObject: currenPath]; [self setNeedsDisplay];} // retrieve the latest besell path and add a new vertex-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {UITouch * touch = [touches anyObject]; CGPoint pos = [touch locationInView: touch. view]; UIBezierPath * currentPath = [self. pathArr lastObject]; [currentPath addLineToPoint: pos]; [self setNeedsDisplay];} // same as move-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {[self touchesMoved: touches withEvent: event] ;}# pragma mark-draw line-(void) drawRect :( CGRect) rect {[[UIColor redColor] set]; for (UIBezierPath * path in self. pathArr) {path. lineWidth = 10; [path stroke] ;}# pragma mark-Test C draw line code-(void) testPath {CGContextRef ctx = UIGraphicsGetCurrentContext (); // CGContextMoveToPoint (ctx, 0, 0); // CGContextAddLineToPoint (ctx, 100,100); // CGContextStrokePath (ctx); CGMutablePathRef path = CGPathCreateMutable (); CGPathMoveToPoint (path, NULL, 0, 0 ); CGPathAddLineToPoint (path, NULL, 100,100); CGContextAddPath (ctx, path); CGMutablePathRef path2 = forward (); CGPathMoveToPoint (path2, NULL, 250,250); CGPathAddLineToPoint (path2, NULL, 110,100); CGContextAddPath (ctx, path2); CGContextStrokePath (ctx); CGPathRelease (path) ;}@ end


Category

/// UIImage + ScreenShot. m // 38 _ gesture /// Created by beyond on 14-9-17. // Copyright (c) 2014 com. beyond. all rights reserved. // category, # import "UIImage + ScreenShot. h "@ implementation UIImage (ScreenShot) + (instancetype) captureWithView :( UIView *) view {// 1. enable context uigraphicsbeginimagecontextwitexceptions (view. frame. size, NO, 0.0); // 2. render the layer of the controller view to the context [view. layer renderInContext: UIGraphicsGetCurrentContext ()]; // 3. retrieve image UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); // 4. end context UIGraphicsEndImageContext (); return newImage ;}@ end









































How to Use ios5 gesture operations

It is absolutely useful. When your home key fails, it is all done by hand to free up the home key. The home Key is too easy to break down or fail.

By capturing the screen, the program will exit and return to the main interface.

What is the use of the Creation gesture on iOS?

For normal people, this is a real HOME, right? I don't mean to use it...
 

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.