【问题标题】:Can't recognise the tap on UILabel which is out of bounds of the parent view无法识别 UILabel 上超出父视图范围的点击
【发布时间】:2013-12-19 08:04:01
【问题描述】:

我有一个情况:

            -------------
            |           |
-------------------------
| UILabel               |
-------------------------
            | UIView    |
            |           |
            -------------

我需要检查用户是否点击了 UILabel。 UILabel 是 UIView 的子视图。

result.userInteractionEnabled = YES;
textLabel.userInteractionEnabled = YES;
[result addSubview: textLabel];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(reportTap:)];
tapRecognizer.delaysTouchesBegan = YES;
[textLabel addGestureRecognizer: tapRecognizer];

所以点击只能在 UIView 上被识别,我试图覆盖 - (UIView *) hitTest: (CGPoint)point withEvent: (UIEvent *)event 但它从未被调用,试图将 tapRecognizer 添加到 UILabel ==> 这也不能解决问题。有什么帮助吗?

【问题讨论】:

  • 你覆盖了哪个 hitTest?标签还是当前视图?即使您将 tapRecognizer 添加到 uilabel 它仍然超出范围,因此它不会注册

标签: ios objective-c uiview uigesturerecognizer


【解决方案1】:

希望这会对你有所帮助。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

BOOL isInside = [super pointInside:point withEvent:event];

// identify the label view subclass
UILabel * textLabel = (UILabel *)[result viewWithTag:1];
CGPoint inLabelSpace = [self convertPoint:point toView:textLabel];

BOOL isInsideLabel = [textLabel pointInside:inLabelSpace withEvent:nil];

if (YES == isInsideLabel) {

    return isInsideLabel;

} // if (YES == isInsideLabel)


return isInside;

}

【讨论】:

    【解决方案2】:

    你在调用[label sizeToFit]吗? 如果是这样,你的情况可能是这样的

                -------------
                |           |
    ------------
    | UILabel   |           |
    ------------
                | UIView    |
                |           |
                -------------
    

    张贴标签的框架并检查它是否正确。

    【讨论】:

      【解决方案3】:

      当您创建新视图时,您可以作为参数传递 - 父级实例,然后将此标签添加到父级实例中,或者添加到此视图中,或者在添加视图时将点击识别器添加到父级; 例如

          UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bounds.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
      
          UIlabel *label = [UILabel alloc] init];
          [childView addSubview:label];
          [self.view addSubview:childView];
      
      
      
          UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleSingleTap:)];
          [childView addGestureRecognizer:tap];
          tap.delegate = self;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多