我在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 第二个按钮在 iPhone 上运行此应用程序时,当我按下此按钮时,突出显示状态有延迟。在装有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
选项将完成此操作。