0

我在 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];

}
4

1 回答 1

1

在配置缩放之前尝试将图像视图添加为子视图。说,在委托分配之前。

viewForZoomingInScrollView:被滚动视图的设置器之一调用;从那里查看堆栈跟踪应该会告诉您是哪一个。

于 2012-06-06T19:14:53.613 回答