Search code examples
iphoneioscocoa-touchuiscrollviewuigesturerecognizer

Rotate Zooming ImageView in ScrollView?


I have a UIScrollView that has a zooming UIImageView, ie it implements:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

I'm trying to add a UIRotationGestureRecognizer to this imageView and I do it as follows:

_rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[self.imageView addGestureRecognizer:_rotationGestureRecognizer];

-(void)rotate:(id)sender
{
    UIRotationGestureRecognizer *rotationGestureRecognizer = [(UIRotationGestureRecognizer*)sender retain];

    if(rotationGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        self.lastRotation = 0.0;
        return;
    }

    CGFloat rotation = 0.0 - (self.lastRotation - rotationGestureRecognizer.rotation);
    rotationGestureRecognizer.view.transform = CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation);
    self.lastRotation = rotationGestureRecognizer.rotation;

    [rotationGestureRecognizer release];
}

I'm just wondering, is it possible to even do this? It seems the UIScrollView is blocking the touches to my UIImageView because nothing is happening. Does Apple recommend to not do this with a zooming view in a UIScrollView?


Solution

  • Following code is working.Add gesture to scrollView instead of imageView.

     UIRotationGestureRecognizer* _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
     [self.scrollView addGestureRecognizer:_rotationGestureRecognizer];
    

    Swift 5 Version:

    let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(self.rotate(_:)))
    scrollView.addGestureRecognizer(rotationGestureRecognizer)