拦截/劫持iPhone Touch Events for MKMapView [英] Intercepting/Hijacking iPhone Touch Events for MKMapView

查看:129
本文介绍了拦截/劫持iPhone Touch Events for MKMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3.0 SDK中是否有一个错误,可以禁用实时缩放并拦截MKMapView的放大手势?我有一些真正简单的代码,所以我可以检测到点击事件,但有两个问题:


  1. 放大手势总是被解释为放大

  2. 没有任何缩放手势实时更新地图的视图。

hitTest,如果我返回地图视图,MKMapView功能非常好,但我没有机会拦截事件。



任何想法? p>

MyMapView.h:

  @interface MyMapView:MKMapView 
{
UIView * map;
}

MyMapView.m:

   - (id)initWithFrame:(CGRect)frame 
{
if(![super initWithFrame:frame])
return nil;

self.multipleTouchEnabled = true;

return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)事件{
NSLog(@Hit Test);
map = [super hitTest:point withEvent:event];
返回自我;


- (void)touchesCancelled:(NSSet *)触摸与事件:(UIEvent *)事件
{
NSLog(@%s,__FUNCTION__) ;
[map touchesCancelled:touches withEvent:event];


- (void)touchesBegan:(NSSet *)触发与事件:(UIEvent *)事件
{
NSLog(@%s,__FUNCTION__) ;
[map touchesBegan:touches withEvent:event];


- (void)touchesMoved:(NSSet *)触摸与事件:(UIEvent *)事件
{
NSLog(@%s,%x ,__FUNCTION__,mViewTouched);
[map touchesMoved:touches withEvent:event];


- (void)touchesEnded:(NSSet *)触摸与事件:(UIEvent *)事件
{
NSLog(@%s,%x ,__FUNCTION__,mViewTouched);
[map touchesEnded:touches withEvent:event];
}


解决方案

实现这一点是用手势识别器。您是否想要自己识别缩放事件或只是在用户缩放/平移时检测不清楚。



我不需要检测到地图平移或缩放 - 我只是关心用户是否把手指放在MKMapView的任何地方。如果您需要检测100%召回和精确度的缩放或平移,可能会比这更复杂。我们真正需要的是MKMapView的开源实现,所以我们可以将其添加到代理中,以及许多其他功能。



这是我做的:实现手势识别器不能防止并且不能阻止其他手势识别器。将它添加到地图视图中,并根据需要处理相关的触摸事件。



如何检测MKMapView中的任何一个点击(sans tricks)



  WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init]; 
tapInterceptor.touchesBeganCallback = ^(NSSet * touches,UIEvent * event){
self.lockedOnUserLocation = NO;
};
[mapView addGestureRecognizer:tapInterceptor];



通配符GestureRecognizer.h



  // 
// WildcardGestureRecognizer.h
//版权所有2010 Floatopian LLC。版权所有。
//

#import< Foundation / Foundation.h>

typedef void(^ TouchesEventBlock)(NSSet * touches,UIEvent * event);

@interface WildcardGestureRecognizer:UIGestureRecognizer {
TouchesEventBlock touchesBeganCallback;
}
@property(copy)TouchesEventBlock touchesBeganCallback;


@end



通配符GestureRecognizer.m



  // 
// WildcardGestureRecognizer.m
//由Raymond Daly于10/31/10创建。
//版权所有2010 Floatopian LLC。版权所有。
//

#importWildcardGestureRecognizer.h


@implementation WildcardGestureRecognizer
@synthesize touchesBeganCallback;

- (id)init {
if(self = [super init])
{
self.cancelsTouchesInView = NO;
}
return self;


- (void)touchesBegan:(NSSet *)触摸与事件:(UIEvent *)事件
{
if(touchesBeganCallback)
touchesBeganCallback触动事件);


- (void)touchesCancelled:(NSSet *)触摸与事件:(UIEvent *)事件
{
}

- (void)touchesEnded:(NSSet *)触摸与事件:(UIEvent *)事件
{
}

- (void)touchesMoved:(NSSet *)触摸与事件:(UIEvent *)event
{
}

- (void)reset
{
}

- (void)ignoreTouch: (UITouch *)touch forEvent:(UIEvent *)事件
{
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventGestureRecognizer
{
返回NO;
}

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventGestureRecognizer
{
return NO;
}

@end


Is there a bug in the 3.0 SDK that disables real-time zooming and intercepting the zoom-in gesture for the MKMapView? I have some real simple code so I can detect tap events, but there are two problems:

  1. zoom-in gesture is always interpreted as a zoom-out
  2. none of the zoom gestures update the Map's view in realtime.

In hitTest, if I return the "map" view, the MKMapView functionality works great, but I don't get the opportunity to intercept the events.

Any ideas?

MyMapView.h:

@interface MyMapView : MKMapView
{
    UIView      *map;
}

MyMapView.m:

- (id)initWithFrame:(CGRect)frame
{
    if (![super initWithFrame:frame])
        return nil;

    self.multipleTouchEnabled = true;

    return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"Hit Test");
    map = [super hitTest:point withEvent:event];
    return self;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __FUNCTION__);
    [map touchesCancelled:touches withEvent:event];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
    NSLog(@"%s", __FUNCTION__);
    [map touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    NSLog(@"%s, %x", __FUNCTION__, mViewTouched);
    [map touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    NSLog(@"%s, %x", __FUNCTION__, mViewTouched);
    [map touchesEnded:touches withEvent:event];
}

解决方案

The best way I have found to achieve this is with a Gesture Recognizer. It's unclear if you want to recognize zoom events yourself or just detect when the user is zooming/panning.

I don't need to detect a map pan or zoom--i just care if the user has put a finger down anywhere in the MKMapView. If you need to detect zooming or panning with 100% recall and precision, it might be more complicated than this. What we really need is an open source implementation of MKMapView so we can add this to the delegate, among many other features.

Here's what I do: Implement a gesture recognizer that cannot be prevented and that cannot prevent other gesture recognizers. Add it to the map view and deal with the relevant touch events as you desire.

How to detect any tap inside an MKMapView (sans tricks)

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

这篇关于拦截/劫持iPhone Touch Events for MKMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆