如何拦截触摸MKMapView或UIWebView对象上的事件?

如何拦截触摸MKMapView或UIWebView对象上的事件?

我不确定我做错了什么,但我试图抓住一个MKMapView物体。我通过创建以下类来继承它:

#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>@interface MapViewWithTouches : MKMapView {}- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event;   @end

并实施:

#import "MapViewWithTouches.h"@implementation MapViewWithTouches- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {

    NSLog(@"hello");
    //[super touchesBegan:touches   withEvent:event];}@end

但看起来当我使用这个类时,我在控制台上看不到任何内容:

MapViewWithTouches *mapView = [[MapViewWithTouches alloc] initWithFrame:self.view.frame];[self.view insertSubview:mapView atIndex:0];

知道我做错了什么吗?


芜湖不芜
浏览 642回答 3
3回答

慕仙森

我发现实现这一目标的最好方法是使用手势识别器。其他方式涉及大量的hackish编程,不完美地复制Apple的代码,特别是在多点触控的情况下。这就是我所做的:实现一个无法阻止的手势识别器,并且无法阻止其他手势识别器。将它添加到地图视图,然后使用gestureRecognizer的touchesBegan,touchesMoved等等。如何检测MKMapView中的任何水龙头(没有技巧)WildcardGestureRecognizer&nbsp;*&nbsp;tapInterceptor&nbsp;=&nbsp;[[WildcardGestureRecognizer&nbsp;alloc]&nbsp;init];tapInterceptor.touchesBeganCallback&nbsp;=&nbsp;^(NSSet&nbsp;*&nbsp;touches,&nbsp;UIEvent&nbsp;*&nbsp;event)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.lockedOnUserLocation&nbsp;=&nbsp;NO;};[mapView&nbsp;addGestureRecognizer:tapInterceptor];WildcardGestureRecognizer.h////&nbsp;&nbsp;WildcardGestureRecognizer.h//&nbsp;&nbsp;Copyright&nbsp;2010&nbsp;Floatopian&nbsp;LLC.&nbsp;All&nbsp;rights&nbsp;reserved.//#import&nbsp;<Foundation/Foundation.h>typedef&nbsp;void&nbsp;(^TouchesEventBlock)(NSSet&nbsp;*&nbsp;touches,&nbsp;UIEvent&nbsp;*&nbsp;event);@interface&nbsp;WildcardGestureRecognizer&nbsp;:&nbsp;UIGestureRecognizer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;TouchesEventBlock&nbsp;touchesBeganCallback;}@property(copy)&nbsp;TouchesEventBlock&nbsp;touchesBeganCallback;@endWildcardGestureRecognizer.m////&nbsp;&nbsp;WildcardGestureRecognizer.m//&nbsp;&nbsp;Created&nbsp;by&nbsp;Raymond&nbsp;Daly&nbsp;on&nbsp;10/31/10.//&nbsp;&nbsp;Copyright&nbsp;2010&nbsp;Floatopian&nbsp;LLC.&nbsp;All&nbsp;rights&nbsp;reserved.//#import&nbsp;"WildcardGestureRecognizer.h"@implementation&nbsp;WildcardGestureRecognizer@synthesize&nbsp;touchesBeganCallback;-(id)&nbsp;init{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(self&nbsp;=&nbsp;[super&nbsp;init]) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cancelsTouchesInView&nbsp;=&nbsp;NO; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self;}-&nbsp;(void)touchesBegan:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(touchesBeganCallback) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;touchesBeganCallback(touches,&nbsp;event);}-&nbsp;(void)touchesCancelled:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event{}-&nbsp;(void)touchesEnded:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event{}-&nbsp;(void)touchesMoved:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event{}-&nbsp;(void)reset{}-&nbsp;(void)ignoreTouch:(UITouch&nbsp;*)touch&nbsp;forEvent:(UIEvent&nbsp;*)event{}-&nbsp;(BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer&nbsp;*)preventingGestureRecognizer{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NO;}-&nbsp;(BOOL)canPreventGestureRecognizer:(UIGestureRecognizer&nbsp;*)preventedGestureRecognizer{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NO;}@endSWIFT 3let&nbsp;tapInterceptor&nbsp;=&nbsp;WildCardGestureRecognizer(target:&nbsp;nil,&nbsp;action:&nbsp;nil)tapInterceptor.touchesBeganCallback&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;_,&nbsp;_&nbsp;in &nbsp;&nbsp;&nbsp;&nbsp;self.lockedOnUserLocation&nbsp;=&nbsp;false}mapView.addGestureRecognizer(tapInterceptor)WildCardGestureRecognizer.swiftimport&nbsp;UIKit.UIGestureRecognizerSubclassclass&nbsp;WildCardGestureRecognizer:&nbsp;UIGestureRecognizer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;touchesBeganCallback:&nbsp;((Set<UITouch>,&nbsp;UIEvent)&nbsp;->&nbsp;Void)? &nbsp;&nbsp;&nbsp;&nbsp;override&nbsp;init(target:&nbsp;Any?,&nbsp;action:&nbsp;Selector?)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.init(target:&nbsp;target,&nbsp;action:&nbsp;action) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cancelsTouchesInView&nbsp;=&nbsp;false &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;override&nbsp;func&nbsp;touchesBegan(_&nbsp;touches:&nbsp;Set<UITouch>,&nbsp;with&nbsp;event:&nbsp;UIEvent)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.touchesBegan(touches,&nbsp;with:&nbsp;event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;touchesBeganCallback?(touches,&nbsp;event) &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;override&nbsp;func&nbsp;canPrevent(_&nbsp;preventedGestureRecognizer:&nbsp;UIGestureRecognizer)&nbsp;->&nbsp;Bool&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;override&nbsp;func&nbsp;canBePrevented(by&nbsp;preventingGestureRecognizer:&nbsp;UIGestureRecognizer)&nbsp;->&nbsp;Bool&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false &nbsp;&nbsp;&nbsp;&nbsp;}}

眼眸繁星

经过一天的比萨饼,尖叫声,我终于找到了解决方案!井井有条!Peter,我使用了上面的技巧并稍微调整了一下,最终得到了一个与MKMapView完美配合的解决方案,并且也应该与UIWebView配合使用MKTouchAppDelegate.h#import&nbsp;<UIKit/UIKit.h>@class&nbsp;UIViewTouch;@class&nbsp;MKMapView;@interface&nbsp;MKTouchAppDelegate&nbsp;:&nbsp;NSObject&nbsp;<UIApplicationDelegate>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;UIWindow&nbsp;*window; &nbsp;&nbsp;&nbsp;&nbsp;UIViewTouch&nbsp;*viewTouch; &nbsp;&nbsp;&nbsp;&nbsp;MKMapView&nbsp;*mapView;}@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;UIViewTouch&nbsp;*viewTouch;@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;MKMapView&nbsp;*mapView;@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;IBOutlet&nbsp;UIWindow&nbsp;*window;@endMKTouchAppDelegate.m#import&nbsp;"MKTouchAppDelegate.h"#import&nbsp;"UIViewTouch.h"#import&nbsp;<MapKit/MapKit.h>@implementation&nbsp;MKTouchAppDelegate@synthesize&nbsp;window;@synthesize&nbsp;viewTouch;@synthesize&nbsp;mapView;-&nbsp;(void)applicationDidFinishLaunching:(UIApplication&nbsp;*)application&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//We&nbsp;create&nbsp;a&nbsp;view&nbsp;wich&nbsp;will&nbsp;catch&nbsp;Events&nbsp;as&nbsp;they&nbsp;occured&nbsp;and&nbsp;Log&nbsp;them&nbsp;in&nbsp;the&nbsp;Console &nbsp;&nbsp;&nbsp;&nbsp;viewTouch&nbsp;=&nbsp;[[UIViewTouch&nbsp;alloc]&nbsp;initWithFrame:CGRectMake(0,&nbsp;0,&nbsp;320,&nbsp;480)]; &nbsp;&nbsp;&nbsp;&nbsp;//Next&nbsp;we&nbsp;create&nbsp;the&nbsp;MKMapView&nbsp;object,&nbsp;which&nbsp;will&nbsp;be&nbsp;added&nbsp;as&nbsp;a&nbsp;subview&nbsp;of&nbsp;viewTouch &nbsp;&nbsp;&nbsp;&nbsp;mapView&nbsp;=&nbsp;[[MKMapView&nbsp;alloc]&nbsp;initWithFrame:CGRectMake(0,&nbsp;0,&nbsp;320,&nbsp;480)]; &nbsp;&nbsp;&nbsp;&nbsp;[viewTouch&nbsp;addSubview:mapView]; &nbsp;&nbsp;&nbsp;&nbsp;//And&nbsp;we&nbsp;display&nbsp;everything! &nbsp;&nbsp;&nbsp;&nbsp;[window&nbsp;addSubview:viewTouch]; &nbsp;&nbsp;&nbsp;&nbsp;[window&nbsp;makeKeyAndVisible];}-&nbsp;(void)dealloc&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[window&nbsp;release]; &nbsp;&nbsp;&nbsp;&nbsp;[super&nbsp;dealloc];}@endUIViewTouch.h#import&nbsp;<UIKit/UIKit.h>@class&nbsp;UIView;@interface&nbsp;UIViewTouch&nbsp;:&nbsp;UIView&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;UIView&nbsp;*viewTouched;}@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;UIView&nbsp;*&nbsp;viewTouched;-&nbsp;(UIView&nbsp;*)hitTest:(CGPoint)point&nbsp;withEvent:(UIEvent&nbsp;*)event;-&nbsp;(void)touchesBegan:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event;-&nbsp;(void)touchesMoved:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event;-&nbsp;(void)touchesEnded:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event;-&nbsp;(void)touchesCancelled:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event;@endUIViewTouch.m#import&nbsp;"UIViewTouch.h"#import&nbsp;<MapKit/MapKit.h>@implementation&nbsp;UIViewTouch@synthesize&nbsp;viewTouched;//The&nbsp;basic&nbsp;idea&nbsp;here&nbsp;is&nbsp;to&nbsp;intercept&nbsp;the&nbsp;view&nbsp;which&nbsp;is&nbsp;sent&nbsp;back&nbsp;as&nbsp;the&nbsp;firstresponder&nbsp;in&nbsp;hitTest.//We&nbsp;keep&nbsp;it&nbsp;preciously&nbsp;in&nbsp;the&nbsp;property&nbsp;viewTouched&nbsp;and&nbsp;we&nbsp;return&nbsp;our&nbsp;view&nbsp;as&nbsp;the&nbsp;firstresponder.-&nbsp;(UIView&nbsp;*)hitTest:(CGPoint)point&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"Hit&nbsp;Test"); &nbsp;&nbsp;&nbsp;&nbsp;viewTouched&nbsp;=&nbsp;[super&nbsp;hitTest:point&nbsp;withEvent:event]; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self;}//Then,&nbsp;when&nbsp;an&nbsp;event&nbsp;is&nbsp;fired,&nbsp;we&nbsp;log&nbsp;this&nbsp;one&nbsp;and&nbsp;then&nbsp;send&nbsp;it&nbsp;back&nbsp;to&nbsp;the&nbsp;viewTouched&nbsp;we&nbsp;kept,&nbsp;and&nbsp;voilà!!!&nbsp;:)-&nbsp;(void)touchesBegan:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"Touch&nbsp;Began"); &nbsp;&nbsp;&nbsp;&nbsp;[viewTouched&nbsp;touchesBegan:touches&nbsp;withEvent:event];}-&nbsp;(void)touchesMoved:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"Touch&nbsp;Moved"); &nbsp;&nbsp;&nbsp;&nbsp;[viewTouched&nbsp;touchesMoved:touches&nbsp;withEvent:event];}-&nbsp;(void)touchesEnded:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"Touch&nbsp;Ended"); &nbsp;&nbsp;&nbsp;&nbsp;[viewTouched&nbsp;touchesEnded:touches&nbsp;withEvent:event];}-&nbsp;(void)touchesCancelled:(NSSet&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"Touch&nbsp;Cancelled");}@end我希望这对你们有所帮助!干杯

慕雪6442864

UITapGestureRecognizer&nbsp;*tgr&nbsp;=&nbsp;[[UITapGestureRecognizer&nbsp;alloc]initWithTarget:self&nbsp;action:@selector(handleGesture:)];&nbsp;&nbsp;&nbsp;tgr.numberOfTapsRequired&nbsp;=&nbsp;2;tgr.numberOfTouchesRequired&nbsp;=&nbsp;1;[mapView&nbsp;addGestureRecognizer:tgr];[tgr&nbsp;release];-&nbsp;(void)handleGesture:(UIGestureRecognizer&nbsp;*)gestureRecognizer{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(gestureRecognizer.state&nbsp;!=&nbsp;UIGestureRecognizerStateEnded) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;&nbsp;&nbsp;CGPoint&nbsp;touchPoint&nbsp;=&nbsp;[gestureRecognizer&nbsp;locationInView:mapView]; &nbsp;&nbsp;&nbsp;&nbsp;CLLocationCoordinate2D&nbsp;touchMapCoordinate&nbsp;=&nbsp;[mapView&nbsp;convertPoint:touchPoint&nbsp;toCoordinateFromView:mapView]; &nbsp;&nbsp;&nbsp;&nbsp;//.............}
打开App,查看更多内容
随时随地看视频慕课网APP