7

我在 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];

当我在 iPhone 上使用 iOS 7 第二个按钮运行此应用程序时,当我按下此按钮时,会出现突出显示状态的延迟。在带有 iOS 6 第二个按钮的 iPhone 上完美运行。

为什么 iOS 7 上的按钮会延迟突出显示?

4

3 回答 3

2

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.

于 2014-01-26T01:22:37.023 回答
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];
}
于 2014-01-24T13:07:44.167 回答
0

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

于 2015-06-19T18:32:14.807 回答