gpt4 book ai didi

iphone - UIMapView : UIPinchGestureRecognizer not called

转载 作者:行者123 更新时间:2023-12-03 20:32:32 27 4
gpt4 key购买 nike

我在 UIMapView 中实现了手势识别器,正如此问题的已接受答案中所述:How to intercept touches events on a MKMapView or UIWebView objects?

单次触摸可以正确识别。但是,当我将类的父类(super class)从 UIGestureRecognizer 更改为 UIPinchGestureRecognizer 以识别 map 缩放时,一切都停止工作。现在 TouchesEnded 事件仅在用户双击 map 上的注释时发生(不知道,为什么!),而当用户捏合 map 时不会发生(放大或缩小并不重要)。

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

我应该纠正什么才能使捏合手势被识别?

最佳答案

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

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

手势识别器可能不会被调用,因为 map View 自己的捏合手势识别器被调用。

要调用您的识别器以及 map View 的识别器,请实现 UIGestureRecognizer 委托(delegate)方法 shouldRecognizeSimultaneouslyWithGestureRecognizer 并返回 YES:

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

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

关于iphone - UIMapView : UIPinchGestureRecognizer not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com