How do I keep two UIScrollView instances zoomed to the same level?
I have two instances of UIScrollView, and I want them to zoom at the same time.
Anyone have any experience doing that?
I'm using the NSNotificationCenter
to tell my object when to zoom. Initially I thought I could somehow get at the currently visible rect, and just call zoomToRect:
, but I don't see a way to do that. What I have now is setting the zoomScale
and contentOffset
properties. It looks like this:
- (void)registerForZoomNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveZoomNotification:)
name:ZOOM_NOTIFICATION_IDENTIFIER
object:nil];
}
- (void)receiveZoomNotification:(NSNotification*)notification {
UIScrollView *currentScrollView = (UIScrollView*)[notification object];
// zoomLevel
[(UIScrollView*)self.view setZoomScale:currentScrollView.zoomScale animated:NO];
// contentOffset
[(UIScrollView*)self.view setContentOffset:currentScrollView.contentOffset animated:NO];
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidZoom:(UIScrollView *)pageScrollView {
[[NSNotificationCenter defaultCenter] postNotificationName:ZOOM_NOTIFICATION_IDENTIFI开发者_如何学运维ER object:pageScrollView];
}
It's not working though, and seems terribly erratic. Ideas anyone? Should I be taking a different approach?
EDIT: I should clarify that both scroll views are not visible at the same time. It's not important that they scroll at the EXACT same time, only that ones scroll view is at the same zoom level (and visible rect) as the other after scrolling completes.
An easier way is to implement the UIScrollViewDelegate Protocol for your UIView control which manage the 2 UIScrollView2
in your .h file just add in the @interface declaration
@interface yourUIViewControll : UIViewControll <UIScrollViewDelegate> {
UIScrollView *aUIScrollView;
UIScrollView *bUIScrollView;
}
this way now you can use all the methods you need when user scroll or zoom one of the 2 UIScrollView so, for example, you wanna know when one is zooming or scrolling and wanna let the other too zoom and scroll you need these 2 in particular
in .m:
// called when a UIScrollView is zooming:
- (void)scrollViewDidZoom:(UIScrollView *)zoomViewInUse{
// just to test in log window:
// NSLog(@"changing zoom... scrollViewInUse.zoomScale: %.5f", zoomViewInUse.zoomScale);
//force both UIScrollViews to zoom at the new value
aUIScrollView.zoomScale = zoomViewInUse.zoomScale;
bUIScrollView.zoomScale = zoomViewInUse.zoomScale;
}
// called when a UIScrollView is scrolling:
- (void)scrollViewDidScroll:(UIScrollView *)scrollViewInUse{
// just to test in log window:
// NSLog(@"scrollViewInUse..contentOffset.x:%.1f", scrollViewInUse.contentOffset.y);
//force both UIScrollViews to scroll at the new value
aUIScrollView.contentOffset = scrollViewInUse.contentOffset;
bUIScrollView.contentOffset = scrollViewInUse.contentOffset;
}
The erratic behavior I witnessed was because I wasn't sending a notification when I scrolled, only zoomed. When I added the following additional delegate method, everything worked correctly:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[[NSNotificationCenter defaultCenter] postNotificationName:ZOOM_NOTIFICATION_IDENTIFIER object:scrollView];
}
精彩评论