2
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  NSLog(@"The tag %i",touch.view.tag);
  if( [touch.view isKindOfClass:[UIScrollView class]] ) {
    for( UIView *checkview in [self.view subviews] ) {
      if( [checkview tag] == 1 ) {
        infoIsVisibile = NO;
        [info removeFromSuperview];                         
      }
    }
  }
  [super touchesBegan:touches withEvent:event];
}

有人可以帮我吗,这只会检测子视图内的触摸,而不是主视图。

4

1 回答 1

2

您可以将 UIGestureRecognizer 连接到您感兴趣的视图。

(一般来说,手势识别器是一种更强大的机制,用于检测任意视图中的触摸,并且它们可以很好地与 UIWebViews 和 UIScrollViews 等倾向于吃传统触摸事件的视图配合使用。)

这是一个片段:

UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleSingleDoubleTap:)];
    singleFingerDTap.numberOfTapsRequired = 2;
    [self.theView addGestureRecognizer:singleFingerDTap];
    [singleFingerDTap release];

(此代码来自下面的链接文档——请注意 self.theView 可能是一些随机视图,存储为当前对象的成员。)

您会对手势识别器简化代码和减少怪异的程度感到惊讶(或震惊)。

这是苹果文档的链接:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html

于 2012-06-07T00:51:50.110 回答