首页 > 代码库 > ios开发--旋转、移动、缩放手势实例代码

ios开发--旋转、移动、缩放手势实例代码

  1. // 添加所有的手势  
  2. - (void) addGestureRecognizerToView:(UIView *)view  
  3. {  
  4.     // 旋转手势  
  5.     UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];  
  6.     [view addGestureRecognizer:rotationGestureRecognizer];  
  7.       
  8.     // 缩放手势  
  9.     UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];  
  10.     [view addGestureRecognizer:pinchGestureRecognizer];  
  11.       
  12.     // 移动手势  
  13.     UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];  
  14.     [view addGestureRecognizer:panGestureRecognizer];  
  15. }  
  16.   
  17. // 处理旋转手势  
  18. - (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer  
  19. {  
  20.     UIView *view = rotationGestureRecognizer.view;  
  21.     if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  22.         view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);  
  23.         [rotationGestureRecognizer setRotation:0];  
  24.     }  
  25. }  
  26.   
  27. // 处理缩放手势  
  28. - (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer  
  29. {  
  30.     UIView *view = pinchGestureRecognizer.view;  
  31.     if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  32.         view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);  
  33.         pinchGestureRecognizer.scale = 1;  
  34.     }  
  35. }  
  36.   
  37. // 处理拖拉手势  
  38. - (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer  
  39. {  
  40.     UIView *view = panGestureRecognizer.view;  
  41.     if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  42.         CGPoint translation = [panGestureRecognizer translationInView:view.superview];  
  43.         [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];  
  44.         [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];  
  45.     }  
  46. }