谁能准确描述-[UIScrollView zoomToRect:animated:]
?这个方法看起来确实做了一些复杂的事情,但苹果的文档非常稀疏。
当内容大小小于滚动视图的宽度和/或高度大小时,我会遇到此方法的不可预测的行为。在某些情况下,此方法会导致滚动视图在本应为 0 的情况下出现负内容偏移。传递略有不同的矩形,它会将内容偏移保留为 0,就像我所期望的那样。
为了演示这种奇怪的行为,我设置了一个示例项目大小为 (200, 200) 的滚动视图含有一个内容视图大小 (100, 100)。我希望缩放至矩形 ((0, 0), (200, 200))内容视图的内容应将内容保留在左上角(即什么都不应该发生)。然而,它实际上导致内容滚动到滚动视图边界的右下角(内容偏移量(-100,-100))。为什么会出现这种情况?
这是我的示例项目中的代码:
@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
只是不是为了显示小于其自身尺寸的内容而设计的。我的用例是,我可能有一个比滚动视图更宽但高度更短的内容视图,并且我需要能够以编程方式滚动到该内容视图的右端或左端。