Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove touch delay from custom UIButton in UITableViewCell

I have a custom button where the image changes upon a press. This button is in the contentView of a UITableViewCell within a UITableView.

There is a noticeable delay when pressing the button before the image changes. Take the button out of the cell and into a UIView the change of image on press is instant.

How do I remove this delay?

It's worth pointing out that I have tried setting delaysContentTouches on the UITableView but it makes no difference that I can see.

Here is some test code that proves the issue. Create new project and add a UITableView to the storyboard.

#import "JWViewController.h"

@implementation JWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.delaysContentTouches = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return return 65.0f; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"apple"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"apple_selected"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(0, 0, 65, 65);
    [cell.contentView addSubview:button];
    return cell;
}

@end

I have searched and there appears to be a number of other posts relating to this but none with a working answer.

This blog post doesn't appear to work either in this simple case.

like image 401
JMWhittaker Avatar asked Nov 06 '13 18:11

JMWhittaker


1 Answers

Finally got solution of your issue.

Try this, it will work fine. Add steps in your existing code:- 1.) Set tag of the button which is in content view. 2.) Add target on that button.

- (void)viewDidLoad
    {
        [super viewDidLoad];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.delaysContentTouches = NO;
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; }
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65.0f; }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"apple.jpg"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"apple_selected.png"] forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 0, 65, 65);
        button.tag = indexPath.row;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventAllTouchEvents];
        [cell.contentView addSubview:button];
        return cell;
    }

    - (void)buttonPressed:(UIButton *)sender
    {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];

        for (UIView *currentView in cell.subviews)
        {
            if ([NSStringFromClass([currentView class]) isEqualToString:@"UITableViewCellScrollView"])
            {
                UIScrollView *svTemp = (UIScrollView *) currentView;
                [svTemp setDelaysContentTouches:NO];
                break;
            }
        }
    }
like image 104
user3757883 Avatar answered Nov 08 '22 14:11

user3757883