首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIMapView:未调用UIPinchGestureRecognizer

UIMapView:未调用UIPinchGestureRecognizer
EN

Stack Overflow用户
提问于 2011-08-22 01:06:04
回答 1查看 1.7K关注 0票数 2

我用UIMapView实现了手势识别器,就像这个问题的公认答案中所描述的那样:How to intercept touches events on a MKMapView or UIWebView objects?

单次触摸被正确识别。但是,当我为了识别地图缩放而将类的超类从UIGestureRecognizer更改为UIPinchGestureRecognizer时,一切都停止了。现在,只有当用户双击地图上的注记时,才会发生TouchesEnded事件(不知道为什么!)并且在用户缩小地图时不会发生(放大或缩小都无关紧要)。

PS我正在使用iOS SDK4.3并在模拟器中测试我的应用程序,如果有关系的话。

mapViewController.m - viewDidLoad方法代码

代码语言:javascript
复制
 - (void)viewDidLoad
 {
     [super viewDidLoad];
     MapGestureRecognizer *changeMapPositionRecognizer = [[MapGestureRecognizer alloc] init];
     changeMapPositionRecognizer.touchesEndedCallback = ^(NSSet * touches, UIEvent * event) 
     {
        ...
     };
     [self.mapView addGestureRecognizer:changeMapPositionRecognizer];
     [changeMapPositionRecognizer release];
}

MapGestureRecognizer.h的代码:

代码语言:javascript
复制
#import <UIKit/UIKit.h>
typedef void (^TouchesEventBlock) (NSSet * touches, UIEvent * event);
@interface MapGestureRecognizer : UIPinchGestureRecognizer    
@property(nonatomic, copy) TouchesEventBlock touchesEndedCallback;
@end

MapGestureRecognizer.m的代码:

代码语言:javascript
复制
#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

我应该在中纠正什么才能使握手手势被识别?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-22 02:11:24

我不确定为什么需要子类化UIPinchGestureRecognizer而不是直接按原样使用它。

我也不确定为什么需要手势识别器来检测地图缩放,这可以通过使用委托方法regionWillChangeAnimatedregionDidChangeAnimated并比较前后的跨度来实现。除非您正在尝试检测正在发生的缩放(并且不想等到用户完成手势)

手势识别器可能不会被调用,因为取而代之的是调用地图视图自己的收缩手势识别器。

要像调用地图视图一样调用识别器,请实现UIGestureRecognizer委托方法shouldRecognizeSimultaneouslyWithGestureRecognizer并返回YES:

代码语言:javascript
复制
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer:
        (UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

确保手势识别器的delegate属性已设置,否则该方法也不会被调用。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7139735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档