Search code examples
iosswiftuicollectionviewuiscrollviewuipangesturerecognizer

Cancel scrolling in UIScrollView if scrolling in subview (containing scrollable UICollectionView) (SWIFT)


I have the current structure: (-> indicates subview)

UIScrollView

  • pageOne:UIView
  • pageTwo:UIView -> collection: UICollectionView

(pageOne and pageTwo are subviews of UIScrollView. pageTwo contains a CollectionView)

I am trying to cancel scrolling of the parent UIScrollView if scrolls (drags) are detected within the collection. I did this by constructing a UIPanGestureRecognizer for pageTwo. While a drag is detected, I set:

UIScrollView.isScrollingEnabled = false collection.isScrollingEnabled = true

When UIPanGestureRecognizer.State.ended (dragging in pageTwo stops), I set back

UIScrollView.isScrollingEnabled = true collection.isScrollingEnabled = false

I have verified that the function correctly identifies dragging / ending dragging, but for some reason although dragging within pageTwo is correctly detected, the UIScrollView continues to scroll. More specifically, upon dragging in pageTwo and successfully changing UIScrollView scroll to false, and collection scroll to true, the initial tap causes the UIScrollView to scroll (page technically, since isPagingEnabled is true for ScrollView), back to the top of pageOne.

  1. collection & UIScrollView delegate equal to the viewController class.
  2. DelayTouchDown is true for UIScrollView.
  3. collection & scrollView both scroll Vertically

Any suggestions?

(Sorry if my formatting for this question is incorrect, first time posting)


Solution

  • SOLVED

    The issue I was specifically having had to do with

    UIScrollView.isPagingEnabled = true
    

    Upon setting isPagingEnabled to false and having collection.isScrollingEnabled always set to true (versus switching to false as stated above), the UI works as anticipated.