Posted By

zopebie on 08/10/12


Tagged

UIScrollView ios


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

zopebie


Scrolling, Panning, Zooming with UIScrollView


 / Published in: Objective C
 

A sample code to show the scrolling, panning and zooming with UIScrollView

  1. Scrolling - A scroll view acts as a container for a larger subview, allowing you to pan around the subview by touching the screen. Vertical and horizontal scroll bars indicate the position in the subview.
  2.  
  3. -(void) viewDidLoad
  4. {
  5. [super viewDidLoad];
  6.  
  7. // Set the frame that is twice the size of the screen
  8. CGRect scrollFrame = CGRectMake(20, 90, 280, 280);
  9.  
  10. // In this example, the UIImage size is greater than the scrollFrame size
  11. UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
  12. UIImageView *largeImageView = [[UIImageView alloc] initWithImage:bigImage];
  13.  
  14.  
  15. // Create the UIScrollView to have the size of the window, matching its size
  16. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
  17.  
  18. [scrollView addSubview:largeImageView];
  19.  
  20. // Tell the scrollView how big its subview is
  21. scrollView.contentSize = largeImageView.frame.size; // Important
  22.  
  23. [self.view addSubview:scrollView];
  24. }
  25.  
  26. - You can hide these scroll bars using the showsHorizontalScrollIndicator and showsVerticalScrollIndicator properties
  27. - If you play around with the previous code, you’ll notice that if you scroll quickly to the edge of the subview, the scroll view actually moves a little too far before springing back. This behavior is controlled by the bounce property. You can restrict bouncing to the x- or y-axis using the alwaysBounceHorizontal and alwaysBounceVertical properties, or you can disable it entirely by setting bounce to NO.
  28.  
  29.  
  30.  
  31. Paging
  32.  
  33. Scroll views support the paging of their content—the ability to add multiple subviews as “pages” and then scroll between them as you might turn the pages of a book.
  34.  
  35.  
  36. -(void) viewDidLoad
  37. {
  38. [super viewDidLoad];
  39.  
  40.  
  41. // Create the UIScrollView to have the size of the view, matching its size
  42. CGRect screenRect = [[self view] bounds];
  43.  
  44. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
  45. [scrollView setPagingEnabled:YES];
  46. [[self view] addSubview:scrollView];
  47.  
  48. // Create the page with a frame that is twice the size of the screen
  49. CGRect bigRect = screenRect;
  50. bigRect.size.width *= 2.0;
  51. HypnosisView *view = [[HypnosisView alloc] initWithFrame:screenRect];
  52.  
  53. // Move the rectangle for the other HypnosisView to the right, just off
  54. // the screen
  55. screeRect.origin.x = screenRect.size.width;
  56. HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:screenRect];
  57. [scrollView addSubview:view];
  58.  
  59. // Tell the scrollView how big its subview is
  60. [scrollView setContentSize:bigRect.size];
  61.  
  62. }
  63.  
  64.  
  65. Zoom
  66.  
  67. You can also zoom in and out of an image using a scroll view. The minimumZoomScale and maximumZoomScale properties control the scale by which you can zoom in and out. By default, both of these properties are set to the same value (1.0), which disables zooming. You must implement one of the UIScrollViewDelegate methods to return the view that is being zoomed.
  68.  
  69. Delegate: <UIScrollViewDelegate>
  70. Instance variable: UIImageView *largeImageView
  71.  
  72.  
  73. -(void) viewDidLoad
  74. {
  75. [super viewDidLoad];
  76.  
  77. // Set the frame that is twice the size of the screen
  78. CGRect scrollFrame = CGRectMake(20, 90, 280, 280);
  79.  
  80. // Create the UIScrollView to have the size of the window, matching its size
  81. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
  82. scrollView.minimumZoomScale = 0.5;
  83. scrollView.maximumZoomScale = 2.0;
  84. scrollView.delegate = self;
  85.  
  86. // In this example, the UIImage size is greater than the scrollFrame size
  87. UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
  88. largeImageView = [[UIImageView alloc] initWithImage:bigImage];
  89.  
  90. // Tell the scrollView how big its subview is
  91. scrollView.contentSize = largeImageView.frame.size; // Important
  92.  
  93. [scrollView addSubview:largeImageView];
  94.  
  95. [self.view addSubview:scrollView];
  96. }
  97.  
  98.  
  99. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  100. {
  101. return largeImageView;
  102. }

Report this snippet  

You need to login to post a comment.