2010-04-13 33 views
5

我正在用我的UIScrollview讓它放大底層的UIImageView。在我的視圖控制器我設置UIScrollView setZoomScale不工作?

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
return myImageView; 
} 

在viewDidLoad方法我嘗試將zoomScale設置爲2如下(注意的UIImageView和圖像在界面生成器設置):

- (void)viewDidLoad { 
[super viewDidLoad]; 

myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height); 
myScrollView.contentOffset = CGPointMake(941.0, 990.0); 
myScrollView.minimumZoomScale = 0.1; 
myScrollView.maximumZoomScale = 10.0; 
myScrollView.zoomScale = 0.7; 
myScrollView.clipsToBounds = YES; 
myScrollView.delegate = self; 

NSLog(@"zoomScale: %.1f, minZoolScale: %.3f", myScrollView.zoomScale, myScrollView.minimumZoomScale); 
} 

我嘗試了幾乎沒有什麼變化,但NSLog總是顯示1.0的zoomScale。

任何想法,我擰這個嗎?

回答

19

我終於得到了這個工作。導致問題的原因是代表電話最後。我現在把它搬上去......在這裏,我們走了。

新的代碼看起來是這樣的:

- (void)viewDidLoad { 
[super viewDidLoad]; 

myScrollView.delegate = self; 
myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height); 
myScrollView.contentOffset = CGPointMake(941.0, 990.0); 
myScrollView.minimumZoomScale = 0.1; 
myScrollView.maximumZoomScale = 10.0; 
myScrollView.zoomScale = 0.7; 
myScrollView.clipsToBounds = YES; 
} 
+6

看起來這是由OS調用-viewForZoomingInScrollView引起的。如果您的委託人爲零,則此調用不會進行,因此視圖縮放功能將被禁用。 – 2010-05-25 14:33:33

+0

非常感謝!這是真的。但爲什麼會發生?這可能不是代表的業務。 – Yeung 2013-02-27 10:13:43

1

這是我做的另一個例子。這是使用資源文件夾中包含的圖像。相比於你有這個人將UIImageView作爲子視圖添加到視圖,然後將縮放更改爲整個視圖。

-(void)viewDidLoad{ 

[super viewDidLoad]; 

UIImage *image = [UIImage imageNamed:@"random.jpg"]; 

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

[self.view addSubview:imageView]; 

[(UIScrollView *) self.view setContentSize:[image size]]; 

[(UIScrollView *) self.view setMaximumZoomScale:2.0]; 

[(UIScrollView *) self.view setMinimumZoomScale:0.5]; 

} 
+0

不知道爲什麼這可能會導致任何麻煩,但在我的絕望嘗試它。結果不變 – iFloh 2010-04-16 04:20:10

1

我知道這是相當晚的答案去,但問題是,你的代碼調用zoomScale它設置委託之前。你是對的,其中的其他東西不需要委託,但zoomScale會這樣做,因爲它必須能夠在縮放完成時回調。至少這就是我認爲它的工作原理。

+0

歡迎來到SO @turboc!這個評論無疑是有用的,但爲了將來的參考,在相應的帖子下面的「評論」部分(在這種情況下,已經被接受爲答案的帖子)中添加諸如此類的評論。 – ewitkows 2014-06-09 18:01:44

0

我的代碼必須完全瘋狂,因爲我使用的規模與教程和其他人正在做的完全相反。對於我來說,minScale = 1表示圖像完全縮小並適合包含它的UIImageView

這裏是我的代碼:

[self.imageView setImage:image]; 
    // Makes the content size the same size as the imageView size. 
    // Since the image size and the scroll view size should be the same, the scroll view shouldn't scroll, only bounce. 
    self.scrollView.contentSize = self.imageView.frame.size; 

    // despite what tutorials say, the scale actually goes from one (image sized to fit screen) to max (image at actual resolution) 
    CGRect scrollViewFrame = self.scrollView.frame; 
    CGFloat minScale = 1; 
    // max is calculated by finding the max ratio factor of the image size to the scroll view size (which will change based on the device) 
    CGFloat scaleWidth = image.size.width/scrollViewFrame.size.width; 
    CGFloat scaleHeight = image.size.height/scrollViewFrame.size.height; 
    self.scrollView.maximumZoomScale = MAX(scaleWidth, scaleHeight); 
    self.scrollView.minimumZoomScale = minScale; 
    // ensure we are zoomed out fully 
    self.scrollView.zoomScale = minScale; 

這個工程,我期望的那樣。當我將圖像加載到UIImageView時,它完全縮小。然後我可以放大,然後我可以平移圖像。

相關問題