如何保持两个 UIScrollView 实例缩放到同一级别?
我有两个 UIScrollView 实例,我希望它们同时缩放。
有人有这样做的经验吗?
我使用 NSNotificationCenter 来告诉我的对象何时缩放。最初我以为我可以以某种方式获取当前可见的矩形,然后只需调用 zoomToRect:
,但我没有找到一种方法来做到这一点。我现在所要做的是设置 zoomScale
和 contentOffset
属性。它看起来像这样:
- (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_IDENTIFIER object:pageScrollView];
}
但它不起作用,而且看起来非常不稳定。有人有想法吗?我应该采取不同的方法吗?
编辑:我应该澄清,两个滚动视图不同时可见。它们在同一时间滚动并不重要,只是滚动完成后,一个滚动视图与另一个滚动视图处于相同的缩放级别(和可见矩形)。
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_IDENTIFIER 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种更简单的方法是为您的 UIView 控件实现 UIScrollViewDelegate 协议,
该协议管理 .h 文件中的 2 个 UIScrollView2 只需添加到 @interface 声明中,
这样现在您可以在用户滚动或缩放 2 个 UIScrollView2 之一时使用所需的所有方法UI滚动视图
因此,例如,您想知道一个何时缩放或滚动,并想让另一个也缩放和滚动,您特别需要这两个
在 .m 中:
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
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:
我目睹的不稳定行为是因为我在滚动时没有发送通知,只是缩放。当我添加以下附加委托方法时,一切正常:
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: