I try to make an Image Gallery that supports zoom abilities. I choose to place every image into a scrollView of a ViewController that is used as a page of PageController. This is the initial pic of ViewController.
When i zoom in the result is fine too.
Although, if i zoom in and change one page and get back ,controller has a bad behaviour. It seems that something is remaining from the previous action into scrollView and i cannot find what.The image below is the false.
I want to have the initial view again(Pic1)
These methods handle the events.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.imageView = [[UIImageView alloc] initWithImage:image];
CGRect lala=(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.imageView setFrame:lala];
//[self.scrollView setFrame:CGRectMake(0, 65, 320, 520)];
[self.scrollView addSubview:self.imageView];
self.imageView.userInteractionEnabled=YES;
// Tell the scroll view the size of the contents
[self.scrollView setContentSize: image.size];
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
self.scrollView.maximumZoomScale = 1.0f;
self.scrollView.zoomScale = minScale;
//[self.scrollView setFrame:self.imageView.frame];
[self.scrollView setFrame:CGRectMake(0, 65, 320, 500)];
[self centerScrollViewContents];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"Will");
self.imageView = [[UIImageView alloc] initWithImage:image];
//self.scrollView=[[UIScrollView alloc] init];
CGRect lala=(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.imageView setFrame:lala];
[self.scrollView setFrame:CGRectMake(0, 65, 320, 500)];
//[self.scrollView setFrame:self.imageView.frame];
//[self.scrollView addSubview:self.imageView];
self.imageView.userInteractionEnabled=YES;
// Tell the scroll view the size of the contents
[self.scrollView setContentSize: image.size];
}
- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;}
In
viewWillAppear:
you are creating new instance of the image view and inviewDidAppear:
you are adding it to the scroll view. These methods are called again when you come back to this view controller. Hence the new image is added again. Actually you are initialising the image view in both methods.Hope this helps.