iOSはジェスチャーUIGestureRecognizerを使用して画像をズーム、移動、回転操作する


iOSはラケット、回転、スライド、つまみなど豊富なジェスチャーを提供しているので、画像の操作は非常に友好的で簡単に見えます.
次のコードは、UIGestureRecognizerを使用して画像をスケーリング、移動、回転操作する方法を示しています.
 
コードを使用する前に、まずUIViewを作成し、画像を表示するためにUIImageViewビューを追加します.では、このUIViewは、画像画板のように、画像の操作をこのビューに基づいて行う.ビューにジェスチャー識別器を作成する
- (void)viewDidLoad { [super viewDidLoad]; UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease]; [pinchRecognizer setDelegate:self]; [self.view addGestureRecognizer:pinchRecognizer]; UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] autorelease]; [rotationRecognizer setDelegate:self]; [self.view addGestureRecognizer:rotationRecognizer]; UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease]; [panRecognizer setMinimumNumberOfTouches:1]; [panRecognizer setMaximumNumberOfTouches:1]; [panRecognizer setDelegate:self]; [canvas addGestureRecognizer:panRecognizer]; UITapGestureRecognizer *tapProfileImageRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)] autorelease]; [tapProfileImageRecognizer setNumberOfTapsRequired:1]; [tapProfileImageRecognizer setDelegate:self]; [canvas addGestureRecognizer:tapProfileImageRecognizer]; }

 
次に、ジェスチャーメッセージに応答する方法を示します.
 
//   

-(void)scale:(id)sender {



    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

      _lastScale = 1.0;

    }



    CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);



    CGAffineTransform currentTransform = photoImage.transform;

    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);



    [photoImage setTransform:newTransform];



    _lastScale = [(UIPinchGestureRecognizer*)sender scale];

    [self showOverlayWithFrame:photoImage.frame];

}



//   

-(void)rotate:(id)sender {



    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {



      _lastRotation = 0.0;

      return;

    }



    CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);



    CGAffineTransform currentTransform = photoImage.transform;

    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);



    [photoImage setTransform:newTransform];



    _lastRotation = [(UIRotationGestureRecognizer*)sender rotation];

    [self showOverlayWithFrame:photoImage.frame];

}



//   

-(void)move:(id)sender {



  CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:canvas];



  if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

    _firstX = [photoImage center].x;

    _firstY = [photoImage center].y;

  }



  translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);



  [photoImage setCenter:translatedPoint];

  [self showOverlayWithFrame:photoImage.frame];

}