简体   繁体   中英

UIPinchGestureRecognizer not called


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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM