0

I have a UIView with where I load images into subviews with scrolling and paging enabled, it's working ok. Now I'm adding a pinch gesture to zoom on the images and it's working but with a few problems:

  1. I seem to be zooming the whole scrollview, not only the image
  2. I want to limit the zoom out to the orignal size of the image, now I can zoom out way to much and it makes it really small and then it's hard to zoom back.
  3. When I zoom in I can't scroll.

Here's the code:

 NSDictionary *dictionary = [tmpAppDelegate.data objectForKey:selectedTitle];
NSArray *MenuImageArr = [[NSArray alloc] init];
MenuImageArr = [dictionary objectForKey:@"MenuArr"];

NSMutableArray *menuImages = [[NSMutableArray alloc] init];   
scrollView.delegate = self;
// loop through all names from the plist and add image to the array
for (NSInteger i = 0; i < [MenuImageArr count]; i++) {
    NSString *name = [MenuImageArr objectAtIndex:i];
    [menuImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%@", selectedTitle, name]]];
}
for (int i = 0; i < menuImages.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = [menuImages objectAtIndex:i];
    [self.scrollView addSubview:subview];
    }
self.pageControl.numberOfPages = menuImages.count;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * menuImages.count, self.scrollView.frame.size.height);
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}

I'm not sure how to proceed with these problems.

Kiran
  • 20,167
  • 11
  • 67
  • 99
KeLLoGsX
  • 1
  • 1
  • 1
  • Why don't you use a UIWebView (instead of a scrollview) and put the image in there (as html) and then you get all the pinch and zoom and scroll functionality built in. :) – Thomas Clayson Mar 14 '12 at 15:37
  • Well I used the scrollview because I wanted paging enabled and thought that was the way to go. But perhaps I should look into using the webview instead. Now I load X amount of images from a plist into subviews for paging. But I could be loading them on top of eachother in a webview and scroll down instead of paging. I'll see how it goes, thanx :-) – KeLLoGsX Mar 16 '12 at 07:06

2 Answers2

0

I do something similiar. In my case I disable zoomin in the scrollview by setting min and maxzoomscale to 1.

My pinch handler will then just work out the zoom factor from the gestures parameters and tell the view contained in the scrollview to repaint. This will then handle the zooming itself. The pinch handle should constrain the zoom factor. Remember the scale factor passed in from the gesture recognizer is not an absolute scale but a delta from the last event.

You need to do something like this:

...

float lastScale = 1;

...

- (void) pinchGestureRecognizer:(UIPinchGestureRecognizer*) recognizer
{
   float newScale = self.myScale + ( recognizer.scale - lastScale )
   self.myDrawing.scale = newScale;

   if ( recognizer.state === UIGestureRecognizerStateEnded )
   {
       lastScale = 1;
       return;
   }

   lastScale = recognizer.scale;

   [self.myDrawing setNeedsDisplay];
}

Thats off the top of my head so hopefully will help

My exact situation is slightly different as I'm manually drawing some SVG vectors but the principles should be pretty similiar

0

Solved! Using a UIWebview loading a pdf. Had some trouble getting the correct path in main bundle in pathForResource.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Millennium_meny" ofType:@"pdf"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request]; 
[webView setScalesPageToFit:YES];

Thanx!

KeLLoGsX
  • 1
  • 1
  • 1