如何保持两个 UIScrollView 实例缩放到同一级别?

发布于 2024-10-30 14:05:23 字数 1293 浏览 1 评论 0原文

我有两个 UIScrollView 实例,我希望它们同时缩放。

有人有这样做的经验吗?

我使用 NSNotificationCenter 来告诉我的对象何时缩放。最初我以为我可以以某种方式获取当前可见的矩形,然后只需调用 zoomToRect:,但我没有找到一种方法来做到这一点。我现在所要做的是设置 zoomScalecontentOffset 属性。它看起来像这样:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-11-06 14:05:23

一种更简单的方法是为您的 UIView 控件实现 UIScrollViewDelegate 协议,

该协议管理 .h 文件中的 2 个 UIScrollView2 只需添加到 @interface 声明中,

@interface yourUIViewControll : UIViewControll <UIScrollViewDelegate> {
    UIScrollView *aUIScrollView;
    UIScrollView *bUIScrollView;
}

这样现在您可以在用户滚动或缩放 2 个 UIScrollView2 之一时使用所需的所有方法UI滚动视图
因此,例如,您想知道一个何时缩放或滚动,并想让另一个也缩放和滚动,您特别需要这两个

在 .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;
    }

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;
    }
So要识趣 2024-11-06 14:05:23

我目睹的不稳定行为是因为我在滚动时没有发送通知,只是缩放。当我添加以下附加委托方法时,一切正常:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     [[NSNotificationCenter defaultCenter] postNotificationName:ZOOM_NOTIFICATION_IDENTIFIER object:scrollView];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文