手势识别
1 - (void)viewDidLoad 2 3 { 4 5 [superviewDidLoad]; 6 7 UIImage *image =[UIImage imageNamed:@"001.jpg"]; 8 9 UIImageView *imageView=[[UIImageView alloc]initWithImage:image]; 10 11 [imageView setFrame:kImageFrame]; 12 13 [imageView setUserInteractionEnabled:YES]; 14 15 16 17 [self.view addSubview:imageView]; 18 19 self.imageView = imageView; 20 21 //点按手势 22 23 UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)]; 24 25 //单击操作 26 27 [tap setNumberOfTapsRequired:1]; 28 29 //一个手指操作 30 31 [tap setNumberOfTouchesRequired:1]; 32 33 //[self.imageView addGestureRecognizer:tap]; 34 35 36 37 //长按手势 38 39 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)]; 40 41 42 43 //[self.imageView addGestureRecognizer:longPress]; 44 45 46 47 //旋转手势 48 49 UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotate:)]; 50 51 //[self.imageView addGestureRecognizer:rotate]; 52 53 54 55 //拖动手势 56 57 58 59 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)]; 60 61 62 63 //[self.imageView addGestureRecognizer:pan]; 64 65 66 67 //捏合手势 68 69 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)]; 70 71 72 73 //[self.imageView addGestureRecognizer:pinch]; 74 75 76 77 //轻扫手势 78 79 UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip:)]; 80 81 [swip1 setDirection:UISwipeGestureRecognizerDirectionRight]; 82 83 [self.viewaddGestureRecognizer:swip1]; 84 85 86 87 UISwipeGestureRecognizer *swip2 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip:)]; 88 89 [swip2 setDirection:UISwipeGestureRecognizerDirectionLeft]; 90 91 [self.viewaddGestureRecognizer:swip2]; 92 93 94 95 UISwipeGestureRecognizer *swip3 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip:)]; 96 97 [swip3 setDirection:UISwipeGestureRecognizerDirectionDown]; 98 99 [self.viewaddGestureRecognizer:swip3]; 100 101 102 103 UISwipeGestureRecognizer *swip4 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip:)]; 104 105 [swip4 setDirection:UISwipeGestureRecognizerDirectionUp]; 106 107 [self.viewaddGestureRecognizer:swip4]; 108 109 110 111 112 113 } 114 115 116 117 #pragma mark 轻扫手势 118 119 - (void) swip:(UISwipeGestureRecognizer *)recognizer 120 121 { 122 123 CGRect frame = kImageFrame; 124 125 if(UISwipeGestureRecognizerDirectionLeft == recognizer.direction){ 126 127 frame.origin.x-=300; 128 129 }elseif(UISwipeGestureRecognizerDirectionRight==recognizer.direction){ 130 131 frame.origin.x+=300; 132 133 }elseif(UISwipeGestureRecognizerDirectionUp == recognizer.direction){ 134 135 frame.origin.y -=400; 136 137 }else{ 138 139 frame.origin.y +=400; 140 141 } 142 143 [UIViewanimateWithDuration:1.0fanimations:^{ 144 145 [self.imageView setFrame:frame]; 146 147 }completion:^(BOOL finished) { 148 149 [self.imageView setFrame:kImageFrame]; 150 151 }]; 152 153 } 154 155 156 157 #pragma mark 捏合手势 158 159 - (void) pinch :(UIPinchGestureRecognizer *)recognizer 160 161 { 162 163 if(UIGestureRecognizerStateChanged == recognizer.state){ 164 165 [UIViewanimateWithDuration:1.0fanimations:^{ 166 167 168 169 [recognizer.view setTransform:CGAffineTransformMakeScale(recognizer.scale, recognizer.scale)]; 170 171 }]; 172 173 }elseif(UIGestureRecognizerStateEnded == recognizer.state){ 174 175 [UIViewanimateWithDuration:0.5fanimations:^{ 176 177 [recognizer.viewsetTransform:CGAffineTransformIdentity]; 178 179 }]; 180 181 } 182 183 } 184 185 186 187 188 189 #pragma mark 拖动手势 190 191 192 193 - (void) pan:(UIPanGestureRecognizer *)recognizer 194 195 { 196 197 if(UIGestureRecognizerStateChanged == recognizer.state){ 198 199 [UIViewanimateWithDuration:1.0fanimations:^{ 200 201 CGPoint deltaPoint = [recognizer translationInView:self.view]; 202 203 [recognizer.view setTransform:CGAffineTransformMakeTranslation(deltaPoint.x, deltaPoint.y)]; 204 205 }]; 206 207 }elseif(UIGestureRecognizerStateEnded == recognizer.state){ 208 209 [UIViewanimateWithDuration:0.5fanimations:^{ 210 211 [recognizer.viewsetTransform:CGAffineTransformIdentity]; 212 213 }]; 214 215 } 216 217 218 219 } 220 221 222 223 # pragma mark 旋转手势 224 225 226 227 - (void) rotate:(UIRotationGestureRecognizer *) recognizer 228 229 { 230 231 if (UIGestureRecognizerStateChanged == recognizer.state) { 232 233 [UIViewanimateWithDuration:1.0fanimations:^{ 234 235 [recognizer.view setTransform:CGAffineTransformMakeRotation(recognizer.rotation)]; 236 237 }]; 238 239 }elseif(UIGestureRecognizerStateEnded == recognizer.state) 240 241 { 242 243 [UIViewanimateWithDuration:0.5fanimations:^{ 244 245 [recognizer.viewsetTransform:CGAffineTransformIdentity]; 246 247 }]; 248 249 } 250 251 } 252 253 254 255 #pragma mark 长按手势 256 257 - (void) longPress :(UILongPressGestureRecognizer *)recognizer 258 259 { 260 261 //长按手势属于连续手势,需要处理状态 262 263 if(UIGestureRecognizerStateBegan==recognizer.state){ 264 265 [UIViewanimateWithDuration:1.0fanimations:^{ 266 267 [recognizer.viewsetTransform:CGAffineTransformMakeRotation(M_PI)]; 268 269 } completion:^(BOOL finished) { 270 271 //CGAffineTransformIdentity将视图的形变复原(平移,缩放,旋转) 272 273 [recognizer.viewsetTransform:CGAffineTransformIdentity]; 274 275 }]; 276 277 } 278 279 280 281 } 282 283 284 285 #pragma mark 点按手势 286 287 - (void) tap:(UITapGestureRecognizer *) recognizer 288 289 { 290 291 CGRect initFrame = recognizer.view.frame; 292 293 CGRect targetFrame = recognizer.view.frame; 294 295 targetFrame.origin.y +=360; 296 297 298 299 [UIViewanimateWithDuration:1.0fanimations:^{ 300 301 [UIViewsetAnimationRepeatAutoreverses:YES]; 302 303 [UIViewsetAnimationRepeatCount:2]; 304 305 306 307 [self.imageView setFrame:targetFrame]; 308 309 }completion:^(BOOL finished) { 310 311 [self.imageView setFrame:initFrame]; 312 313 }]; 314 315 316 317 }