IOS learning 35th-day notes (Touch gesture Introduction), ios 35th-day
I. Touch gestures
1. Use gestures to implement UIButton mobile effect instance code
1) create a class MyButton. h code that inherits from UIButton.
1 #import <UIKit/UIKit.h>2 @interface MyButton : UIButton3 @end
2) code implementation of MyButton. m
1 # import "MyButton. h "2 @ implementation MyButton 3 {4 CGPoint _ lastPoint; 5} 6 7 // gesture start 8-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event 9 {10 UITouch * touch = [touches anyObject]; 11 CGPoint point = [touch locationInView: self]; 12 NSLog (@ "began: % @", NSStringFromCGPoint (point); 13 _ lastPoint = point; 14} 15 16-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event17 {18 UITouch * touch = [touches anyObject]; 19 CGPoint point = [touch locationInView: self]; 20 CGFloat offsetx = point. x-_ lastPoint. x; 21 CGFloat offsety = point. y-_ lastPoint. y; 22 self. center = CGPointMake (self. center. x + offsetx, self. center. y + offsety); 23 NSLog (@ "moved: % @", NSStringFromCGPoint (point); 24} 25 26-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event27 {28 UITouch * touch = [touches anyObject]; 29 CGPoint point = [touch locationInView: self]; 30 NSLog (@ "end: % @", NSStringFromCGPoint (point); 31} 32 @ end
3) code implementation in the parent View
1 # import "ViewController. h "2 # import" MyButton. h "3 @ interface ViewController () 4 {5 MyButton * _ v; 6 CGPoint _ lastPoint; 7} 8 @ end 9 10 @ implementation ViewController11 12-(void) viewDidLoad {13 [super viewDidLoad]; 14 _ v = [[MyButton alloc] initWithFrame: CGRectMake (100,100,100,100)]; 15 _ v. backgroundColor = [UIColor redColor]; 16 [self. view addSubview: _ v]; 17} 18 19 // gesture start 20-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event21 {22 UITouch * touch = [touches anyObject]; 23 CGPoint point = [touch locationInView: self. view]; 24 NSLog (@ "began: % @", NSStringFromCGPoint (point); 25 _ lastPoint = point; 26} 27 28-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event29 {30 UITouch * touch = [touches anyObject]; 31 CGPoint point = [touch locationInView: self. view]; 32 CGFloat offsetx = point. x-_ lastPoint. x; 33 CGFloat offsety = point. y-_ lastPoint. y; 34 _ v. center = CGPointMake (_ v. center. x + offsetx, _ v. center. y + offsety); 35 _ lastPoint = point; 36 NSLog (@ "moved: % @", NSStringFromCGPoint (point); 37} 38 39-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event40 {41 UITouch * touch = [touches anyObject]; 42 CGPoint point = [touch locationInView: self. view]; 43 NSLog (@ "end: % @", NSStringFromCGPoint (point); 44} 45 46-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event47 {48 49} 50 @ end
2. Use Touch gestures to scale and rotate controls. Example code
1 # import "ViewController. h "2 // follow the rotation and scaling proxy Protocol 3 @ interface ViewController () <UIGestureRecognizerDelegate> 4 @ end 5 6 @ implementation ViewController 7 8-(void) viewDidLoad {9 [super viewDidLoad]; 10 11 UIImageView * imgv = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0,200,200)]; 12 imgv. center = self. view. center; 13 [self. view addSubview: imgv]; 14 imgv. image = [UIImage imageNamed: @ "3"]; 15 imgv. userInteractionEnabled = YES; 16 17 // click gesture 18 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (GES tap:)]; 19 20 // set the hand index 21 tap required for this gesture. numberOfTouchesRequired = 2; 22 23 // set the number of clicks for this gesture 24 tap. numberOfTapsRequired = 4; 25 [imgv addGestureRecognizer: tap]; 26 27 // pan gesture, drag gesture 28 UIPanGestureRecognizer * pan = [UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGes :)]; 29 [imgv addGestureRecognizer: pan]; 30 31 // scaling gesture 32 ''* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinchGes :)]; 33 [imgv addGestureRecognizer: pinch]; 34 pinch. delegate = self; 35 36 // rotate 37 bytes * rotation = [[externalloc] initWithTarget: self action: @ selector (rotationGes :)]; 38 [imgv addGestureRecognizer: rotation]; 39 rotation. delegate = self; 40} 41 42 // The returned value indicates whether other (relative to a proxy gesture already set) gestures can be recognized simultaneously. 43-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer identifier:
(UIGestureRecognizer *) otherGestureRecognizer44 {45 return YES; 46} 47-(void) rotationGes :( UIRotationGestureRecognizer *) rotation48 {49 rotation. view. transform = CGAffineTransformRotate (rotation. view. transform, rotation. rotation); 50 rotation. rotation = 0.0; 51} 52-(void) pinchGes :( UIPinchGestureRecognizer *) pinch53 {54 // transform: affine transformation 55 // pinch. scale: the kneading rate of the scaling gesture is 56 pinch. view. transform = CGAffineTransformScale (pinch. view. transform, pinch. scale, pinch. scale); 57 58 // rate reduction 59 pinch. scale = 1.0; 60} 61-(void) panGes :( UIPanGestureRecognizer *) pan62 {63 // return the current gesture offset 64 CGPoint offset = [pan translationInView: pan. view]; 65 // pan. view is the view added by the pan gesture 66 pan. view. center = CGPointMake (pan. view. center. x + offset. x, pan. view. center. y + offset. y); 67 // After moving, return the offset to 068 [pan setTranslation: CGPointZero inView: pan. view]; 69} 70 71-(void) tapGes :( UIGestureRecognizer *) tap72 {73 NSLog (@ "=========== "); 74} 75 @ end