UIButton touch is delayed when in UIScrollView

2019-01-08 05:28发布

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.

7条回答
我想做一个坏孩纸
2楼-- · 2019-01-08 05:36

Ok I've solved this by subclassing UIScrollView and overriding touchesShouldCancelInContentView

Now my UIButton that was tagged as 99 highlights properly and my scrollview is scrolling!

myCustomScrollView.h:

@interface myCustomScrollView : UIScrollView  {

}

@end

and myCustomScrollView.m:

@implementation myCustomScrollView

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view
    {
        NSLog(@"touchesShouldCancelInContentView");

        if (view.tag == 99)
            return NO;
        else 
            return YES;
    }
查看更多
我只想做你的唯一
3楼-- · 2019-01-08 05:38

Storyboard solution: Select the scroll view, open the "Attributes Inspector" and uncheck "Delays Content Touches"

enter image description here

查看更多
贼婆χ
4楼-- · 2019-01-08 05:42

Try to set UIScrollView delaysContentTouches property to NO.

查看更多
家丑人穷心不美
5楼-- · 2019-01-08 05:43

Swift 3 :

scrollView.delaysContentTouches = false
查看更多
仙女界的扛把子
6楼-- · 2019-01-08 05:54

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)
}
查看更多
Ridiculous、
7楼-- · 2019-01-08 05:54

In Swift 3:

import UIKit

class ScrollViewWithButtons: UIScrollView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        myInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        myInit()
    }

    private func myInit() {
        self.delaysContentTouches = false
    }

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

You can then use this ScrollViewWithButtons in IB or in code.

查看更多
登录 后发表回答