Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two scroll view work Simultaneous with one touch

Tags:

xcode

ios

iphone

I am working on application in it I have to work with two scroll view Simultaneous in one touch. It means if I scroll one scroll view at same time another scroll view must scroll with it.

If this is possible then how can it be done?

like image 818
sinh99 Avatar asked Nov 29 '11 06:11

sinh99


2 Answers

Implement the UIScrollViewDelegate protocol in the view controller containing both scroll views. In the:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

delegate method, get the content offset:

CGPoint offset = [scrollViewA contentOffset]; // or scrollViewB

Then set the other control with:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

You can determine which one to change by comparing in the delegate method above:

if( scrollView == scrollViewA ) // change offset of B
else // change offset of A
like image 152
TigerCoding Avatar answered Sep 30 '22 21:09

TigerCoding


Don't have to read

Generally (at least from what I know) it's bad "style" to have 2 UIScrollView/UITableVIew's in each other because it causes the UI to be hard to interact with. But I think if you have a valid enough use/reason for doing it then I'll show you a way to get it done.

CODE!

If it was me then I'd just override the UIScrollView's touchesMoved method and scroll the other UIScrollView that way.

Within scrollView_1

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {

    UITouch *touch = [ touches anyObject];
    CGPoint newLocation = [touch locationInView: [touch view]];
    CGPoint oldLocation = [touch previousLocationInView:touch.view];
    CGPoint translation = CGPointMake(newLocation.x - oldLocation.x, newLocation.y - oldLocation.y);
    scrollView_2.contentOffset = CGPointMake(scrollView_2.contentOffset.x + translation.x, scrollView_2.contentOffset.y + translation.y)

}

Hope this helps

like image 20
Jab Avatar answered Sep 30 '22 20:09

Jab