[iOS UI进阶
1 - (void) testTap { 2 // 创建手势识别器 3 UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)]; 4 tapRec.numberOfTapsRequired = 3; // 触发需要点击的次数 5 tapRec.numberOfTouchesRequired = 2; // 触发需要同时点击的点数目 6 7 // 配置手势识别器到控件 8 [self.hvwView addGestureRecognizer: tapRec]; 9 } 10 11 /** tap手势的事件处理方法 */ 12 - (void) tapRun:(UITapGestureRecognizer *) tapRec { 13 NSLog(@"tapRun"); 14 }
1 - (void) testLongPress { 2 UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)]; 3 4 longRec.minimumPressDuration = 2; // 长按生效时间 5 longRec.allowableMovement = 50; // 长按允许移动范围,单位:px 6 7 [self.hvwView addGestureRecognizer:longRec]; 8 }
1 typedef enum { 2 UISwipeGestureRecognizerDirectionRight = 1 << 0, 3 UISwipeGestureRecognizerDirectionLeft = 1 << 1, 4 UISwipeGestureRecognizerDirectionUp = 1 << 2, 5 UISwipeGestureRecognizerDirectionDown = 1 << 3 6 } UISwipeGestureRecognizerDirection;
1 - (void) testSwipe { 2 UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)]; 3 swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向 4 5 [self.hvwView addGestureRecognizer:swipeRec]; 6 }
1 - (void) testPinch { 2 UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)]; 3 4 [self.hvwView addGestureRecognizer:rec]; 5 } 6 7 - (void) pinchRun:(UIPinchGestureRecognizer *) rec { 8 rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale); 9 10 // 一定要复位!!!不然按住捏合会不断叠加倍数 11 rec.scale = 1; 12 }
1 - (void) testRotation { 2 UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)]; 3 4 [self.hvwView addGestureRecognizer:rotationRec]; 5 } 6 7 - (void) rotationRun:(UIRotationGestureRecognizer *) rec { 8 rec.view.transform = CGAffineTransformRotate(rec.view.transform, rec.rotation); 9 10 // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation 11 rec.rotation = 0; 12 }
1 - (void) testPan { 2 UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)]; 3 4 [self.hvwView addGestureRecognizer:rec]; 5 } 6 7 - (void) panRun:(UIPanGestureRecognizer *) rec { 8 // 取得拖曳距离 9 CGPoint movedDistance = [rec translationInView:rec.view]; 10 CGPoint viewCenter = rec.view.center; 11 viewCenter.x += movedDistance.x; 12 viewCenter.y += movedDistance.y; 13 rec.view.center = viewCenter; 14 15 // 复位拖曳距离 16 [rec setTranslation:CGPointZero inView:rec.view]; 17 }
1 @interface ViewController () <UIGestureRecognizerDelegate>
1 rec.delegate = self;
1 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 2 3 // 任何两种手势识别器都能同时使用 4 return YES; 5 }
1 // 2 // ViewController.m 3 // GestureRecognizerTest 4 // 5 // Created by hellovoidworld on 15/1/13. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () <UIGestureRecognizerDelegate> 12 13 @property (weak, nonatomic) IBOutlet UIView *hvwView; 14 15 @end 16 17 @implementation ViewController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view, typically from a nib. 22 23 // [self testTap]; 24 // [self testLongPress]; 25 // [self testSwipe]; 26 [self testRotation]; 27 [self testPinch]; 28 [self testPan]; 29 } 30 31 - (void) testTap { 32 // 创建手势识别器 33 UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)]; 34 tapRec.numberOfTapsRequired = 3; // 触发需要点击的次数 35 tapRec.numberOfTouchesRequired = 2; // 触发需要同时点击的点数目 36 37 // 配置手势识别器到控件 38 [self.hvwView addGestureRecognizer: tapRec]; 39 } 40 41 /** tap手势的事件处理方法 */ 42 - (void) tapRun:(UITapGestureRecognizer *) tapRec { 43 NSLog(@"tapRun"); 44 } 45 46 - (void) testLongPress { 47 UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)]; 48 49 longRec.minimumPressDuration = 2; // 长按生效时间 50 longRec.allowableMovement = 50; // 长按允许移动范围,单位:px 51 52 [self.hvwView addGestureRecognizer:longRec]; 53 } 54 55 - (void) longPressRun:(UILongPressGestureRecognizer *) rec { 56 NSLog(@"longPress"); 57 } 58 59 - (void) testSwipe { 60 UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)]; 61 swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向 62 63 [self.hvwView addGestureRecognizer:swipeRec]; 64 } 65 66 - (void) swipeRun:(UISwipeGestureRecognizer *) rec { 67 NSLog(@"swipe"); 68 } 69 70 - (void) testRotation { 71 UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)]; 72 rotationRec.delegate = self; 73 74 [self.hvwView addGestureRecognizer:rotationRec]; 75 } 76 77 - (void) rotationRun:(UIRotationGestureRecognizer *) rec { 78 rec.view.transform = CGAffineTransformRotate(self.hvwView.transform, rec.rotation); 79 80 // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation 81 rec.rotation = 0; 82 } 83 84 - (void) testPinch { 85 UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)]; 86 rec.delegate = self; 87 88 [self.hvwView addGestureRecognizer:rec]; 89 } 90 91 - (void) pinchRun:(UIPinchGestureRecognizer *) rec { 92 rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale); 93 94 // 一定要复位!!!不然按住捏合会不断叠加倍数 95 rec.scale = 1; 96 } 97 98 - (void) testPan { 99 UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)]; 100 rec.delegate = self; 101 102 [self.hvwView addGestureRecognizer:rec]; 103 } 104 105 - (void) panRun:(UIPanGestureRecognizer *) rec { 106 // 取得拖曳距离 107 CGPoint movedDistance = [rec translationInView:rec.view]; 108 CGPoint viewCenter = rec.view.center; 109 viewCenter.x += movedDistance.x; 110 viewCenter.y += movedDistance.y; 111 rec.view.center = viewCenter; 112 113 // 复位拖曳距离 114 [rec setTranslation:CGPointZero inView:rec.view]; 115 } 116 117 - (void)didReceiveMemoryWarning { 118 [super didReceiveMemoryWarning]; 119 // Dispose of any resources that can be recreated. 120 } 121 122 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 123 124 // 任何两种手势识别器都能同时使用 125 return YES; 126 } 127 128 @end