UIMapView: UIPinchGestureRecognizer not called
I implemented gesture recognizer in UIMapView
just as described in the accepted answer to this question: How to intercept touches events on a MKMapView or UIWebView objects?
Single touches are recognized correctly. However, when I changed the superclass of my class from UIGestureRecognizer
to UIPinchGestureRecognizer
in order to recognize map scaling, everything stopped working.
Now TouchesEnded event occurs only when the user double tap the annotation on map (don't know, why!) and doesn't occur when the user pinches the map (zoom in or out doesn't matter).
PS I'm using iOS SDK 4.3 and testing my app in simulator if that matters.
The code of mapViewController.m - viewDidLoad metho开发者_StackOverflow中文版d:
- (void)viewDidLoad
{
[super viewDidLoad];
MapGestureRecognizer *changeMapPositionRecognizer = [[MapGestureRecognizer alloc] init];
changeMapPositionRecognizer.touchesEndedCallback = ^(NSSet * touches, UIEvent * event)
{
...
};
[self.mapView addGestureRecognizer:changeMapPositionRecognizer];
[changeMapPositionRecognizer release];
}
The code of MapGestureRecognizer.h:
#import <UIKit/UIKit.h>
typedef void (^TouchesEventBlock) (NSSet * touches, UIEvent * event);
@interface MapGestureRecognizer : UIPinchGestureRecognizer
@property(nonatomic, copy) TouchesEventBlock touchesEndedCallback;
@end
The code of MapGestureRecognizer.m:
#import "MapGestureRecognizer.h"
@implementation MapGestureRecognizer
@synthesize touchesEndedCallback = _touchesEndedCallback;
- (id)init
{
self = [super init];
if (self) {
self.cancelsTouchesInView = NO;
}
return self;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.touchesEndedCallback)
{
self.touchesEndedCallback(touches, event);
NSLog(@"Touches ended, callback done");
}
else
{
NSLog(@"Touches ended, callback skipped");
}
}
- (void) dealloc
{
[super dealloc];
}
@end
What should I correct in to make pinch gesture to be recognized?
I'm not sure why you need to subclass UIPinchGestureRecognizer
instead of using it directly as-is.
Also not sure why you need the gesture recognizer to detect map scaling which you could do by using the delegate methods regionWillChangeAnimated
and regionDidChangeAnimated
and comparing the span before and after. Unless you are trying to detect the scaling as it is happening (and not wanting to wait until user finishes the gesture)
The gesture recognizer may not be getting called because the map view's own pinch gesture recognizer is getting called instead.
To have your recognizer called as well as the map view's, implement the UIGestureRecognizer delegate method shouldRecognizeSimultaneouslyWithGestureRecognizer
and return YES:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Make sure the gesture recognizer's delegate
property is set or that method won't get called either.
精彩评论