2013-12-17 39 views
2

我嘗試製作一個支持縮放功能的圖片庫。我選擇將每個圖像放置在用作PageController頁面的ViewController的scrollView中。這是ViewController的初始圖片。頁面控制器中的ScrollView

initial

當我在結果放大是沒關係。

zoom in picture

雖然,如果我放大,改變一個頁面,並返回,控制器具有不良行爲。看起來前面的動作還剩下一些東西進入了scrollView,我找不到什麼。下面的圖片是錯誤的。

false image

我想有初步意見再次(PIC1)

這些方法處理的事件。

- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 


self.imageView = [[UIImageView alloc] initWithImage:image]; 

CGRect lala=(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}; 

[self.imageView setFrame:lala]; 


//[self.scrollView setFrame:CGRectMake(0, 65, 320, 520)]; 

[self.scrollView addSubview:self.imageView]; 
self.imageView.userInteractionEnabled=YES; 

// Tell the scroll view the size of the contents 
[self.scrollView setContentSize: image.size]; 

// Set up the minimum & maximum zoom scales 
CGRect scrollViewFrame = self.scrollView.frame; 
CGFloat scaleWidth = scrollViewFrame.size.width/self.scrollView.contentSize.width; 
CGFloat scaleHeight = scrollViewFrame.size.height/self.scrollView.contentSize.height; 
CGFloat minScale = MIN(scaleWidth, scaleHeight); 

self.scrollView.minimumZoomScale = minScale; 
self.scrollView.maximumZoomScale = 1.0f; 
self.scrollView.zoomScale = minScale; 
//[self.scrollView setFrame:self.imageView.frame]; 
[self.scrollView setFrame:CGRectMake(0, 65, 320, 500)]; 

[self centerScrollViewContents]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

NSLog(@"Will"); 

self.imageView = [[UIImageView alloc] initWithImage:image]; 
//self.scrollView=[[UIScrollView alloc] init]; 
CGRect lala=(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}; 

[self.imageView setFrame:lala]; 

[self.scrollView setFrame:CGRectMake(0, 65, 320, 500)]; 
//[self.scrollView setFrame:self.imageView.frame]; 

//[self.scrollView addSubview:self.imageView]; 
self.imageView.userInteractionEnabled=YES; 

// Tell the scroll view the size of the contents 
[self.scrollView setContentSize: image.size]; 

} 

- (void)centerScrollViewContents { 
CGSize boundsSize = self.scrollView.bounds.size; 
CGRect contentsFrame = self.imageView.frame; 

if (contentsFrame.size.width < boundsSize.width) { 
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
} else { 
    contentsFrame.origin.x = 0.0f; 
} 

if (contentsFrame.size.height < boundsSize.height) { 
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
} else { 
    contentsFrame.origin.y = 0.0f; 
} 

self.imageView.frame = contentsFrame;} 

回答

0

viewWillAppear:所創建的圖像視圖的新實例,並在viewDidAppear:將其添加到滾動視圖。當您返回到此視圖控制器時,將再次調用這些方法。因此新的圖像再次被添加。 其實你是在這兩種方法初始化圖像視圖。

希望這會有所幫助。

+0

感謝您的回覆。我已經嘗試了這兩種方法的許多可能的組合,但問題仍然存在。你能爲這兩種方法提出建議嗎?謝謝。 – hoya21

+0

您可以嘗試初始化圖像視圖並將其添加到viewDidLoad中的滾動視圖中:並保持原樣。即使將圖像分配給viewWillAppear:或viewDidAppear:中的圖像視圖。 – Nameet

+0

我現在試過,但沒有奏效。 – hoya21

相關問題