2011-06-20 105 views
2

我有UIView有3个子视图,我需要处理触摸superview和子视图上的每一个触摸,但子视图拦截触摸。我怎样才能做到这一点? 谢谢。 UPD:那么更简单的方法不存在?事实上,除了继承之外,更容易发现事件。处理父视图触摸

回答

2

你可以使用

[subView setUserinteractionEnabled: NO]; 

这样,他们不会拦截触摸的事件禁用自受理触摸子视图,它们将被发送到仅父视图。

如果您希望两个视图都能接收事件,您可以在子视图中捕获事件,然后手动将它们发送到父视图。

+1

这实际上不是一个坏主意。 +1 –

+0

听到关于如何最好地在子视图中捕捉事件并将它们手动发送到父视图的一些指导会很酷。 :] – weienw

2

你需要重写触摸事件在如下子视图:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
    [super touchesBegan: touches withEvent: event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.nextResponder touchesMoved: touches withEvent:event]; 
    [super touchesMoved: touches withEvent: event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    [super touchesEnded: touches withEvent: event]; 
} 
+0

我不知道你在做什么,但我认为每个电话只需要调用super和* not * sefl.nextResponder。查看' - (void)touches *:withEvent:'的文档 – Olie