-[UIScrollView zoomToRect:animated:] contentSize<bounds.size [英] -[UIScrollView zoomToRect:animated:] weird behavior when contentSize < bounds.size

查看:47
本文介绍了-[UIScrollView zoomToRect:animated:] contentSize<bounds.size的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能准确描述-[UIScrollView zoomToRect:animated:] 的行为?这种方法似乎确实可以完成一些复杂的工作,但是Apple的文档非常稀疏.

Can anyone precisely describe the behavior of -[UIScrollView zoomToRect:animated:] ? This method really seems to do some complicated stuff, but Apple's documentation of it is very sparse.

当内容的大小在宽度和/或高度上小于滚动视图的大小时,此方法将出现无法预测的行为.在某些情况下,此方法会导致滚动视图在应为0时具有负的内容偏移.通过略微不同的rect,它将内容偏移保持为0,就像我期望的那样.

I am getting unpredictable behavior of this method when the content size is smaller than the size of the scroll view in width and/or height. In some cases, this method causes the scroll view to have a negative content offset when it should be 0. Passing slightly different rects, it leaves the content offset at 0 like I would expect.

为了展示这种奇怪的行为,我设置了一个示例项目,该项目的滚动视图大小为(200,200),其中包含内容视图大小为(100,100).我希望缩放到内容视图的((0,0),(200,200))应该将内容留在左上方(即什么都不会发生>).但是,它实际上使内容滚动到滚动视图的边界的右下角(内容偏移量(-100,-100)).为什么会这样?

To demonstrate this weird behavior, I set up an example project with a scroll view of size (200, 200) containing a content view of size (100, 100). I would expect that zooming to rect ((0, 0), (200, 200)) of the content view should leave the content in the top left (i.e. nothing should happen). However, it actually causes the content to scroll to the bottom right of the scroll view's bounds (content offset (-100, -100)). Why does this happen?

这是我的示例项目中的代码:

Here is the code in my example project:

@implementation RootViewController

- (void)loadView {
    self.view = [[UIView alloc] init];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.minimumZoomScale = .5;
    self.scrollView.maximumZoomScale = 4;

    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    self.contentView.backgroundColor = [UIColor redColor];
    [self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]];

    [self.scrollView addSubview:self.contentView];
    self.scrollView.contentSize = self.contentView.frame.size;
    [self.view addSubview:self.scrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.contentView;
}

- (void)handleTap {
    [self.scrollView zoomToRect:CGRectMake(0, 0, 200, 200) animated:YES]; // Try changing to 199, 199 to get completely different behavior
}

@end

感谢您的任何见解!现在,我猜测 UIScrollView 并非旨在显示小于其自身大小的内容.我的用例是,我的内容视图可能比滚动视图宽,但高度较短,并且需要能够以编程方式滚动到该内容视图的右端或左端.

Thanks for any insights! Right now I am guessing UIScrollView just isn't designed to display content smaller than its own size. My use case is that I may have a content view that is wider than the scroll view but shorter in height, and I need to be able to scroll to either the right or left end of this content view programmatically.

推荐答案

除非有人有更好的答案,否则我将得出结论,调用 -zoomToRect:animated:会产生不确定的结果在任何一个维度中, contentSize 均小于 bounds 的大小.换句话说,为了安全起见,内容应大于滚动视图.

Unless someone has a better answer, I'm going to conclude that calling -zoomToRect:animated: has undefined results when the resulting contentSize is smaller than the bounds size in either dimension. In other words, the content should be larger than the scroll view if you want to be safe.

这篇关于-[UIScrollView zoomToRect:animated:] contentSize<bounds.size的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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