當使用zoomToRect
時,我的UIScrollView
未設置其contentOffset
。UIScrollviews zoomToRect只設置zoomScale,但不設置contentOffset
我有一個UIScrollView
與UIImageView
裏面。滾動和縮放本身到目前爲止工作。現在我想給應用程序啓動一個縮放的圖像視圖的滾動視圖。爲此,我實施了zoomToRect:
並正確設置了zoomsScale
,但它沒有設置contentOffset
。
預期結果使用zoomToRect
時在於UIScrollView
放大或縮小根據所選擇的矩形,並根據提供給zoomToRect
方法的矩形的原點座標設置其contentOffset
。
實際行爲是它放大到正確的zoomScale
,但我的UIImageView
總是在原點0,0,而不是在zoomToRect
中指定的矩形I的協調x(475)和y(520)的預期原點。
我的圖片尺寸爲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
?