您的位置:首页 > 其它

同一个view上添加两个相同的手势

2014-11-18 17:55 323 查看
因为使用第三方库,第三方库里已经实现添加了点击事件,然后没有相应的对外接口,没办法添加手势响应事件,需要另外再注册一个手势。但是很遗憾,ios不支持一个view里同时注册两个相同的手势,只会响应后添加的那个手势。除非强制重写没有对外开放的那个接口,但这肯定不是解决问题的办法。
想到一个办法可以尝试解决这个问题。再添加一层透明layer,然后在layer上注册手势,并添加响应时间来实现类似于一个view上添加两个相同的手势的功能。我没有添加一层蒙板,因为之前的 mapView 上添加手势,而mapView又是添加在self.view上的。我直接在self.view上注册手势。

@property (nonatomic,
retain) UITapGestureRecognizer *tapMapGestureRecognizer;

self.tapMapGestureRecognizer =

[[UITapGestureRecognize alloc initWithTarget:sel action:@selector(tapMap:)];

self.tapMapGestureRecognizer.delegate =
self;

[self.view
addGestureRecognizer:self.tapMapGestureRecognizer];

手势响应的方法为:

-(void)tapMap:(UITapGestureRecognizer *)gestureRecognizer
{

}

然后实现手势的代理方法为:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{

return
YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch
*)touch
{

//如果点击的是annotationView,则不响应地图选点事件(返回选点界面有点慢,查看什么问题)

if (gestureRecognizer ==
self.tapMapGestureRecognizer && [touch.view
isKindOfClass:[CMPoiLabelBallonView class]])
{

return NO;
}

return
YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer
{

return
YES;
}

特别要注意的地方就是最后一个代理方法的实现,在模拟器上调试,总是不响应事件,找了很久的原因才发现原来是这个代理方法没有实现。因为这些代理方法都是optional可选的,不是强制性的,需要特别注意下。
然后就是上面的第二个方法里的处理,在识别手势时,点击地图,会弹出一个气泡,然后下次再点击气泡,当然不能响应self.view的点击事件了,因此需要这么处理下。

另外还有一种处理方法,没有细究,姑且也放在这里。UIView是UIResponder的子类,在UIResponder的类里有几个实例方法:

- (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;

这几个实例方法会自动捕获view上的任何手势,当然既然能捕获所有手势,很自然就需要费一番力气来分别处理这些手势了。可以参看下面的例子来操作。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

if (touches.count >
0) {

UITouch *touch = [touches
anyObject];

CGPoint point = [touch
locationInView:self];
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

if (touches.count >
0) {

UITouch *touch = [touches
anyObject];

CGPoint point = [touch
locationInView:self];

CLLocationCoordinate2D coordinate = [self
convertPoint:point
toCoordinateFromView:self];

// [self.tapDelegate mapView:self.mapView onMapClick:coordinate];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐