6 iphone events gesture-recognition
3.0 SDK中是否存在禁用实时缩放和拦截MKMapView放大手势的错误?我有一些真正简单的代码,所以我可以检测到tap事件,但有两个问题:
在hitTest中,如果我返回"map"视图,MKMapView功能效果很好,但我没有机会拦截事件.
有任何想法吗?
MyMapView.h:
@interface MyMapView : MKMapView
{
UIView *map;
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
我发现实现这一目标的最好方法是使用手势识别器.目前还不清楚您是想自己识别缩放事件还是只是检测用户何时缩放/平移.
我不需要检测地图平移或缩放 - 我只关心用户是否已将手指放在MKMapView中的任何位置.如果需要以100%的调用和精度检测缩放或平移,则可能比这更复杂.我们真正需要的是MKMapView的开源实现,因此我们可以将其添加到委托中,以及许多其他功能.
这就是我所做的:实现一个无法阻止的手势识别器,并且无法阻止其他手势识别器.将其添加到地图视图并根据需要处理相关的触摸事件.
WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
self.lockedOnUserLocation = NO;
};
[mapView addGestureRecognizer:tapInterceptor];
Run Code Online (Sandbox Code Playgroud)
//
// 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
Run Code Online (Sandbox Code Playgroud)
//
// 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
Run Code Online (Sandbox Code Playgroud)
小智 5
我有同样的问题 - 我想在地图视图上绘制地图比例.为了做到这一点,我不得不拦截触摸事件,然后将它们发送回地图视图.不幸的是,当MKMapView不是事件的原始接收器时,一些平滑的平移和缩放动画不再起作用.
但是我已经找到了解决这个问题的方法 - 有点hacky但是有效:1.我已将MapScales UIView置于MKMapView之上,并关闭接收事件,以便底层MKMapView默认接收事件.2.我已经使用MyMainWindow类对UIWindow进行了子类化,并且我已经覆盖了该方法:
- (void) sendEvent:(UIEvent*)event {
[super sendEvent:event];
[self send_the_event_also_to_my_MapScales_component_with_use_of_listener_design_pattern];
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我的MapScales组件接收并可以对所有触摸事件做出反应,同时它不会破坏底层的MKMapView :)
| 归档时间: |
|
| 查看次数: |
16387 次 |
| 最近记录: |