【问题标题】:Delay of UIButton's highlighted state on iOS 7iOS 7 上 UIButton 高亮状态的延迟
【发布时间】:2013-11-22 21:58:49
【问题描述】:

我在 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 上的按钮会延迟高亮?

【问题讨论】:

  • 您能否展示将这些添加到视图中的方法。即整个方法。
  • 我在 viewDidLoad 方法中添加了这两个按钮。这个单一视图应用程序只是测试应用程序。我想检查按钮是否会在非常简单的应用程序中延迟 - 延迟就像在我的“普通”应用程序中一样。
  • 尝试删除本地化字符串然后测试
  • 不幸的是按钮在有和没有 NSLocalizedString 的情况下都有延迟。
  • 两个按钮在 ios 7 中都延迟突出显示还是只有一个?无论如何尝试设置 button2.adjustsImageWhenHighlighted = YES;和 button2.showsTouchWhenHighlighted = YES;

标签: ios objective-c uibutton ios7


【解决方案1】:

尝试在你的滚动视图子类中重载这个方法:

- (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】:

我的问题是我有一个 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;

这将允许您在按钮上滑动而不触发它,并且还可以让您按下按钮并触发其突出显示。希望这会有所帮助。

【讨论】:

    【解决方案3】:

    我不确定 OP 是否只是想要视觉反馈,但如果是这样,在代码中将 showsTouchWhenHighlighted 属性设置为 YES/true 或检查 IB 中的 Shows Touch On Highlight 选项即可实现。

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 1970-01-01
      • 2011-11-29
      • 2012-08-17
      • 2019-06-18
      • 2011-01-16
      • 2011-11-05
      • 2013-09-25
      • 2014-05-20
      相关资源
      最近更新 更多