2010-04-13 62 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时,它完全缩小。然后我可以放大,然后我可以平移图像。