0


I have a single view on my iOS application, with a mapView in it. When adding a tap or long press recognizer, the events are properly called. But not with the pinch event...

    UIPinchGestureRecognizer *handlePinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:mapView action:@selector(handleGesture:)];
    [mapView addGestureRecognizer:handlePinchGesture];

Any idea what I should add ? Thanks.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89

2 Answers2

3

Assuming your mapView is an MKMapView, it has its own pinch gesture recognizer for zooming the map.

If you want to add your own recognizer, you have to allow it to recognize simultaneously with the other (mapview-controlled) recognizer. Set your gesture recognizer's delegate and implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: (you can just always return YES).

You should also probably set self as the gesture recognizer's target and not the mapView.

omz
  • 53,243
  • 5
  • 129
  • 141
  • Even adding this function doesn't fire my handlePinchGesture function (I have set a breakpoint at line 1, and it is not reached). So I added a delegate of handlePinchGesture to self, but and now when I pinch, the code crashes... In my xib, I have no delegate. Can it be linked ? – Laurent Crivello May 06 '12 at 14:34
  • What exactly crashes the code? Have you set the delegate or not? Just implementing this method without setting the delegate won't do anything... – omz May 06 '12 at 14:40
  • "unrecognized selector sent to instance". In my xib, I can't find the delegate object... – Laurent Crivello May 06 '12 at 14:42
  • The delegate has nothing to do with your xib. Do something like `handlePinchGesture.delegate = self` in your code. – omz May 06 '12 at 14:43
  • That's what I did, and after that I get the "unrecognized selector" issue when I pinch. – Laurent Crivello May 06 '12 at 14:51
  • I see, I think I know what the issue is then. Could it be that your `handleGesture` method doesn't take any parameters? In that case, you have to use `@selector(handleGesture)` and not `@selector(handleGesture:)` (note the missing colon) when you add the gesture recognizer. – omz May 06 '12 at 14:56
  • Unfortunately it has a parameter (sender)... Any other idea ? Thanks for your time. – Laurent Crivello May 06 '12 at 15:48
  • What is the unrecognized selector's name? – omz May 06 '12 at 16:00
  • @LaurentCrivello: Better yet, what is the **complete error message** that *starts with* “unrecognized selector”? – Peter Hosey May 06 '12 at 16:06
  • That's the message:"-[MKMapView handleGesture:]: unrecognized selector sent to instance 0x7b91550", and that's the function: "-(void)handleGesture:(UIGestureRecognizer*)sender" – Laurent Crivello May 06 '12 at 16:17
  • Ah, overlooked that. You should use `self` as the target and not `mapView`. – omz May 06 '12 at 17:02
1

In the handleGesture method did you do something like this:

CGFloat beginPinch;  //declare this as your ivars

-(void)handleGesture:(UIPinchGestureRecognizer *)pinchRecognizer
{
    if (pinchRecognizer.state == UIGestureRecognizerStateBegan)
    {
        beginPinch = pinchRecognizer.scale;    
    } 
    else if (pinchRecognizer.state == UIGestureRecognizerStateEnded)
    {
        if (pinchRecognizer.scale < beginPinch)
        {
              //do your stuff
        }
    }
}
user523234
  • 14,323
  • 10
  • 62
  • 102