如何拦截触摸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];知道我做错了什么吗?
3 回答
慕仙森
TA贡献1827条经验 获得超7个赞
我发现实现这一目标的最好方法是使用手势识别器。其他方式涉及大量的hackish编程,不完美地复制Apple的代码,特别是在多点触控的情况下。
这就是我所做的:实现一个无法阻止的手势识别器,并且无法阻止其他手势识别器。将它添加到地图视图,然后使用gestureRecognizer的touchesBegan,touchesMoved等等。
如何检测MKMapView中的任何水龙头(没有技巧)
WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) { self.lockedOnUserLocation = NO;};[mapView addGestureRecognizer:tapInterceptor];
WildcardGestureRecognizer.h
//// WildcardGestureRecognizer.h// Copyright 2010 Floatopian LLC. All rights reserved.//#import <Foundation/Foundation.h>typedef void (^TouchesEventBlock)(NSSet * touches, UIEvent * event);@interface WildcardGestureRecognizer : UIGestureRecognizer { TouchesEventBlock touchesBeganCallback;}@property(copy) TouchesEventBlock touchesBeganCallback;@end
WildcardGestureRecognizer.m
//// WildcardGestureRecognizer.m// Created by Raymond Daly on 10/31/10.// Copyright 2010 Floatopian LLC. All rights reserved.//#import "WildcardGestureRecognizer.h"@implementation WildcardGestureRecognizer@synthesize touchesBeganCallback;-(id) init{ if (self = [super init]) { self.cancelsTouchesInView = NO; } return self;}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ if (touchesBeganCallback) touchesBeganCallback(touches, event);}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)reset{}- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event{}- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer{ return NO;}- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer{ return NO;}@end
SWIFT 3
let tapInterceptor = WildCardGestureRecognizer(target: nil, action: nil)tapInterceptor.touchesBeganCallback = { _, _ in self.lockedOnUserLocation = false}mapView.addGestureRecognizer(tapInterceptor)
WildCardGestureRecognizer.swift
import UIKit.UIGestureRecognizerSubclassclass WildCardGestureRecognizer: UIGestureRecognizer { var touchesBeganCallback: ((Set<UITouch>, UIEvent) -> Void)? override init(target: Any?, action: Selector?) { super.init(target: target, action: action) self.cancelsTouchesInView = false } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { super.touchesBegan(touches, with: event) touchesBeganCallback?(touches, event) } override func canPrevent(_ preventedGestureRecognizer: UIGestureRecognizer) -> Bool { return false } override func canBePrevented(by preventingGestureRecognizer: UIGestureRecognizer) -> Bool { return false }}
眼眸繁星
TA贡献1873条经验 获得超9个赞
经过一天的比萨饼,尖叫声,我终于找到了解决方案!井井有条!
Peter,我使用了上面的技巧并稍微调整了一下,最终得到了一个与MKMapView完美配合的解决方案,并且也应该与UIWebView配合使用
MKTouchAppDelegate.h
#import <UIKit/UIKit.h>@class UIViewTouch;@class MKMapView;@interface MKTouchAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UIViewTouch *viewTouch; MKMapView *mapView;}@property (nonatomic, retain) UIViewTouch *viewTouch;@property (nonatomic, retain) MKMapView *mapView;@property (nonatomic, retain) IBOutlet UIWindow *window;@end
MKTouchAppDelegate.m
#import "MKTouchAppDelegate.h"#import "UIViewTouch.h"#import <MapKit/MapKit.h>@implementation MKTouchAppDelegate@synthesize window;@synthesize viewTouch;@synthesize mapView;- (void)applicationDidFinishLaunching:(UIApplication *)application { //We create a view wich will catch Events as they occured and Log them in the Console viewTouch = [[UIViewTouch alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; //Next we create the MKMapView object, which will be added as a subview of viewTouch mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; [viewTouch addSubview:mapView]; //And we display everything! [window addSubview:viewTouch]; [window makeKeyAndVisible];}- (void)dealloc { [window release]; [super dealloc];}@end
UIViewTouch.h
#import <UIKit/UIKit.h>@class UIView;@interface UIViewTouch : UIView { UIView *viewTouched;}@property (nonatomic, retain) UIView * viewTouched;- (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
UIViewTouch.m
#import "UIViewTouch.h"#import <MapKit/MapKit.h>@implementation UIViewTouch@synthesize viewTouched;//The basic idea here is to intercept the view which is sent back as the firstresponder in hitTest.//We keep it preciously in the property viewTouched and we return our view as the firstresponder.- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { NSLog(@"Hit Test"); viewTouched = [super hitTest:point withEvent:event]; return self;}//Then, when an event is fired, we log this one and then send it back to the viewTouched we kept, and voilà!!! :)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Touch Began"); [viewTouched touchesBegan:touches withEvent:event];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Touch Moved"); [viewTouched touchesMoved:touches withEvent:event];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Touch Ended"); [viewTouched touchesEnded:touches withEvent:event];}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Touch Cancelled");}@end
我希望这对你们有所帮助!
干杯
慕雪6442864
TA贡献1812条经验 获得超5个赞
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleGesture:)]; tgr.numberOfTapsRequired = 2;tgr.numberOfTouchesRequired = 1;[mapView addGestureRecognizer:tgr];[tgr release];- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer{ if (gestureRecognizer.state != UIGestureRecognizerStateEnded) return; CGPoint touchPoint = [gestureRecognizer locationInView:mapView]; CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; //.............}
- 3 回答
- 0 关注
- 641 浏览
添加回答
举报
0/150
提交
取消