UIButton touch is delayed when in UIScrollView

前端 未结 7 1550
遥遥无期
遥遥无期 2020-11-30 19:01

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

7条回答
  •  野性不改
    2020-11-30 19:36

    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)
    }
    

提交回复
热议问题