7

I created new project in Xcode - Single View app. App have only two buttons.

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setBackgroundColor:[UIColor greenColor]];
[button1 setFrame:CGRectMake(0, self.view.frame.size.height-40-100, self.view.frame.size.width, 40)];
[button1 setTitle:NSLocalizedString(@"button 1", nil) forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundColor:[UIColor greenColor]];
[button2 setFrame:CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 40)];
[button2 setTitle:NSLocalizedString(@"button 2", nil) forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button2];

When I run this app on iPhone with iOS 7 second button have delay of highlighted state when I press this button. On iPhone with iOS 6 second button works perfect.

Why button on iOS 7 have delay of highlighted?

vzm
  • 2,440
  • 6
  • 28
  • 47
gcalikpl
  • 352
  • 1
  • 7
  • 17
  • Can you show the method where you are adding these to your view. i.e the whole method for this. – sbarow Nov 11 '13 at 07:10
  • I'm adding this two buttons in viewDidLoad method. This sigle view app is only test app. I want check if button will be have delay in very very simple app - delay like in my 'normal' app. – gcalikpl Nov 11 '13 at 07:18
  • try to remove the localisation string and then test – Retro Nov 11 '13 at 07:23
  • Unfortunately button have delay with and without NSLocalizedString. – gcalikpl Nov 11 '13 at 07:38
  • 1
    Both buttons have delayed highlighting in ios 7 or just one? anyway try setting button2.adjustsImageWhenHighlighted = YES; and button2.showsTouchWhenHighlighted = YES; – Pochi Nov 11 '13 at 07:43
  • Only button2 have delay. – gcalikpl Nov 11 '13 at 07:45
  • see previous comment, i edited it by mistake – Pochi Nov 11 '13 at 07:47
  • I was try it and unfortunately it doesn't work. – gcalikpl Nov 11 '13 at 08:06
  • I got the same problem, but in my case it is a UIToolbar. It works on the simulator but not on a real devices. – Leandros Nov 21 '13 at 23:33
  • murzynpl, I would suppose that you have some sort of scroll view in buttons hierarchy (collection/table view) and thats why highlighting is delayed. I had the same problem and solved it with disabling `delaysContentTouches` in scroll view. But in that case scrolling becomes interrupted :( – brigadir Dec 02 '13 at 12:46
  • @brigadir - ditto, I'm having the same issue because my buttons are on a scrollview – Ser Pounce Jan 24 '14 at 12:05
  • CoDEFRo, in my case I solved this problem by overloading `touchesShouldCancelInContentView:` method. Take a look at my answer to this question. – brigadir Jan 24 '14 at 13:09
  • @brigadir - thank you for that, going to take a look at it. – Ser Pounce Jan 25 '14 at 04:49

3 Answers3

2

My problem was I had a UIButton as a subview of a paging UIScrollView, so I wanted the user to be able to do a right swipe over the area where the button was without it pressing the button. In iOS6 if you do this over a rounded rect button, it works fine, but in iOS7 it also works but the button won't trigger it's highlight. So to fix this, I implemented my own UIButton using the longPressGestureRecognizer:

- (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer
{
    if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
    {

        CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];

        if (CGRectContainsPoint(self.bounds, touchedPoint))
        {
            [self addHighlights];
        }
        else
        {
            [self removeHighlights];
        }
    }
    else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        if (self.highlightView.superview)
        {
            [self removeHighlights];
        }

        CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];

        if (CGRectContainsPoint(self.bounds, touchedPoint))
        {
            if ([self.delegate respondsToSelector:@selector(buttonViewDidTouchUpInside:)])
            {
                [self.delegate buttonViewDidTouchUpInside:self];
            }
        }
    }
}

Then when you initialize the longPressGestureRecognizer and you do:

self.longPressGestureRecognizer.minimumPressDuration = .05;

This will allow you to do a swipe over the button without it triggering it, and will also enable you to press the button and have its highlight trigger. Hope this helps.

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169
1

Try to overload this method in your scroll view subclass:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    // fix conflicts with scrolling and button highlighting delay:
    if ([view isKindOfClass:[UIButton class]])
        return YES;
    else
        return [super touchesShouldCancelInContentView:view];
}
brigadir
  • 6,874
  • 6
  • 46
  • 81
  • For my case this didn't work out for some reason, but thanks for posting. I also posted a solution I used for the specific case I was having trouble with, don't know if it'll help. – Ser Pounce Jan 26 '14 at 01:23
0

I'm not sure if the OP just wants visual feedback, but if so, setting the showsTouchWhenHighlighted property to YES/true in code or checking the Shows Touch On Highlight option in IB will accomplish that.

Rob C
  • 4,877
  • 1
  • 11
  • 24