3
votes

I have an imageView, to which, I have added UIPinchGestureRecognizer and UIRotationGestureRecognizer.

in pinch gesture, I transform and scale the View and in rotation gesture I apply rotation transform to it.

The problem is when I rotate the imageView and then start zooming. Zooming always begins from the normal state.

So What i want is when I rotate it to say 30 degree clockwise and then zoom it. it should zoom while remaining that 30 degree on the clockwise direction.

Here is the code:

- (void)viewDidLoad{
    [super viewDidLoad];

//setting up the image view

mTotalRotation = 0.0;
self.imageView.image = self.photo;
self.imageView.userInteractionEnabled = YES;

UIRotationGestureRecognizer *twoFingersRotate = 
[[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
[self.imageView addGestureRecognizer:twoFingersRotate];

UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchZoom:)] autorelease];
[self.imageView addGestureRecognizer:pinchGesture];

// Do any additional setup after loading the view from its nib.
}
// Rotation gesture handler
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 
{


if ([recognizer state] == UIGestureRecognizerStateEnded) {

    mTotalRotation += recognizer.rotation;
    return;
}

self.imageView.transform = CGAffineTransformMakeRotation(mTotalRotation + recognizer.rotation);         


}


   // Pinch Gesture

   -(void)pinchZoom:(UIPinchGestureRecognizer*)recognizer{


    self.imageView.transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale) ;



}
2
Can you share the code you use to rotate and zoom the image view?sch
Check this tutorial: UIGestureRecognizer Tutorial in iOS 5: Pinches, Pans, and More! If You already know how to handle Gesture Recognizers, jump to Simultaneous Gesture RecognizersFrade
@sch: Please check now. I have added the code.Amogh Talpallikar

2 Answers

4
votes

Change the line:

self.imageView.transform = CGAffineTransformMakeRotation(mTotalRotation + recognizer.rotation);

with:

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, recognizer.rotation);

And the line:

self.imageView.transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);

with:

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, recognizer.scale, recognizer.scale);

Edit

To limit the scale, you can do the following:

CGAffineTransform transform = self.imageView.transform;
float newScale = recognizer.scale * sqrt(transform.a*transform.a + transform.c*transform.c);
if (newScale > scaleLimit) {
    self.imageView.transform = CGAffineTransformScale(transform, recognizer.scale, recognizer.scale);
}
0
votes

Maybe you could have your scale and rotate view as a subview of a view that you zoom?