0
votes

I have two collection views inside a horizontal scrollview. To simplify things I have no navigation bar or toolbar/TabBar. So, I can scroll perfectly in the collection views (vertical scrolling) and perfectly as well between the collection views (using the horizontal scrollview). However, I am being able to somehow drag the collection views. In order to differentiate, I set different background colors. So, I have a horizontal scrollview that has a background color of yellow and two subviews: the first collection view that has a background color of red and a second collection view that has a background color of blue. The horizontal scroll view works similarly to a UIPageControl.

As one of the problems I had was that when I scrolled to the top in both scrollviews, after the collection view scroll indicator had disappeared (the collection view stopped scrolling), I was not being able to scroll to the top anymore (get the bouncy effect) in the collection view but rather was receiving the touch on the superview (the horizontal scrollview) and then, the collection view was being shifted/misplaced by 20 pixels, I solved this problem by subclassing the horizontal scroll view and changing the gesture recognizer method:

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    print("RECEIVING GEST RECOGNIZER IN CUSTOM SCROLLVIEW")
    if gestureRecognizer.isKindOfClass(UIPanGestureRecognizer){
        print("GEST IS PANGESTURERECOGNIZER")
         let gesture = gestureRecognizer as! UIPanGestureRecognizer
         if gesture.translationInView(self).y != 0  {
                print("DO NOT ALLOW VERTICAL SCROLLING")
                return false
         }
    }
    return true
}

It seems like a hacky solution and is not perfect since when I started the gesture only moving horizontally the gesture is recognized since the translation.y == 0 and then I can move on the y axis as well. Other methods like func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { that would check continuously instead of only at the beginning do not recognize horizontal scrolling somehow. So, the only solution I can think of right now is overriding the UIPanGestureRecognizer of the horizontal scrollview to allow only horizontal translation but seems so hacky. Please help. Objective-C solutions are as welcome as Swift ones.

1
I don't understand why do you even need the UIPanGestureRecognizer. Both collectionViews are scrollable vertically and the scrollView is scrollable horizontally. I might have misunderstood the question. A screenshot of the drag result would help. - Mercurial

1 Answers

0
votes

So, the problem was somewhat related to the UIStatusBar. I don't understand why but when the status bar is present, it was possible to "drag" the collection view 20 pixels down every time I swiped the scrollview of the collection view down, scrolling its way up to the top. I took the status bar off by calling UIApplication.sharedApplication().statusBarHidden = false and problem was solved!