14

I have many UIButtons within a UIScrollView. Those UIButtons have actions attached to them in Touch Down Repeat. My problem is my scroll view doesn't scroll when I touch a button then scroll, but it works fine if I touch outside of the button.

How can I allow my scroll view to scroll even though a button is pressed?

etolstoy
  • 1,798
  • 21
  • 33
Matt S.
  • 13,305
  • 15
  • 73
  • 129

3 Answers3

14

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;
Domestic Cat
  • 1,434
  • 11
  • 18
9

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)
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0

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

Rudolf J
  • 517
  • 4
  • 10