我有一个子类UIView,我将调用customView。我想启用触摸,以便用户可以操作具有手势识别器和其他控件的子视图,但是视图本身我不想被触摸,以便在视图下方绘制的视图仍然可以触摸。换句话说,customView将在应用程序中的其他视图之上绘制,但我仍然希望下面的视图是可触摸的,同时允许触摸customView的子视图。
我试过像这样使用touchesBegan,但这不起作用。有任何想法吗?谢谢你的阅读!
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
//I've tagged the views that I want to be touchable.
if ([touch view].tag == 1000 || [touch view].tag == 2000 || [touch view].tag == 3000) {
self.userInteractionEnabled = YES;
} else {
self.userInteractionEnabled = NO;
}
}
答案 0 :(得分:1)
您需要做的是在customView中实现以下方法:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
//check if one of the subviews was hit, if so forward the touch event to it
for (UIView *view in self.subviews){
if (CGRectContainsPoint(view.frame, point))
return view;
}
// use this to pass the 'touch' upward in case no subviews trigger the touch
return [super hitTest:point withEvent:event];
}
答案 1 :(得分:0)
然后将这些子视图大写添加到您的customview.other副本,您的自定义视图不应让他们触摸。并将自定义视图放在这些子视图下方,以便其他任何区域都可以触摸您选择的子视图。