UIGestureRecognizer on UIImageView

system picture system · Oct 11, 2010 · Viewed 105.4k times · Source

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?

Answer

user467105 picture user467105 · Oct 11, 2010

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...
}