我有3个滚动视图,我在IB中添加然后在我的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];
}
答案 0 :(得分:1)
在配置缩放之前,尝试将图像视图添加为子视图。比如说,在委托作业之前。
viewForZoomingInScrollView:
被其中一个滚动视图的setter调用;从那里看堆栈跟踪应该告诉你哪一个。