Gestures, gesture pictures
Effect
Details
1. UITouch
# Import "ViewController_0.h" @ interface ViewController_0 () @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_0-(void) viewDidLoad {[super viewDidLoad. label = [[UILabel alloc] initWithFrame: CGRectMake (100,100,100,100)]; self. label. backgroundColor = [UIColor yellowColor]; self. label. layer. borderWidth = 1; [self. view addSubview: self. label]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "drag box"; [textlabel sizeToFit]; textlabel. font = [UIFont systemFontOfSize: 12]; [self. view addSubview: textlabel];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( nullable UIEvent *) event {// 1. get the gesture UITouch * touch = [touches anyObject]; // 2. get the coordinate CGPoint point = [touch locationInView: self. view]; // 3. let the label get the coordinate self. label. center = point; NSLog (@ "1. finger touch to screen ");}-(void) touchesMoved :( NSSet <UITouch *> *) touches withEvent :( nullable UIEvent *) event {// 1. get the gesture UITouch * touch = [touches anyObject]; // 2. get the coordinate CGPoint point = [touch locationInView: self. view]; // 3. let the label get the coordinate self. label. center = point; NSLog (@ "2. move your finger on the screen ");}-(void) touchesEnded :( NSSet <UITouch *> *) touches withEvent :( nullable UIEvent *) event {NSLog (@" 3. finger just leaves the screen ");}-(void) touchesCancelled :( NSSet <UITouch *> *) touches withEvent :( nullable UIEvent *) event {NSLog (@" 4. gesture failed ");} @ end
2. UITapGestureRecognizer
# Import "ViewController_1.h" @ interface ViewController_1 () @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_1-(void) viewDidLoad {[super viewDidLoad]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "the operation of the tap gesture alt + shift on the computer requires two consecutive clicks"; [textlabel sizeToFit]; textlabel. font = [UIFont systemFontOfSize: 12]; [self. view addSubview: textlabel]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (100,150,100,100)]; self. label. backgroundColor = [UIColor orangeColor]; self. label. userInteractionEnabled = YES; self. label. text = @ "0"; self. label. textAlignment = NSTextAlignmentCenter; [self. view addSubview: self. label]; // 1. create a tap gesture UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (labelTap :)]; tap. numberOfTapsRequired = 2; // The number of clicks required. numberOfTouchesRequired = 2; // number of required fingers: 2 fingers alt + shift need to match the number of clicks twice (in fact, use the default one directly) [self. label addGestureRecognizer: tap];}-(void) labelTap :( UITapGestureRecognizer *) tap {int num = [self. label. text intValue]; num ++; self. label. text = [NSString stringWithFormat: @ "% d", num] ;}@ end
3. UILongPressGestureRecognizer
# Import "ViewController_2.h" @ interface ViewController_2 () <strong> @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_2-(void) viewDidLoad {[super viewDidLoad]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "long-pressed gesture"; [textlabel sizeToFit]; textlabel. font = [UIFont systemFontOfSize: 12]; [self. view addSubview: textlabel]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (40,150,200,150)]; self. label. backgroundColor = [UIColor grayColor]; self. label. userInteractionEnabled = YES; self. label. text = @ "0"; self. label. textAlignment = NSTextAlignmentCenter; [self. view addSubview: self. label]; UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPressAction :)]; longPress. numberOfTapsRequired = 1; longPress. numberOfTouchesRequired = 1; longPress. minimumPressDuration = 1.0; longPress. delegate = self; [self. label addGestureRecognizer: longPress];}-(void) longPressAction :( UILongPressGestureRecognizer *) longPress {if (longPress. state = UIGestureRecognizerStateBegan) {NSLog (@ "Starting gesture status");} else if (longPress. state = UIGestureRecognizerStateEnded) {NSLog (@ "gesture status ends");} else if (longPress. state = UIGestureRecognizerStateChanged) {NSLog (@ "gesture status changed"); NSInteger num = [self. label. text integerValue]; num ++; self. label. text = [NSString stringWithFormat: @ "% ld", (long) num] ;}}@ end
4. UISwipeGestureRecognizer
# Import "ViewController_3.h" @ interface ViewController_3 () @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_3-(void) viewDidLoad {[super viewDidLoad]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "swipe gesture slide to the right 1, you can also set the left to move up and down"; textlabel. numberOfLines = 0; textlabel. font = [UIFont systemFontOfSize: 12]; [self. view addSubview: textlabel]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (100,150,100,100)]; self. label. backgroundColor = [UIColor grayColor]; self. label. userInteractionEnabled = YES; self. label. text = @ "0"; self. label. textAlignment = NSTextAlignmentCenter; [self. view addSubview: self. label]; UISwipeGestureRecognizer * swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipeAction :)]; swipeGesture. direction = UISwipeGestureRecognizerDirectionRight; // The default value is to drag [self. label addGestureRecognizer: swipeGesture];}-(void) swipeAction :( UISwipeGestureRecognizer *) swipe {if (swipe. direction = uiswipegesturerecognizerdireleft) {NSLog (@ "");} else if (swipe. direction = uiswipegesturerecognizerdireright) {NSLog (@ ""); NSInteger num = [self. label. text integerValue]; num ++; self. label. text = [NSString stringWithFormat: @ "% ld", (long) num];} else if (swipe. direction = uiswipegesturerecognizerdireup up) {NSLog (@ "current response gesture");} else if (swipe. direction = UISwipeGestureRecognizerDirectionDown) {NSLog (@ ""); }}@ end
5. UIPanGestureRecognizer
# Import "ViewController_4.h" @ interface ViewController_4 () @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_4-(void) viewDidLoad {[super viewDidLoad]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "pan gesture, drag the square"; [textlabel sizeToFit]; textlabel. font = [UIFont systemFontOfSize: 12]; [self. view addSubview: textlabel]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (100,100,100,100)]; self. label. backgroundColor = [UIColor grayColor]; self. label. userInteractionEnabled = YES; self. label. text = @ "0"; self. label. textAlignment = NSTextAlignmentCenter; [self. view addSubview: self. label]; UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panAction :)]; [self. label addGestureRecognizer: pan];}-(void) panAction :( UIPanGestureRecognizer *) pan {CGPoint offset = [pan locationInView: self. view]; self. label. center = offset;} @ end
6. UIRotationGestureRecognizer
# Import "ViewController_5.h" @ interface ViewController_5 () @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_5-(void) viewDidLoad {[super viewDidLoad]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "When testing a rotation gesture in the simulator, press and hold the option key, and then use the touchpad or mouse to operate"; textlabel. font = [UIFont systemFontOfSize: 12]; textlabel. numberOfLines = 0; [self. v Iew addSubview: textlabel]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (40,200,200, 50)]; self. label. backgroundColor = [UIColor grayColor]; self. label. userInteractionEnabled = YES; self. label. text = @ "we are friends"; self. label. textAlignment = NSTextAlignmentCenter; [self. view addSubview: self. label]; // (when the simulator tests the pinch and rotate gestures, press and hold the option key, and then use the touchpad or mouse to operate) UIRotationGestureRecognizer * rotation = [UI RotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotationAction :)]; [self. view addGestureRecognizer: rotation];}-(void) rotationAction :( UIRotationGestureRecognizer *) rotation {self. label. transform = CGAffineTransformRotate (self. label. transform, rotation. rotation); rotation. rotation = 0; // This is very important !!!!! // Static float orginState; // self. label. transform = CGAffineTransformMakeRotation (rotation. rotation + orginState); // if (rotation. state = UIGestureRecognizerStateEnded) {// orginState = orginState + rotation. state; // self. label. transform = CGAffineTransformMakeRotation (rotation. rotation); //} @ end
7. UIPinchGestureRecognizer
# Import "ViewController_6.h" @ interface ViewController_6 () @ property (nonatomic, strong) UILabel * label; @ end @ implementation ViewController_6-(void) viewDidLoad {[super viewDidLoad]; UILabel * textlabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 80,200, 45)]; textlabel. text = @ "when the simulator tests a pinch gesture, press and hold the option key, and then use the touchpad or mouse to operate"; textlabel. font = [UIFont systemFontOfSize: 12]; textlabel. numberOfLines = 0; [self. view addSubview: textlabel]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (100,250, 80, 80)]; self. label. backgroundColor = [UIColor grayColor]; self. label. userInteractionEnabled = YES; self. label. text = @ "0"; self. label. textAlignment = NSTextAlignmentCenter; [self. view addSubview: self. label]; // (when the simulator tests the pinch and rotate gesture, press and hold the option key and then use the touchpad or mouse) UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: @ selector (pichGesture :)]; [self. view addGestureRecognizer: pinch];}-(void) pichGesture :( UIPinchGestureRecognizer *) pinch {static float originScale = 1; // scale returned by gesture scaling is relative to the last self. label. transform = CGAffineTransformMakeScale (pinch. scale * originScale, pinch. scale * originScale); if (pinch. state = UIGestureRecognizerStateEnded) {originScale = originScale * pinch. scale; }}@ end