Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

ios - UIButton touch is delayed when in UIScrollView

I'm running into a small issue in my app.

I essentially have a series of UIButtons added as subviews in a UIScrollView which is part of a nib. Every time I tap on a button there is a noticeable delay before the button is highlighted. I essentially have to hold it for about half a second before the button dims and appears selected.

I'm assuming this is because the UIScrollView needs to determine if the touch is a scroll or if it's a touch that is meant for a subview.

Anyways, I'm a little unsure on how to proceed. I simply want the button to appear selected as soon as I tap it.

Any help is appreciated!

Edit:

I've tried setting delaysContentTouches to NO but scrolling becomes almost impossible since a majority of my scrollView is filled with UIButtons.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Jeff's solution wasn't quite working for me, but this similar one does: http://charlesharley.com/2013/programming/uibutton-in-uitableviewcell-has-no-highlight-state

In addition to overriding touchesShouldCancelInContentView in your scroll view subclass, you still need to set delaysContentTouches to false. Lastly, you need to return true rather than false for your buttons. Here's a modified example from the above link. As commenters suggested, it checks for any subclass of UIControl rather than UIButton specifically so that this behavior applies to any type of control.

Objective-C:

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.delaysContentTouches = false;
    }

    return self;
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    if ([view isKindOfClass:UIControl.class]) {
        return true;
    }

    return [super touchesShouldCancelInContentView:view];
}

Swift 4:

override func touchesShouldCancel(in view: UIView) -> Bool {
    if view is UIControl {
        return true
    }
    return super.touchesShouldCancel(in: view)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...