Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320, 300)];
- self.mainView=[[UIView alloc] initWithFrame:CGRectMake(0,0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
- self.mainView.backgroundColor = [[UIColor alloc] initWithPatternImage:mapBackgroundPattern]; // Assigned some custom color
- scrollView.delegate=self;
- CGRect scrollViewFrame = self.scrollView.frame;
- CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
- CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
- CGFloat minScale = MIN(scaleWidth, scaleHeight);
- self.scrollView.zoomScale = minScale;
- //self.scrollView.minimumZoomScale = 0.1f;
- self.scrollView.maximumZoomScale = 10.0f;
- self.scrollView.contentSize = mainView.frame.size;
- UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
- doubleTapRecognizer.numberOfTapsRequired = 2;
- doubleTapRecognizer.numberOfTouchesRequired = 1;
- [self.scrollView addGestureRecognizer:doubleTapRecognizer];
- [self.scrollView addSubview:self.mainView];
- [self addSubview:self.scrollView];
- [self addSubview:imgView11];
- }
- return self;
- }
- - (void)centerScrollViewContents {
- CGSize boundsSize = self.scrollView.bounds.size;
- CGRect contentsFrame = self.mainView.frame;
- if (contentsFrame.size.width < boundsSize.width) {
- contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
- } else {
- contentsFrame.origin.x = 0.0f;
- }
- if (contentsFrame.size.height < boundsSize.height) {
- contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
- } else {
- contentsFrame.origin.y = 0.0f;
- }
- self.mainView.frame = contentsFrame;
- }
- - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
- // Get the location within the image view where we tapped
- CGPoint pointInView = [recognizer locationInView:self.mainView];
- // Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
- CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
- newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
- // Figure out the rect we want to zoom to, then zoom to it
- CGSize scrollViewSize = self.scrollView.bounds.size;
- CGFloat w = scrollViewSize.width / newZoomScale;
- CGFloat h = scrollViewSize.height / newZoomScale;
- CGFloat x = pointInView.x - (w / 2.0f);
- CGFloat y = pointInView.y - (h / 2.0f);
- CGRect rectToZoomTo = CGRectMake(x, y, w, h);
- NSLog(@"FloatValues = %f %f %f %f", x, y, w, h);
- [self.scrollView zoomToRect:rectToZoomTo animated:YES];
- }
- - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
- // Return the view that we want to zoom
- return self.mainView;
- }
- - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
- // The scroll view has zoomed, so we need to re-center the contents
- [self centerScrollViewContents];
- }
Advertisement
Add Comment
Please, Sign In to add comment