UIButton Doesn't allow UIScrollView to scroll

前端 未结 3 1760
予麋鹿
予麋鹿 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:34

    As long as you have the Cancellable Content Touches in Interface Builder set it should work. You can also set it in code:

    scrollView.canCancelContentTouches = YES;
    
    0 讨论(0)
  • 2021-02-04 08:34

    Use a UITapGestureRecognizer with delaysTouchesBegan as a property set to true.

    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
提交回复
热议问题