我想在用户使用 map 视图移动时添加一些逻辑,例如,他进行平移touch .但是,当我添加手势识别器并想要记录touch 时,什么都没有发生.当我在另一个视图控制器中try 并将识别器添加到控制器的视图中时,它工作正常.

下面是我的代码(map view是application delegate的一个属性,因为即使它不可见,我也需要用它做一些其他事情):

- (void)viewDidLoad
{
    ...
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showPan)];
    [appDelegate.mapView addGestureRecognizer:panGesture];
    [panGesture release];
}

- (void)showPan
{
    NSLog(@"pan!");
}

我使用最新的iOS 4.2.1

谢谢你的建议.

推荐答案

好吧,因为没人知道,我不得不花一个苹果的技术支持咨询;o)

因为MKMapView显然有自己的识别器与用户交互,所以您必须遵守UIgestureRecognitizerDelegate协议,并实现如下(BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer::

- (void)viewDidLoad
{
    ...
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showPan)];
    panGesture.delegate = self;
    [appDelegate.mapView addGestureRecognizer:panGesture];
    [panGesture release];
}

- (void)showPan
{
    NSLog(@"pan!");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {   
    return YES;
}

然后它就像一个符咒.

Objective-c相关问答推荐

iOS中的非延迟图像加载

Objective C 布尔数组

获取 Objective-C 类或实例的所有方法

在 NSData 和 base64 字符串之间转换

如何调整 UIModalPresentationFormSheet 的大小?

如何更改 UICollectionView 中的滚动方向?

当单元格全屏时,为什么 UICollectionView 会记录错误?

自定义 UISwitch 和 App Store 批准

对块进行类型定义是如何工作的

在 ios8 中没有调用 didRegisterForRemoteNotificationsWithDeviceToken,但是 didRegister...Settings 是

从昨天开始无法将存档上传到应用store

如何在 ios 8 的 alertview 中添加文本输入?

从 iPhone 设备查找当前国家

判断是否已实现可选协议方法

如何设置 NSPredicate 来查找具有 nil 属性的对象

如何更改标签栏上的非活动图标/文本 colored颜色 ?

带有Go按钮而不是返回的 iOS 键盘

找出用户是否按下了 uinavigationcontroller 中的后退按钮?

公开只读但具有私有设置器的 Objective-C 属性

UITableViewCell中的文本居中对齐问题