2009-08-17 81 views
0

我有一个很大的UIView响应触摸,它覆盖了大量的小UIViews对触摸反应不同。是否有可能触摸屏幕上的任何地方并滑动,并让每个视图知道它是否被触摸?我可以检测是否触摸了更高的子视图吗?

例如,我将手指放在左上方并向右下方滑动。 touchesBegan/Moved由baseView收集。当我通过itemView1,itemView2和itemView3时,控制权交给他们。如果我在itemView2上抬起手指,它会执行itemView2的touchesEnded方法。如果我将手指放在任何项目上,它都会执行baseView的touchesEnded。

此刻,如果我触及baseView,touchEnded始终是baseView,而较高的itemView被忽略。

任何想法?

回答

1

如果我理解正确,touchesEnded事件检测到的,但不是由需要知道它的子视图。我认为这可能适用于您:

在通用文件中,将TOUCHES_ENDED_IN_SUPERVIEW定义为@“触摸在超级视图中结束”。

在含认为烧成touchesEnded方法中,添加

[[NSNotificationCenter defaultCenter] postNotificationName: TOUCHES_ENDED_IN_SUPERVIEW object: self]; 

在子视图的的touchesBegan,添加

[[NSNotificationCenter defaultCenter] addObserver: self 
    selector: @selector(touchesEnded:) 
    name: TOUCHES_ENDED_IN_SUPERVIEW 
    object: self.superview]; 

在子视图的touchesEnded方法,使用您事件的正常逻辑,并且还添加

[[NSNotificationCenter defaultCenter] removeObserver: self name: TOUCHES_ENDED_IN_SUPERVIEW object: self.superview]; 

请记住在你的dealloc中放置[[NSNotificationCenter defaultCenter] removeObserver:self],以防万一可能离开页面而没有得到touchesEnded事件。

您可能希望通知将其消息发送到一个特殊的touchesEndedInSuperview方法,该方法将调用touchesEnded本身,但这取决于您是否有任何特殊的处理。

+0

谢谢Amagrammer,我给这个一杆。这对我来说听起来很不错,但我已经把我的代码分成了暂时不涉及这个问题,我不想再次开启这些门。如果它有效,我一定会让你知道的。 – 2009-08-17 14:24:58

0

您可以使用这样的事情:


-(void)touchesEnded: (NSSet *)touches 
      withEvent: (UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView: touch.view]; 
    if (CGRectContainsPoint(control1.frame, location)) { 
     [self control1Action]; 
    } else if (CGRectContainsPoint(control2.frame, location)) { 
     [self control2Action]; 
    } 
}