UIButton Doesn't allow UIScrollView to scroll

前端 未结 3 1806
予麋鹿
予麋鹿 2021-02-04 07:44

I have many UIButtons within a UIScrollView. Those UIButtons have actions attached to them in Touch Down Repeat. My problem

3条回答
  •  佛祖请我去吃肉
    2021-02-04 08:47

    So view.canCancelContentTouches = YES works OK if you don't also have delaysContentTouches set to YES. If you do though, the buttons won't work at all. What you need to do is subclass the UIScrollView (or UICollectionView/UITableView) and implement the following:

    Objective-C

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view {
        if ([view isKindOfClass:UIButton.class]) {
            return YES;
        }
        return [super touchesShouldCancelInContentView:view];
    }
    

    Swift 2

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

提交回复
热议问题