I have looked on stackoverflow, but unable to find an answer that exactly answers my question. If I missed something, please point me to the correct question.
Here is my scenario --
I originally had a case where my vertical window size was set, so it was easy for me to restrict scrolling completely simply by doing this --
CGRect scrollContainer = CGRectMake(0, 0, 320, 440);
self.baseView = [[UIScrollView alloc] initWithFrame:scrollContainer];
self.baseView.contentSize = CGSizeMake(6400, 440);
self.baseView.bounces = NO;
self.baseView.pagingEnabled = NO;
self.baseView.scrollEnabled = NO;
Based on the movement of the phone, I was then able to scroll programmatically --
CGPoint scrollOffset = CGPointMake(x*20, 0);
[self.baseView setContentOffset:scrollOffset];
In my new requirement, the vertical content increases drastically so I would have the contentSize like so
self.baseView.contentSize = CGSizeMake(6400, 4400);
I still want to scroll horizontally programmatically, but give the user the ability to scroll ONLY vertically by touch. So, I'm assuming I would have to say scrollEnabled = YES. But this lets the user scroll in both directions.
Do I have to capture touch events and override them ? How would I recognize if it was a horizontal scroll or a vertical scroll? What happens if its a diagonal scroll action?
I'm a bit clueless at this point, so any input would be greatly appreciated.
You would probably be able to do this by nesting two UIScrollViews.
The inner scroll view would have contentSize of the actual content and its actual dimensions would be screen width and the same height as contentSize
with scrolling disabled.
The outer scroll view would have it's contentSize be the same as the outer scroll view's dimensions and it's actual dimensions would be the screen dimensions with scrolling enabled.
This way the user can scroll up and down on the outer view while you control the horizontal offset programmatically with the inner view.
It's a little complex but at least you don't have to subclass. :D
(Also will only work with iOS 3.0+) but that doesn't seem to be a problem)
[See "Scroll View Programming Guide for iOS" section on "Nesting Scroll Views"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With