2011-01-13 97 views
0

我試圖設置一個非常類似於iPhone上的照片應用程序的應用程序。我遇到的問題是我無法弄清楚設置最小縮放比例的一種好方法,並且如果當前縮放比例小於最小值,則迫使當前縮放比例回到最小值。如何更改方向更改的最小縮放比例?

這裏是我當前如何建立我的滾動視圖...

- (void)viewDidLoad{ 

NSData * imageData = [[[NSData alloc] autorelease] initWithContentsOfURL: [NSURL URLWithString: imageURL]]; 
UIImage *babe = [UIImage imageWithData:imageData]; 
babeView = [[UIImageView alloc] 
      initWithImage:babe]; 
[self.view addSubview:babeView]; 
UIBabeScrollView* myScrollview = (UIBabeScrollView*)self.view; 
myScrollview.frame = [UIScreen mainScreen].applicationFrame; 
[myScrollview setContentSize:[babe size]]; 
[myScrollview setMaximumZoomScale:2.0]; 
// Work out a nice minimum zoom for the image - if it's smaller than the ScrollView then 1.0x zoom otherwise a scaled down zoom so it fits in the ScrollView entirely when zoomed out 
CGSize imageSize = babeView.image.size; 
CGSize scrollSize = myScrollview.frame.size; 
CGFloat widthRatio = scrollSize.width/imageSize.width; 
CGFloat heightRatio = scrollSize.height/imageSize.height; 
CGFloat minimumZoom = MIN(1.0, (widthRatio > heightRatio) ? heightRatio : widthRatio); 

[myScrollview setMinimumZoomScale:minimumZoom]; 
[myScrollview setZoomScale:minimumZoom]; 

我UIBabeScrollView的子類重載它的layoutSubviews是這樣的...

- (void)layoutSubviews { 
[super layoutSubviews]; 

// center the image as it becomes smaller than the size of the screen 
CGSize boundsSize = self.bounds.size; 
CGRect frameToCenter = ((UIImageView*)[self.subviews objectAtIndex:0]).frame; 

// center horizontally 
if (frameToCenter.size.width < boundsSize.width) 
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2; 
else 
    frameToCenter.origin.x = 0; 

// center vertically 
if (frameToCenter.size.height < boundsSize.height) 
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2; 
else 
    frameToCenter.origin.y = 0; 

((UIImageView*)[self.subviews objectAtIndex:0]).frame = frameToCenter; 

}

的影響我要去的是圖像始終居中,不能縮小超過圖像的寬度或高度。

現在,這可以在縱向模式下正常工作,但切換到橫向時,縮放比例不正確。

任何幫助將不勝感激,因爲我還是一個剛起步的iPhone應用程序開發人員。

回答

1

在您的視圖控制器,實現-willRotateToInterfaceOrientation:duration:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    if (toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || 
     toInterfaceOrientation == UIDeviceOrientationLandscapeRight) {  
    [myScrollview setMinimumZoomScale:yourLandscapeMinimumZoomValue]; 
    [myScrollview setZoomScale:yourLandscapeMinimumZoomValue]; 
    } else { 
    [myScrollview setMinimumZoomScale:yourPortraitMinimumZoomValue]; 
    [myScrollview setZoomScale:yourPortraitMinimumZoomValue]; 
    } 
} 
+0

這樣做的訣竅,是我真的應該已經意識到自己>。<謝謝! –

+0

很高興提供幫助。 :) – Altealice

0

而且還有一點,只是爲了信息。我正在嘗試[scrollview setZoomScale :: animated]方法。在方向改變時,它不會縮放到所需的值。

它導致未完成的動畫。

相關問題