iOS:UIScrollView 以编程方式缩放不起作用 [英] iOS: UIScrollView zooming programmatically not working

查看:89
本文介绍了iOS:UIScrollView 以编程方式缩放不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可分页的 UIScrollView,它包含不同类型的信息,如 UITables 以及可缩放的图像.因此,我设置了一个可分页的 main-ScrollView,作为子视图,我添加了可缩放的 image-ScrollViews,并将图像作为内容.一切正常,只是我无法设置 imageScrollViews 的较小当前缩放比例.

I have a pageable UIScrollView which contains different kind of informations like UITables but also zoomable images. Therefore I set up a pageable main-ScrollView and as subviews I added zoomable image-ScrollViews with the images as content. Works everything fine, just I fail to set the smaller current zoom scale of the imageScrollViews.

UIImageView *imageView = [[UIImageView alloc] initWithImage:Image];

//storing a link to the imageView
[imagelinkArray addObject:imageView];

CGRect ScrollViewImageRect;
ScrollViewImageRect = CGRectMake((self.scrollView.frame.size.width) * i, 0, 320, self.scrollView.frame.size.height);

float widthfactor = ScrollViewImageRect.size.width / imageView.frame.size.width;
float heightfactor = ScrollViewImageRect.size.height / imageView.frame.size.height;
float zoomscale = MIN(widthfactor, heightfactor);

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:ScrollViewImageRect];
imageScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);

imageScrollView.delegate = self;

[imageScrollView setMinimumZoomScale:zoomscale];
[imageScrollView setMaximumZoomScale:1.5];

[imageScrollView addSubview:imageView];

//doesn't work:
[imageScrollView setZoomScale:0.5 animated:YES];

[self.scrollView addSubview:imageScrollView];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [imagelinkArray objectAtIndex:page];
}

main-ScrollView 和 image-ScrollView 绘制的很完美,可以正常缩放和分页.最小缩放系数的计算也是正确的.当我缩小时,我可以放大直到达到图像限制.但是,当我第一次翻到 image-ScrollView 时,它的当前缩放比例始终为 1.0,而它应该是最小比例.

The main-ScrollView and the image-ScrollViews are drawn perfectly and it's possible to zoom and page properly. The minimum zoom factor is calculated also correct. When I zoom out I can zoom until the image limits are reached. However the first time I page to the image-ScrollView it's current zoom scale is always 1.0 while it should be the minimum scale.

无论我在上面的代码中在哪里设置缩放比例都不起作用:

Wherever I set the zoom scale in the code above it doesn't work:

[changeScrollView setZoomScale:changeScrollView.minimumZoomScale animated:YES];

如果我记录当前的缩放比例,我总是得到 1.0;

If I log the current zoom scale i always get 1.0;

唯一有效的是在 - (void)scrollViewDidScroll: 方法中更改缩放比例,这当然没有多大帮助,因为缩放也会调用它立即重置缩放.但至少我可以弄清楚,代码以某种方式起作用.我感觉 UIScrollView 现在在屏幕上不可见时不会缩放.我该如何解决这个问题?

The only thing which works is changing the zoom scale in the - (void)scrollViewDidScroll: method, which of course doesn't help a lot since zooming also calls it which resets the zoom immediately. But at least I could figure out, that the code somehow works. I have the feeling a UIScrollView doesn't zoom when it's not visible on the screen right now. How can I fix this?

更新:好的.与此同时,我发现问题很可能来自我的子查看"ScrollViews 到另一个 ScrollView 的基本布局.当我缩放其中一张图像并记录 ScrollView 的当前缩放系数时,它们都是相同的(主 ScrollView 以及所有子 ScrollView).可能是什么原因或者我如何用不同的布局解决它?

Update: Okay. In the meantime I figured out that the problem most likely comes from my base layout of "sub-viewing" ScrollViews into another ScrollView. When I zoom one of the images and log the current zoom factor of the ScrollViews they are all the same (main ScrollView as well as ALL sub-ScrollViews). What could be the reason for it or how could I solve it with a different layout?

推荐答案

我认为 viewForZoomingInScrollView: 可能不会被滚动视图调用.尝试添加断点并检查您在那里返回的内容.

I think viewForZoomingInScrollView: may not be getting called by the scrollview. Try to add a breakpoint and check what you're returning there.

这篇关于iOS:UIScrollView 以编程方式缩放不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆