Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. https://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.

like image 402
Mona Avatar asked Feb 21 '12 00:02

Mona


3 Answers

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.

like image 98
Mona Avatar answered Sep 28 '22 06:09

Mona


Here is how I implement a scroll view to scroll around a big image in viewDidLoad

UIImage *img = [UIImage imageNamed:@"image_name.png"];
largeImageView = [[UIImageView alloc] initWithImage:img];

[self.view insertSubview: largeImageView atIndex:0];
[(UIScrollView *)self.view setContentSize:[img size]];
[(UIScrollView *)self.view setMaximumZoomScale:2.2];
[(UIScrollView *)self.view setMinimumZoomScale:0.3];
[(UIScrollView *)self.view setCanCancelContentTouches:YES];
[(UIScrollView *)self.view setBounces:YES];
[mapImageView setUserInteractionEnabled:YES];

Then I also have the viewForZoomingInScrollView method:

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

You scrollViewDidZoom function is unnecessary at this point. I implement something similar to keep the image centered when the entire image is visible, but right now I wouldn't worry about that if I were you.

like image 38
atreat Avatar answered Sep 28 '22 07:09

atreat


Same issue but got a simple workaround, just set the image view to user interaction enabled:

[self.imageView setUserInteractionEnabled:YES];
like image 38
Kjuly Avatar answered Sep 28 '22 06:09

Kjuly