开发者

UIScrollView and detecting subview of tap gesture

I've added a TapGestureRecognizer to my self.view:

    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tap];
    [tap release];

The view contains a single UIScrollView with images and labels. I want to detect if the user taps on a label or not.

- 开发者_StackOverflow社区(void)singleTap:(UIGestureRecognizer*)gestureRecognizer {

    CGPoint pt = [gestureRecognizer locationInView:self.view];

    UIView *v = [self.view hitTest:pt withEvent:nil];
    if ([v isKindOfClass:[UILabel class]]) {
        NSLog(@"label!");
        return;
    }   
    // else do other stuff if its not a label

However I don't see the label! in my log.


I think it's because userInteractionEnabled is by default NO on UILabels. Try turning that on.

EDIT: It was really a guess, but just to confirm, Apple docs on [UIView hitTest:withEvent:] state:

This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.


Your subviews, such as the labels themself, actually hide the user interactions from the underlying view.

Why don't you add the gesture recognizers to your label(s). Alternatively you may want to use UIButton for the labels.

Or -

if you do not want to determine which label has been touched, you may want to add an invisible view (an empty view, neither a hidden one nor one with alpha=0) on top of all labels and add the gesture recognizers to those.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜