UIMapView:未调用 UIPinchGestureRecognizer
我在 UIMapView
中实现了手势识别器,正如该问题的接受答案中所述:如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?
单次触摸可以正确识别。但是,当我将类的超类从 UIGestureRecognizer
更改为 UIPinchGestureRecognizer
以识别地图缩放时,一切都停止工作。 现在,仅当用户双击地图上的注释时才会发生 TouchesEnded 事件(不知道,为什么!),而当用户捏住地图时则不会发生(放大或缩小并不重要)。
PS 我正在使用 iOS SDK 4.3 并在模拟器中测试我的应用程序(如果有的话)。
mapViewController.m - viewDidLoad 方法的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
MapGestureRecognizer *changeMapPositionRecognizer = [[MapGestureRecognizer alloc] init];
changeMapPositionRecognizer.touchesEndedCallback = ^(NSSet * touches, UIEvent * event)
{
...
};
[self.mapView addGestureRecognizer:changeMapPositionRecognizer];
[changeMapPositionRecognizer release];
}
MapGestureRecognizer.h 的代码:
#import <UIKit/UIKit.h>
typedef void (^TouchesEventBlock) (NSSet * touches, UIEvent * event);
@interface MapGestureRecognizer : UIPinchGestureRecognizer
@property(nonatomic, copy) TouchesEventBlock touchesEndedCallback;
@end
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
我应该更正什么才能识别捏合手势?
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 method:
- (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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么您需要子类化
UIPinchGestureRecognizer
而不是直接按原样使用它。也不知道为什么需要手势识别器来检测地图缩放,您可以通过使用委托方法
regionWillChangeAnimated
和regionDidChangeAnimated
并比较前后的跨度来完成。除非您尝试检测正在发生的缩放(并且不想等到用户完成手势),否则可能不会调用手势识别器,因为地图视图自己的捏合手势识别器正在被调用。
要调用您的识别器以及地图视图的识别器,请实现 UIGestureRecognizer 委托方法
shouldRecognizeSimultaneouslyWithGestureRecognizer
并返回 YES:确保设置了手势识别器的
delegate
属性,否则该方法将不会执行也不会被打电话。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
andregionDidChangeAnimated
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:Make sure the gesture recognizer's
delegate
property is set or that method won't get called either.