touchesBegan:withEvent: on UIView in UITableViewCell called with noticable delay

I have a UIView that has touchesBegan:withEvent: to handle touches, this UIView is in the UITableViewCell.

When I touch it nothing happens, when I "long press" it then the touch event is trigerred.

Is there a setup I skipped? What can I do to have instant reaction to touch on my UIView?

This is the code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    [self handleTouchAtLocation:touchLocation];
}

When I put NSLog to this method, this is what is traced (formatting is mine):

touchesBegan:touches withEvent:<UITouchesEvent: 0x170078f00> 
timestamp: 281850 touches: {(
    <UITouch: 0x1781486c0> 
        phase: Began tap 
        count: 1 window: <UIWindow: 0x145e0ab30; 
                    frame = (0 0; 320 568); 
                    gestureRecognizers = <NSArray: 0x178240720>; 
                    layer = <UIWindowLayer: 0x17802b800>> 
        view: <CSRateView: 0x145d49850; 
            frame = (50 54; 220 40); 
            autoresize = TM; 
            layer = <CALayer: 0x17003c0c0>> 
        location in window: {207, 81} 
        previous location in window: {207, 81} 
        location in view: {157, 7} 
        previous location in view: {157, 7}

)}

Upvotes: 1

Views: 927

Answers (1)

arturdev
arturdev

Reputation: 11039

Disable delaysContentTouches for tableView
self.tableView.delaysContentTouches = NO;

Then, in you cell class:

@implementation MyCell

- (void)awakeFromNib
{
    for (id view in self.subviews) {
        if ([view respondsToSelector:@selector(setDelaysContentTouches:)]){
            [view setDelaysContentTouches:NO];
        }
    }
}

@end

-- EDIT--

Or make a category on UITableViewCell

#import "UITableViewCell+DelayTouches.h"

@implementation UITableViewCell (DelayTouches)

- (void)setDelayContentTouches:(BOOL)enable
{
    for (id view in self.subviews) {
        if ([view respondsToSelector:@selector(setDelaysContentTouches:)]){
            [view setDelaysContentTouches:enable];
        }
    }
}

@end

And in cellForRow:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = ...
    [cell setDelayContentTouches:NO];
    return cell;
}

Upvotes: 3

Related Questions