Nesting UIScrollViews and getting info, when the scrolling happened
I tried reading the other questions about similar cases, but no luck, so here’s a question. I have a setup of nested UIScrollView’s and while the scrolling all happens fine, I don’t seem to any of the delegate events.
Outer UIScrollView is paginated and scrolls vertically. Inside I have multiple "rows" of horizontally scrolling paginated UIScrollViews. Now, what I want, is to force the inner scrollers to a specific location when vertical scroll happened.
I made the ViewController a delegate and am calling the scrollViewDidScroll, but it only fires when I initially set up the scrollers and never if I actually touch the device and scroll the grid.
I’m listening both inner and outer scrollers and they both ignore firing any events when I scroll on the device.
I’m quite new to Ob开发者_如何学Cj-C and Cocoa dev, so I might be completely missing something obvious, but any help is appreciated.
I created a small test project and it all seemed to work fine. Make sure to call [scrollView setDelegate: self]
on all of the scrollViews (assuming the controller itself is the delegate. Apart from that, always make sure the scrollViews have a bigger contentSize
than their frames, if not then scrollDidScroll:
shouldn't fire at all.
My small project basically had one pointer to the outer scrollView and an NSArray of all the inner scrollViews, every time the outer one scrolled (just had a small check in the scrollViewDidScroll:
delegate method) I simply recursively changed all the inner scrollViews' contentOffsets to CGPointMake(0.0, 0.0)
.
This isn't the most optimised approach as actually only the previously visible scrollViews would need to be reset (all the other ones should already be at that offset anyway). Also, as a side note, depending on the number of inner scrollViews this approach of adding them all to the outer scrollView may severely damage performance (the app needs to have all the subviews in memory), so you may want to think about reusing scrollViews that are not currently visible (Apple sample project PhotoScroller has some neat tricks on reusing subviews in a UIScrollView, which basically mimics how UITableView works).
If you have any other issues, let me know I'll post the sample project.
精彩评论