正确缩放包含许多子视图的 UIScrollView

发布于 2024-08-12 19:10:00 字数 360 浏览 4 评论 0原文

我创建了一个可缩放的 UIScrollView 并向其添加了 100 个子视图(平铺)。视图左右滚动完美。但是,我想允许缩放。

为此,我读到我的委托需要实现:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return ???;
}

我见过只有一个子视图需要缩放的示例,因此它们在该方法中返回该子视图。然而,就我而言,我还有更多。进行缩放的正确方法是什么?

我尝试创建另一个 UIView 并向其中添加 100 个子视图。然后返回上述方法的一个视图,但我不起作用(它会缩放,但一旦停止,它就不再具有交互性)。

I created a zoomable UIScrollView and added 100 subviews to it (tiled). The view scrolls perfectly left and right. However, I'd like to allow zooming.

To do so I read that my delegate needs to implement:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return ???;
}

I have seen examples that have only one subview to zoom, so they return that subview in that method. In my case, however, I have a lot more. What is the proper way to do the zooming?

I tried creating another UIView and adding the 100 subviews to that one. And then return that one view on the method above, but I doesn't work (it zooms but once it stops, it's not interactive any more).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

感情洁癖 2024-08-19 19:10:00

确切地说,

这也是苹果在 中提到的内容滚动视图编程指南

只需创建一个视图,将所有子视图添加到该视图,并将新创建的视图作为单个子视图添加到滚动视图...

然后在 viewForZoomingInScrollView 委托方法中返回索引 0 处的对象:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return [self.scrollView.subviews objectAtIndex:0];
}

Exactly,

This is what Apple also is mentioning in the Scroll View Programming Guide:

Just create a view, add all subviews to this view and add the newly created view as a single subview to the scrollview...

Then in the viewForZoomingInScrollView delegate method return the object at Index 0:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return [self.scrollView.subviews objectAtIndex:0];
}
〃安静 2024-08-19 19:10:00

我创建了一个视图,在其中添加了所有内容:

UIView *zoomableView = [[UIView alloc] init];

而不设置其框架。

当我向其中添加所有子视图后,将其框架设置为足够大以容纳所有平铺子视图时,问题就解决了。

I created the view where I added everything using:

UIView *zoomableView = [[UIView alloc] init];

without setting its frame.

The problem was solved when, after adding all the subviews to it, I set its frame to something large enough to accommodate all the tiled subviews.

仅冇旳回忆 2024-08-19 19:10:00

您必须将要添加的视图作为子视图返回到滚动视图。

例如:如果要将图像视图添加到滚动视图,则编写

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
return imageView object;
}

You have to return what your going to add views to scroll view as a subviews.

Ex: If you are adding image view to scroll view then write

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
return imageView object;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文