views:

306

answers:

4

I would like to have a pair of scrollviews - call them scrollA and scrollB - on the screen that work in parallel. When the user scroll/zooms scrollA scrollB mimics that behavior, zooming and panning identically. And vice versa.

Is this possible? Thanks in advance.

Cheers, Doug

A: 

Key value coding, if you don't know that then pick up Cocoa programming for Mac OS X, and learning objective-C by stephen kochan (spelling & punctuation?)

Check the docs for a way to get the position of the scroll view (like the index of a tableview) then set up KVC. Then you'll understand how the iPod keeps your scroll position after quits.

JoePasq
Yes, I am aware of and use KVO/KVC. My question is what to observe during pan/zoom and what state information to pass to the twin scrollViews. Thanks.
dugla
+2  A: 

I think the best way would be to set the delegate of each scrollview to your controller then implement the '- (void)scrollViewDidScroll:(UIScrollView *)scrollView' in your controller, inside the method you'll want to call 'setContentOffset:animated:' on the scrollview that didn't scroll, to get the correct contentOffset you can use the UIScrollView.contentOffset property of the UIScrollView that was scrolled which will be passed to your implementation of - (void)scrollViewDidScroll:(UIScrollView *)scrollView in your controller

Hi JonC,Thanks for this. That handles panning. I'm a bit puzzle regarding zoom though? Or does UIScrollView internally compute zoom from content offset?Cheers,Doug
dugla
+3  A: 

As per JoePasq's answer, I would use KVO for this and register observers for whichever key/value pairs you'd like to mimic in each UIScrollView. It would look something like this (untested):

// Do this during initialisation of scrollView2
[scrollView1 addObserver:self
              forKeyPath:@"contentOffset"
                 options:NSKeyValueObservingOptionNew
                 context:NULL];

[scrollView1 addObserver:self
              forKeyPath:@"zoomScale"
                 options:NSKeyValueObservingOptionNew
                 context:NULL];

// Implement this method on scrollView2
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

  [self setValue:[change valueForKey:NSKeyValueChangeNewKey] forKey:keyPath];
}
Nathan de Vries
Thanks Nathan. I'll give this a try. Cheers.
dugla
Very cool. How did I ever write code without KVO/KVC? Thanks again.
dugla
A: 

Does this work for the flick gesture as well?

From my experiences, the contentOffset is updated after the flick gesture deceleration is done.. so even with KVO it will still be observing old contentOffsets

am i right?

Michael Xu