我在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上运行此应用程序时,当我按下此按钮时,第二个按钮会突出显示突出显示状态。在iPhone上使用iOS 6秒按钮非常完美。
为什么iOS 7上的按钮会突出显示延迟?
答案 0 :(得分:2)
我的问题是我有一个UIButton
作为分页UIScrollView
的子视图,所以我希望用户能够在没有按下按钮的区域右键滑动按钮。在iOS6中,如果你通过圆角矩形按钮执行此操作,它可以正常工作,但在iOS7中它也可以工作,但按钮不会触发它的突出显示。为了解决这个问题,我使用UIButton
:
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];
}
}
}
}
然后在初始化longPressGestureRecognizer
时执行:
self.longPressGestureRecognizer.minimumPressDuration = .05;
这将允许您在不触发按钮的情况下滑动按钮,并且还可以按下按钮并使其突出显示触发器。希望这会有所帮助。
答案 1 :(得分:1)
尝试在uiscrollview子类中重写此方法:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
// fix conflicts with scrolling and button highlighting delay:
if ([view isKindOfClass:[UIButton class]])
return YES;
else
return [super touchesShouldCancelInContentView:view];
}
答案 2 :(得分:0)
我不确定OP是否只需要视觉反馈,但如果是这样,在代码中将showsTouchWhenHighlighted
属性设置为YES / true或检查IB中的Shows Touch On Highlight
选项将实现