9

The Problem: When a user taps on a UIButton in a UITableViewCell, the button will only highlight on a long tap, not on a quick tap. The desired behaviour for this button to highlight regardless of the tap duration.

Unfortunately: Setting delaysContentTouches to NO on any UIScrollView or UITableView is not an option because of other undesired side effects.

So: How can I get around this - is there a way to forward touches to the button, bypassing the delaysContentTouches value?

Jordan Smith
  • 10,310
  • 7
  • 68
  • 114

5 Answers5

3

just set delayContentTouches = false to whatever scrollview you're using (UITableView or UICollectionView). That should do it.

As the doc says:

A Boolean value that determines whether the scroll view delays the handling of touch-down gestures. If the value of this property is true, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is false , the scroll view immediately calls touchesShouldBegin(_:with:in:). The default value is true.

facumenzella
  • 559
  • 3
  • 10
0

This worked For me :

    @weakify(self);
[self.publishButton bk_addEventHandler:^(id sender) {
    @strongify(self);
    if (self.clickBlock) {
        self.clickBlock(self.draftModel);
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.publishButton.backgroundColor = [UIColor whiteColor];
    }];
} forControlEvents:UIControlEventTouchUpInside];

[self.publishButton bk_addEventHandler:^(id sender) {
    self.publishButton.backgroundColor = [UIColor redColor];
} forControlEvents:UIControlEventTouchDown];
Jonguo
  • 681
  • 1
  • 10
  • 17
-1

Set the tag of the button to "1" in the prototype cell.

In you cellForRowAtIndexPath you should link the UIButton to a method:

UIButton *button = (UIButton *)[cell viewWithTag:1];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];

Then in the method all you do is:

-(void) aMethod: (id) sender{


    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.cartTableView];
    NSIndexPath *indexPath = [self.cartTableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
     {
        //Do your stuff
    }
}

This will not add any delay before it runs the code.

Mika
  • 5,807
  • 6
  • 38
  • 83
-1

You need to create a UIButton category (or subclass, if you don't want to affect all your other buttons), and set highlight = YES in touchesBegan.

See the code in this answer for an example implementation.

Community
  • 1
  • 1
Guillaume Boudreau
  • 2,676
  • 29
  • 27
-1

Use the main thread.

    dispatch_async(dispatch_get_main_queue(), ^{

    });
doxsi
  • 1,002
  • 19
  • 42