我尝试在地图视图上检测事件。我只需要检测缩放(在屏幕上双击或两根手指)。我尝试添加一个检测事件的 UIview 图层,但是如果我添加一个图层,我会失去对地图的控制(如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?)
感谢帮助!
托尼
我尝试在地图视图上检测事件。我只需要检测缩放(在屏幕上双击或两根手指)。我尝试添加一个检测事件的 UIview 图层,但是如果我添加一个图层,我会失去对地图的控制(如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?)
感谢帮助!
托尼
给我们看一些代码。您应该能够将您不感兴趣的任何事件传递回父视图。例如,在您检测到两根手指敲击并做任何您想做的事情后,将相同的事件传递回 mapview 并让它自行缩放。
这是您在完成事件检测后调用的内容:
[self.nextResponder touchesBegan:touches withEvent:event];
据此:链接文本
Mkmapview 必须是事件的默认接收器。
所以我将主窗口的类更改为 MyMainWindow:
我的主窗口.h
#import <Foundation/Foundation.h>
@class TouchListener;
@interface MyMainWindow : UIWindow {
TouchListener *Touch;
}
@end
我的主窗口.m
#import "MyMainWindow.h"
@implementation MyMainWindow
- (void)sendEvent:(UIEvent*)event {
[super sendEvent:event];
[Touch sendEvent:event];
}
@end
触摸监听器.h
#import <Foundation/Foundation.h>
@interface TouchListener : UIView {
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
@end
TouchListeners.m
#import "TouchListener.h"
@implementation TouchListener
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return self;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Moved");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Began");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Ended");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Cancel");
}
@end
我错过了什么?
感谢帮助