Guest User

Untitled

a guest
Jan 20th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. - (id)initWithFrame:(CGRect)frame
  2. {
  3. self = [super initWithFrame:frame];
  4. if (self) {
  5.  
  6. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320, 300)];
  7.  
  8. self.mainView=[[UIView alloc] initWithFrame:CGRectMake(0,0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
  9.  
  10. self.mainView.backgroundColor = [[UIColor alloc] initWithPatternImage:mapBackgroundPattern]; // Assigned some custom color
  11.  
  12. scrollView.delegate=self;
  13.  
  14. CGRect scrollViewFrame = self.scrollView.frame;
  15. CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
  16. CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
  17. CGFloat minScale = MIN(scaleWidth, scaleHeight);
  18. self.scrollView.zoomScale = minScale;
  19. //self.scrollView.minimumZoomScale = 0.1f;
  20. self.scrollView.maximumZoomScale = 10.0f;
  21. self.scrollView.contentSize = mainView.frame.size;
  22.  
  23. UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
  24. doubleTapRecognizer.numberOfTapsRequired = 2;
  25. doubleTapRecognizer.numberOfTouchesRequired = 1;
  26. [self.scrollView addGestureRecognizer:doubleTapRecognizer];
  27.  
  28. [self.scrollView addSubview:self.mainView];
  29. [self addSubview:self.scrollView];
  30. [self addSubview:imgView11];
  31. }
  32. return self;
  33. }
  34.  
  35.  
  36. - (void)centerScrollViewContents {
  37. CGSize boundsSize = self.scrollView.bounds.size;
  38. CGRect contentsFrame = self.mainView.frame;
  39.  
  40. if (contentsFrame.size.width < boundsSize.width) {
  41. contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
  42. } else {
  43. contentsFrame.origin.x = 0.0f;
  44. }
  45.  
  46. if (contentsFrame.size.height < boundsSize.height) {
  47. contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
  48. } else {
  49. contentsFrame.origin.y = 0.0f;
  50. }
  51. self.mainView.frame = contentsFrame;
  52. }
  53.  
  54. - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
  55. // Get the location within the image view where we tapped
  56. CGPoint pointInView = [recognizer locationInView:self.mainView];
  57.  
  58. // Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
  59. CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
  60. newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
  61.  
  62. // Figure out the rect we want to zoom to, then zoom to it
  63. CGSize scrollViewSize = self.scrollView.bounds.size;
  64.  
  65. CGFloat w = scrollViewSize.width / newZoomScale;
  66. CGFloat h = scrollViewSize.height / newZoomScale;
  67. CGFloat x = pointInView.x - (w / 2.0f);
  68. CGFloat y = pointInView.y - (h / 2.0f);
  69.  
  70. CGRect rectToZoomTo = CGRectMake(x, y, w, h);
  71.  
  72. NSLog(@"FloatValues = %f %f %f %f", x, y, w, h);
  73.  
  74. [self.scrollView zoomToRect:rectToZoomTo animated:YES];
  75. }
  76. - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  77. // Return the view that we want to zoom
  78. return self.mainView;
  79. }
  80.  
  81. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  82. // The scroll view has zoomed, so we need to re-center the contents
  83. [self centerScrollViewContents];
  84. }
Advertisement
Add Comment
Please, Sign In to add comment