Delay of UIButton's highlighted state on iOS 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?
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.
尝试在滚动视图子类中重载此方法:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
// fix conflicts with scrolling and button highlighting delay:
if ([view isKindOfClass:[UIButton class]])
return YES;
else
return [super touchesShouldCancelInContentView:view];
}
我不确定OP是否仅仅需要视觉反馈,但如果是这样,将代码中的showsTouchWhenHighlighted
属性设置为YES / true或检查IB中的Shows Touch On Highlight
选项将完成此操作。