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:
- I seem to be zooming the whole scrollview, not only the image
- 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.
- 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.