Mona Mona - 7 months ago 31
iOS Question

uiscrollview zoom in and zoom out doesn't work properly

I have a uiscrollview and there is a uiimageview inside it. I want the used to be able to zoom in out of the image since it's a big image. the scrollview only needs to scroll vertically not horizontally.

before adding the zooming effect I had the following code and it worked as I wanted,

-(void)viewDidLoad
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1690)];
}


So I did some research and ended up with the following code to enable pinch zoom in and out,

-(void)viewDidLoad
{
scrollView.scrollEnabled = YES;

[scrollView setContentSize:CGSizeMake(320, 1690)];
scrollView.delegate = self;
// scrollView.contentSize = instImage.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / instImage.frame.size.width;
scrollView.maximumZoomScale = 2.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
[[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate distantFuture]];

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

return instImage;
}


- (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {

CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width) {

frameToCenter.origin.x = ((boundsSize.width - frameToCenter.size.width) / 2);

}else {

frameToCenter.origin.x = 0;
}

return frameToCenter;
}


- (void)scrollViewDidZoom:(UIScrollView *)scrollV {

instImage.frame = [self centeredFrameForScrollView:scrollV andUIView:instImage];;
}


Now I have the following problems and I can't figure out what's wrong,


  1. when the view is just loaded before any pinch the scrollview doesn't scroll all the way to the end of the image.

  2. when pinched to zoom in, it scrolls left and right and up and down the image, but still it doesn't scroll down all the way to the end of the image, but still more than what it originally did at the begining.

  3. when I zoom out, the whole thing stops. you can't zoom in or out, it doesn't even scroll anymore. totally locked.



I did some reseach and found the following post.
http://stackoverflow.com/a/6384834/1103257

but I don't know where would I find NSDefaultRunLoopMode or even how to search for it to figure out whether this can solve it or not.

Answer Source

I finally got it. This solution works perfectly!

in your .h file

@interface scrollViewController : UIViewController <UIScrollViewDelegate>{

      float oldScale;

      IBOutlet UIScrollView *scrollView;
      IBOutlet UIImageView  *image;
}

@end

and just connect the image to the uiimageview that has the photo that you want to zoom in and out and is inside your scrollview. then connect the scrollview to your uiscrollview on your interface.

in your .m file

@implementation scrollViewController


-(void)viewDidLoad
{
[super viewDidLoad];

scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale=1.0;
scrollView.maximumZoomScale=4.0;
[scrollView setContentSize:CGSizeMake(320, 1700)];
scrollView.delegate=self;

 }

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
 {
     return image;
 }

 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollV withView:(UIView *)view atScale:(float)scale
 {
     [scrollView setContentSize:CGSizeMake(scale*320, scale*1700)];
 }

  @end

hope this helps.