我正在使用我的UIScrollview让它放大底层的UIImageView。在我的视图控制器中,我设置了
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return myImageView;
}
在viewDidLoad方法中,我尝试将zoomScale设置为2,如下所示(注意在Interface Builder中设置了UIImageView和Image):
- (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。
我把这个搞砸了吗?
答案 0 :(得分:20)
我终于开始工作了。导致问题的原因是委托电话在最后。我现在把它搬了起来......我们走了。
新代码如下所示:
- (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;
}
答案 1 :(得分: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];
}
答案 2 :(得分:1)
我知道答案已经很晚了,但问题是你的代码在设置委托之前调用了zoomScale。你是对的,那里的其他东西不需要委托,但zoomScale会这样做,因为它必须能够在缩放完成时回调。至少我认为它是如何工作的。
答案 3 :(得分: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
时,它会完全缩小。我可以放大然后我可以平移图像。