简体   繁体   中英

UIScrollView and UIPinchGestureRecognizer

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.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM