Reputation: 1164
I have 2 UIScrollViews displaying an image each (overview and zoom mode). I can scroll in both views already individually, now I want to keep the centerpoint of both views in sync.
I guess when one of the views is creating events, I need to forward them also to the other view, just can't find out which functions are relevant. Any hints?
edit: If parallel live scrolling is not possible, I want to at least sync after a scroll event.
Upvotes: 8
Views: 7842
Reputation: 20274
This is not specific to the requirement here but helps in syncing scrollViews among different classes.
To sync scrollViews between different classes, use NSNotificationCenter to observe for a particular notification.
When scroll occurs then within the -scrollViewDidScroll: delegate method, use NSNotificationCenter to post the notification (with userInfo contents as scrollView.contentOffset.x & scrollView.contentOffset.y) Example:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc]init];
[userInfo setObject:[NSNumber numberWithFloat:scrollView.contentOffset.x]
forKey:@"offsetX"];
[userInfo setObject:[NSNumber numberWithFloat:scrollView.contentOffset.y]
forKey:@"offsetY"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"scrollSync"
object:nil
userInfo:userInfo];
}
Register for this notification in the same class or anyother class and perform the -setContentOffset: method on the scrollView/s you want synced.
.
Example on Adding Observer (either in didLoad or init or anywhere suitable to you in as many classes as you like):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotification:)
name:@"scrollSync" object:nil];
.
Example of the Selector Method
-(void)handleNotification:(NSNotification *)sender
{
float newX = [[sender.userInfo objectForKey:@"offsetX"] floatValue];
float newY = [[sender.userInfo objectForKey:@"offsetY"] floatValue];
[myOtherScrollView setContentOffset:CGPointMake(newX,newY)];
}
Upvotes: 3
Reputation: 55583
Check out the functions
You can link this together in the delegate method -scrollViewDidScroll:, which is constantly called while the user is scrolling.
Upvotes: 15