在iOS 7上延迟UIButton突出显示的状态
我在Xcode - Single View应用程序中创建了新项目。 应用程序只有两个按钮。
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];
当我在iOS 7上运行此应用程序时,第二个按钮在按下此按钮时延迟了突出显示的状态。 在iOS 6的iPhone上,第二个按钮非常完美。
为什么iOS 7上的按钮有突出显示的延迟?
我的问题是我有一个UIButton
作为分页UIScrollView
的子视图,所以我希望用户能够正确地滑过按钮所在的区域,而不用按按钮。 在iOS6中,如果你通过一个四舍五入的矩形按钮来做到这一点,它可以正常工作,但在iOS7中它也可以工作,但按钮不会触发它的亮点。 所以要解决这个问题,我使用longPressGestureRecognizer
实现了自己的UIButton
:
- (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];
}
}
}
}
然后,当你初始化longPressGestureRecognizer
,你会这样做:
self.longPressGestureRecognizer.minimumPressDuration = .05;
这将允许您在不触发按钮的情况下轻扫按钮,还可以让您按下该按钮并使其高亮触发。 希望这可以帮助。
尝试在滚动视图子类中重载此方法:
- (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
选项将完成此操作。
上一篇: Delay of UIButton's highlighted state on iOS 7
下一篇: iOS UILabel use negative background color as text color