我在 IB 中添加了 3 个滚动视图,然后在我的 viewDidLoad 中调用了一个将 UIImageView 添加到每个子视图的方法。我想放大每个子视图,所以我使用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
但是在将图像视图添加到子视图之前会调用 viewForZoomingInScrollView。如何解决这个问题?
这是 loadImages 方法:
-(void) loadImages{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData1 = [defaults dataForKey:@"image1"];
NSData *imageData2 = [defaults dataForKey:@"image2"];
NSData *imageData3 = [defaults dataForKey:@"image3"];
UIImage *img1 = [UIImage imageWithData:imageData1];
UIImage *img2 = [UIImage imageWithData:imageData2];
UIImage *img3 = [UIImage imageWithData:imageData3];
[self loadImage:img1 toScrollView:scrollViewTop];
[self loadImage:img2 toScrollView:scrollViewLeft];
[self loadImage:img3 toScrollView:scrollViewRight];
}
-(void) loadImage:(UIImage *)image toScrollView:(UIScrollView *)scrollView
{
//scale and rotate image to default
UIImage *scaledImage = scaleAndRotateImage(image);
//create imageview and add image to the view
UIImageView *imageView = [[UIImageView alloc]initWithImage:scaledImage];
CGFloat imageWidth = scaledImage.size.width;
CGFloat imageHeight = scaledImage.size.height;
int scrollWidth = scrollView.frame.size.width;
int scrollHeight = scrollView.frame.size.height;
//Limit by width or height, depending on which is smaller in relation to
//the scrollview dimension.
float scaleX = scrollWidth / imageWidth;
float scaleY = scrollHeight / imageHeight;
float scaleScroll = (scaleX < scaleY ? scaleY : scaleX);
// scrollView.bounds = CGRectMake(0, 0,imageWidth , imageHeight );
//scrollView.frame = CGRectMake(0, 0, scrollWidth, scrollHeight);
scrollView.delegate = self;
scrollView.contentSize = imageView.frame.size;
scrollView.pagingEnabled = NO;
scrollView.maximumZoomScale = scaleScroll*3;
scrollView.minimumZoomScale = scaleScroll;
scrollView.zoomScale = scaleScroll;
[scrollView addSubview:imageView];
}