【发布时间】:2012-09-11 21:04:56
【问题描述】:
我的UIScrollView 在使用zoomToRect 时没有设置它的contentOffset。
我有一个UIScrollView,里面有一个UIImageView。滚动和缩放本身到目前为止工作。现在我想在应用程序开始时为滚动视图提供图像视图的某个缩放矩形。为此,我实现了zoomToRect:,它正确设置了zoomsScale,但没有设置contentOffset。
使用zoomToRect时的预期结果是UIScrollView根据所选矩形放大或缩小,并根据zoomToRect方法给出的矩形原点坐标设置其contentOffset。
实际行为是它缩放到正确的zoomScale,但我的UIImageView 始终位于原点 0,0,而不是我在 @ 中指定的矩形坐标的 x (475) 和 y (520) 的预期原点987654335@.
我的图片尺寸是 1473x1473。
这是一些代码
- (void)viewDidLoad {
CGRect bounds = self.view.frame;
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImage.png"]];
self.containerView = [[UIView alloc] initWithFrame:bounds];
_containerView.contentMode = UIViewContentModeCenter;
_scrollView = [[UIScrollView alloc] initWithFrame:bounds];
_scrollView.delegate = self;
_scrollView.contentSize = _imageView.bounds.size;
_scrollView.minimumZoomScale = 0.2;
[_scrollView addSubview:_containerView];
[_containerView addSubview:_imageView];
[self.view addSubview:_scrollView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_scrollView zoomToRect:CGRectMake(475.0, 150.0, 520.0, 747.0) animated:NO];
}
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _containerView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
[self printVisibleRectToConsole:scrollView];
CGSize newImageViewSizeWithScale = CGSizeMake(_imageView.bounds.size.width * _scrollView.zoomScale,
_imageView.bounds.size.height * _scrollView.zoomScale);
_scrollView.contentSize = newImageViewSizeWithScale;
}
我的问题:
- 为什么
zoomToRect没有设置contentOffset? - 如何让
zoomToRect按预期更改我的contentOffset?
【问题讨论】:
标签: ios uiscrollview