SebastianThiebaud / STTweetLabel

Deprecated - A UILabel with #hashtag @handle and links tappable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Supporting UITapGestureRecognizer based touches

RyanCopley opened this issue · comments

Currently, if you have STTweetLabel inside of a UIScrollView, the touch system (touchesBegan, etc) do not work..

Implementing UITapGestureRecognizer is pretty easy, somewhere in your code (You know it best-- I put it in the end of setupLabel), add:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[self addGestureRecognizer:gestureRecognizer];

Which calls:

- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer {
    CGPoint touchLocation = [recognizer locationInView:recognizer.view];

    if (!CGRectContainsPoint(_textView.frame, touchLocation))
        return;

    int charIndex = (int)[self charIndexAtLocation: touchLocation];

    [_rangesOfHotWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSRange range = [[obj objectForKey:@"range"] rangeValue];

        if (charIndex >= range.location && charIndex < range.location + range.length) {
            _detectionBlock((STTweetHotWord)[[obj objectForKey:@"hotWord"] intValue], [_cleanText substringWithRange:range], [obj objectForKey:@"protocol"], range);

            *stop = YES;
        }
    }];
}

So far this works flawlessly on my implementation, but there should definitely be a discussion about this before I even propose a PR :)

Hey :)

Just a question, why is it not working in a UIScrollView but working in a UITableView?

Scroll views are weird... Instead of your typical UI Element "stacking" on its subview, it embeds it inside of it. Your touches get sent to the scroll view to move it around, and never get passed "through" it to its contents...

If that makes sense :)

Ok, let's do this:

  • (void)handleTapFrom:(UITapGestureRecognizer *)recognizer;
  • (void)touchesBegan;

-->

  • (void)handleTapAtLocation:(CGPoint)location;

Be sure to implement the different states of the gesture recognizer like it's right now in touchesBegan, Ended...

Actually that's something I wanted to mention, I'm not sure it's needed BUT I don't know the code all that well (and less than you obviously)...

The current just-touchesEnded method works, but I'm not sure if it handles every behavior that I use.

I'll look into it this weekend and try to come up with something

Edit: Merged comments, mobile was being stupid

Nice. I'm pretty busy this week-end but I can give a hand next week.

I just googled the UITapGestureRecognizer protocol and it should be no problem to easily port it over, they both support all the different events.

My only worry now is code envy... It isn't all that awesome to have the same code in two places, so it might be better to abstract it out and let both touches* and the UITapGestureRecognizer call the same function...

I dunno, I'll look into it and try to do a good job, I like clean code :3

Oh, I just noticed that that is what you suggested.