Iphone 点击测试以避免子视图,但希望另一个子视图工作
我有一个背景子视图,它只有灰色的,并且在它上面有可以拖动的瓷砖。这一切都发生在一个视图控制器中。问题是,当我做命中测试并试图避开背景视图时,由于瓷砖位于该视图的顶部,它仍然可以看到这些坐标,并避免触摸移动事件,因为我返回了nil 代码如下:Iphone 点击测试以避免子视图,但希望另一个子视图工作,iphone,ios,objective-c,ipad,cocoa-touch,Iphone,Ios,Objective C,Ipad,Cocoa Touch,我有一个背景子视图,它只有灰色的,并且在它上面有可以拖动的瓷砖。这一切都发生在一个视图控制器中。问题是,当我做命中测试并试图避开背景视图时,由于瓷砖位于该视图的顶部,它仍然可以看到这些坐标,并避免触摸移动事件,因为我返回了nil 代码如下: - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { for (UIView *view in self.subviews) { //This checks
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
//This checks if touches are within the region of the custom view based on its cordinates.
if (CGRectContainsPoint(view.frame, point)) {
NSLog(@"touched the boat view %f %f and %f %f",view.frame.origin.x, view.frame.origin.y, point.x, point.y);
if ([view isEqual:backgroundView]) {
return nil;
}
else
return view;
}
}
return nil;
}
如果您可以看到我正在验证背景视图,但由于我的分幅位于背景视图的顶部,因此不会根据我的其他逻辑拖动这些分幅。我已经尝试将userInteractiveEnabled设置为No,但这不起作用。有什么建议吗?您应该拥有uiview的子类,并在此子类上实现hitTest:
@implementation EDPassTroughView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self)
return nil;
else
return hitView;
}
@end
您应该拥有uiview的子类,并在此子类上实现hitTest:
@implementation EDPassTroughView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self)
return nil;
else
return hitView;
}
@end
我尝试过这种方法,但它没有调用子类中的hitTest。为什么会这样?谢谢。elio.d,你的答案是正确的,我所做的是为所有子类到超类制作这些触控事件,并在超类上交互所有内容,但它无法工作,我需要将其缩小到子类级别。我尝试了这种方法,但它没有调用子类中的hitTest。为什么会这样?谢谢。elio.d,你的答案是正确的,我所做的是为所有子类到超类制作这些触控事件,并在超类上交互所有内容,但它不能这样工作,我需要将其缩小到子类级别。