Search code examples
iosobjective-cuiimageviewuigesturerecognizer

UIGestureRecognizer on UIImageView


I have a UIImageView, which I want to be able to resize and rotate etc.

Can a UIGestureRecognizer be added to the UIImageView?

I would want to add a rotate and pinch recognizer to a UIImageView which would be created at runtime.

How does one add these recognizers?


Solution

  • Check that userInteractionEnabled is YES on the UIImageView. Then you can add a gesture recognizer.

    imageView.userInteractionEnabled = YES;
    UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] 
        initWithTarget:self action:@selector(handlePinch:)];
    pgr.delegate = self;
    [imageView addGestureRecognizer:pgr];
    [pgr release];
    :
    :
    - (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
    {
      //handle pinch...
    }