我有一种情况,我想在另一个UIView之上放置一个UIView(不是我的代码),并将事件传播到最底层的视图。更具体一点的是,我想在一个MKMapView上放置一个UIView。
我有一个包含两个视图的UIView(作为子视图)。我希望我的uiview能够将事件从它的视图转发到它的子视图。
·H:
@interface MyUIView : UIView
@end
的.m:
#import "MyMapView.h"
#import <MapKit/MapKit.h>
#import "MapGestureRecognizer.h"
@implementation MyMapView
MKMapView *mkMapView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
mkMapView = [[MKMapView alloc] initWithFrame:frame];
[self addSubview:mkMapView];
// Create another view to place on top of the mkMapView
// This is not my view I do not control is codebase. I would like to put it on top of the mkMapView
// and still have the mkMapView receive touch events
UIView * otherView = [[UIView alloc] initWithFrame:frame];
otherView.backgroundColor = [UIColor clearColor];
[self addSubview:otherView];
MapGestureRecognizer *mgr = [[MapGestureRecognizer alloc] init];
mgr.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [[event allTouches] anyObject];
NSLog(@"touches began=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
[mkMapView touchesBegan:touches withEvent:event];
};
mgr.touchesCancelledCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [[event allTouches] anyObject];
NSLog(@"touches cancelled=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
[mkMapView touchesCancelled:touches withEvent:event];
};
mgr.touchesEndedCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [[event allTouches] anyObject];
NSLog(@"touches ended=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
[mkMapView touchesEnded:touches withEvent:event];
};
mgr.touchesMovedCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [[event allTouches] anyObject];
NSLog(@"touches moved=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
[mkMapView touchesMoved:touches withEvent:event];
};
[self addGestureRecognizer:mgr];
return self;
}
@end
我实现了自己的手势识别器来拦截这些事件,我试图将截获的事件传递给mkMapView。我看到日志消息,但它似乎没有通过事件,因为我可以使mkmapview滚动/平移。
答案 0 :(得分:0)
如果另一个视图位于具有附加手势识别器的视图顶部,则手势识别器与最顶层视图一起工作,识别器不会触发。一种快速而肮脏的方法是将同一组识别器分配给otherView,并在不需要时适当地删除它们。不需要在接收手势的同一视图上执行操作,因此在Otherviews中接收平移手势然后在后台平移地图应该有效。
答案 1 :(得分:0)
将您的代码更改为低于1 - >
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
mkMapView = [[MKMapView alloc] initWithFrame:frame];
[self addSubview:mkMapView];
UIView * otherView = [[UIView alloc] initWithFrame:frame];
otherView.backgroundColor = [UIColor clearColor];
[self addSubview:otherView];
return self;
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
return mkMapView;
}