我用UIMapView实现了手势识别器,就像这个问题的公认答案中所描述的那样:How to intercept touches events on a MKMapView or UIWebView objects?
单次触摸被正确识别。但是,当我为了识别地图缩放而将类的超类从UIGestureRecognizer更改为UIPinchGestureRecognizer时,一切都停止了。现在,只有当用户双击地图上的注记时,才会发生TouchesEnded事件(不知道为什么!)并且在用户缩小地图时不会发生(放大或缩小都无关紧要)。
PS我正在使用iOS SDK4.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;
@endMapGestureRecognizer.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我应该在中纠正什么才能使握手手势被识别?
发布于 2011-08-22 02:11:24
我不确定为什么需要子类化UIPinchGestureRecognizer而不是直接按原样使用它。
我也不确定为什么需要手势识别器来检测地图缩放,这可以通过使用委托方法regionWillChangeAnimated和regionDidChangeAnimated并比较前后的跨度来实现。除非您正在尝试检测正在发生的缩放(并且不想等到用户完成手势)
手势识别器可能不会被调用,因为取而代之的是调用地图视图自己的收缩手势识别器。
要像调用地图视图一样调用识别器,请实现UIGestureRecognizer委托方法shouldRecognizeSimultaneouslyWithGestureRecognizer并返回YES:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}确保手势识别器的delegate属性已设置,否则该方法也不会被调用。
https://stackoverflow.com/questions/7139735
复制相似问题