2014-01-20 58 views
2

所以我想顯示用戶可以縮放,平移和旋轉的圖像。如何獲得縮放,平移和旋轉以在滾動視圖中爲圖像工作

我希望圖像適合屏幕(即縱橫比保持不變,整個圖像在屏幕上可見,圖像的兩個相對邊接觸屏幕邊緣)。

當屏幕旋轉時,我想保持行爲。

我玩過不少設置,但無法獲得完美的行爲。

我目前有:

一個UIScrollViewUIImageView作爲其子。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.imageView.image = [UIImage imageNamed:@"test2.jpg"]; 
    self.imageView.contentMode = UIViewContentModeScaleAspectFit; 
    self.imageView.clipsToBounds = YES; 
} 

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    CGFloat width = self.scrollView.frame.size.width; 
    CGFloat height = self.scrollView.frame.size.height; 

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if (UIInterfaceOrientationIsLandscape(orientation)) 
    { 
     NSLog(@"Screen rotated to landscape orientation."); 
     self.scrollView.frame = CGRectMake(0, 0, height, width); 

     [self.scrollView setZoomScale:1 animated:YES]; 
    } 
    else 
    { 
     NSLog(@"Screen rotated to portrait orientation."); 
     self.scrollView.frame = CGRectMake(0, 0, width, height); 

     [self.scrollView setZoomScale:1 animated:YES]; 
    } 
} 

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

當前代碼在縱向模式下對我來說工作得很好。圖像適合屏幕並居中。景觀的行爲不符合我希望的方式。

我真的不明白爲什麼[self.scrollView setZoomScale:1 animated:YES];可以工作。它似乎適用於任何尺寸的圖像。也許我不太明白zoomScale的功能是什麼?

回答