标签:
一、iOS下的手势
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @property (weak, nonatomic) IBOutlet UILabel *genstureLabel; 6 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 16 [self tapGesture]; 17 18 [self SwipeGesture]; 19 20 [self PinchGesture]; 21 22 [self PanGesture]; 23 24 [self RotationGesture]; 25 26 [self LongPressGesture]; 27 } 28 29 /* 30 * UIGestureRecognizer 定义所有手势的基本行为 31 * 32 * UITapGestureRecognizer Tap(点击) 33 * UIPinchGestureRecognizer Pinch(捏合-->放大缩小) 34 * UIRotationGestureRecognizer Rotation(旋转) 35 * UISwipeGestureRecognizer Swipe(轻扫,快速移动,是用于监测滑动的方向的) 36 * UIPanGestureRecognizer Pan(拖拽,慢速移动,是用于监测偏移量的) 37 * UILongPressGestureRecognizer LongPress(长按) 38 */ 39 40 41 #pragma mark - UITapGestureRecognizer Tap(点击) 42 - (void)tapGesture{ 43 /* 一个手指单击的方法*/ 44 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)]; 45 tap.numberOfTapsRequired = 1;//点击的次数 46 tap.numberOfTouchesRequired = 1;//手指的个数 47 48 [self.view addGestureRecognizer:tap]; 49 50 /* 一个手指双击的方法*/ 51 UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)]; 52 tap2.numberOfTapsRequired = 2;//点击的次数 53 tap2.numberOfTouchesRequired = 1;//手指的个数 54 55 [self.view addGestureRecognizer:tap2]; 56 57 /* 58 * 手势识别是遵循互斥的原则的,当识别为一种手势时,后面的手势就不会被识别了,这时我们可以用到下面的方法 59 /* 当A手势发生时,即便A已经满足条件,但要等到B手势失效后才触发 60 /* [A requireGestureRecognizerToFail:B]; 61 */ 62 [tap requireGestureRecognizerToFail:tap2]; 63 64 65 /* 两个手指单击*/ 66 UITapGestureRecognizer * tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTwoFingerTap:)]; 67 tap3.numberOfTapsRequired = 1;//点击的次数 68 tap3.numberOfTouchesRequired = 2;//手指的个数 69 70 [tap2 requireGestureRecognizerToFail:tap3]; 71 72 [self.view addGestureRecognizer:tap3]; 73 } 74 75 76 #pragma mark - UISwipeGestureRecognizer Swipe(轻扫,快速移动,是用于监测滑动的方向的) 77 - (void)SwipeGesture{ 78 UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeAction:)]; 79 /* 80 * 轻扫的方向,有上下左右四种,默认为向右 81 * 82 * UISwipeGestureRecognizerDirectionRigh 83 * 84 * UISwipeGestureRecognizerDirectionLeft 85 * 86 * UISwipeGestureRecognizerDirectionUp 87 * 88 * UISwipeGestureRecognizerDirectionDown 89 */ 90 swipe.direction = UISwipeGestureRecognizerDirectionLeft; 91 [self.view addGestureRecognizer:swipe]; 92 } 93 94 #pragma mark - UIPinchGestureRecognizer Pinch(捏合-->放大缩小) 95 - (void)PinchGesture{ 96 UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(PinchActon:)]; 97 /* 98 * UIPinchGestureRecognizer 有两个属性 99 * 100 * CGFloat scale; 放大缩小的比例 101 * 102 * CGFloat velocity; 放大缩小的速度 103 */ 104 [self.view addGestureRecognizer:pinch]; 105 } 106 107 #pragma mark - UIPanGestureRecognizer Pan(拖拽,慢速移动,是用于监测偏移量的) 108 - (void)PanGesture{ 109 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(PanAction:)]; 110 111 /* 112 * UIPanGestureRecognizer 的两个属性 113 * minimumNumberOfTouches -- >最小的Touches对象 114 * maximumNumberOfTouches -- >最大的Touches对象 115 * 116 * UIPanGestureRecognizer 的三个方法 117 * 返回指定的View的坐标 118 * - (CGPoint)translationInView:(nullable UIView *)view; 119 * 设定坐标值 120 * - (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view; 121 * 计算移动的速度 单位时像素每秒 122 * - (CGPoint)velocityInView:(nullable UIView *)view; 123 */ 124 self.genstureLabel.userInteractionEnabled = YES; 125 [self.genstureLabel addGestureRecognizer:pan]; 126 } 127 128 #pragma mark - UIRotationGestureRecognizer Rotation(旋转) 129 - (void)RotationGesture{ 130 UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(RotationAction:)]; 131 132 /* 133 * UIRotationGestureRecognizer 只有两个属性 134 * CGFloat rotation; 旋转弧度 135 * CGFloat velocity;旋转速度 弧度每秒 136 * 137 */ 138 [self.view addGestureRecognizer:rotation]; 139 } 140 141 #pragma mark - UILongPressGestureRecognizer LongPress(长按) 142 - (void)LongPressGesture{ 143 UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressAction:)]; 144 //numberOfTapsRequired 点击次数,默认时0 145 //numberOfTouchesRequired 手指数,默认为1 146 //minimumPressDuration 最短时间,默认0.5秒 147 //allowableMovement 允许运动的次数 148 longPress.minimumPressDuration = 2; 149 [self.view addGestureRecognizer:longPress]; 150 } 151 152 153 154 #pragma mark - 响应方法 155 - (void)SingleTap:(UITapGestureRecognizer *)tap{ 156 self.genstureLabel.text = @"一个手指单击"; 157 } 158 159 - (void)DoubleTap:(UITapGestureRecognizer *)tap{ 160 self.genstureLabel.text = @"一个手指双击"; 161 } 162 163 - (void)SingleTwoFingerTap:(UITapGestureRecognizer *)tap{ 164 self.genstureLabel.text = @"两个手指单击"; 165 } 166 167 - (void)SwipeAction:(UISwipeGestureRecognizer *)swipe{ 168 self.genstureLabel.text = @"向左轻扫"; 169 } 170 171 - (void)PinchActon:(UIPinchGestureRecognizer *)pinch{ 172 float scale = pinch.scale; 173 /* 174 * 参数1:是视图本身的大小 175 * 176 * 参数2:是X方向的缩放比例 177 * 178 * 参数3:是Y方向的缩放比例 179 */ 180 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale); 181 182 if (scale > 1) { 183 self.genstureLabel.text = @"捏合放大"; 184 }else if (scale < 1){ 185 self.genstureLabel.text = @"捏合缩小"; 186 } 187 } 188 189 - (void)PanAction:(UIPanGestureRecognizer *)pan{ 190 191 //移动的坐标值 192 CGPoint translation = [pan translationInView:self.view]; 193 //移动后的坐标 194 pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y); 195 196 //设置坐标和速度 197 [pan setTranslation:CGPointZero inView:self.view]; 198 199 self.genstureLabel.text = @"把我放哪"; 200 201 } 202 203 - (void)RotationAction:(UIRotationGestureRecognizer *)rotation{ 204 205 rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation); 206 rotation.rotation = M_PI; 207 _genstureLabel.text = @"旋转"; 208 209 } 210 - (void)LongPressAction:(UILongPressGestureRecognizer *)longPress{ 211 212 self.genstureLabel.text = @"长按2秒"; 213 214 } 215 216 217 - (void)didReceiveMemoryWarning { 218 [super didReceiveMemoryWarning]; 219 220 } 221 222 @end
二、手势密码
敬请期待...
标签:
原文地址:http://www.cnblogs.com/HOYF/p/5433411.html