2012-09-11 24 views
1

當使用zoomToRect時,我的UIScrollView未設置其contentOffsetUIScrollviews zoomToRect只設置zoomScale,但不設置contentOffset

我有一個UIScrollViewUIImageView裏面。滾動和縮放本身到目前爲止工作。現在我想給應用程序啓動一個縮放的圖像視圖的滾動視圖。爲此,我實施了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

回答

1

問題是您正在縮放的​​視圖(containerView)不像它包含的圖像視圖(以及您實際想要縮放的視圖)那麼大。它的frame被設置爲視圖控制器的frame。您看不到這個,因爲默認情況下,UIView不會剪裁其子視圖。

您應該初始化containerView而不是圖像視圖的邊界。

self.containerView = [[UIView alloc] initWithFrame:_imageView.bounds]; 
相關問題