Return to Snippet

Revision: 58928
at August 10, 2012 02:31 by zopebie


Initial Code
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.

-(void) viewDidLoad 
{
    [super viewDidLoad];

    // Set the frame that is twice the size of the screen
    CGRect scrollFrame = CGRectMake(20, 90, 280, 280);
	
	// In this example, the UIImage size is greater than the scrollFrame size
	UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
	UIImageView *largeImageView = [[UIImageView alloc] initWithImage:bigImage];
   
   
    // Create the UIScrollView to have the size of the window, matching its size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
	
	[scrollView addSubview:largeImageView]; 
	
	// Tell the scrollView how big its subview is
    scrollView.contentSize = largeImageView.frame.size;   // Important
   
    [self.view addSubview:scrollView];
}	

- You can hide these scroll bars using the showsHorizontalScrollIndicator and showsVerticalScrollIndicator properties
- 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.



Paging 

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. 


-(void) viewDidLoad 
{
    [super viewDidLoad];

   
    // Create the UIScrollView to have the size of the view, matching its size
    CGRect screenRect = [[self view] bounds];
	
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
	[scrollView setPagingEnabled:YES];
	[[self view] addSubview:scrollView];

    // Create the page with a frame that is twice the size of the screen
    CGRect bigRect = screenRect;
	bigRect.size.width *= 2.0;
    HypnosisView *view = [[HypnosisView alloc] initWithFrame:screenRect];
	
	// Move the rectangle for the other HypnosisView to the right, just off
	// the screen
	screeRect.origin.x = screenRect.size.width;
	HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:screenRect];
    [scrollView addSubview:view];
	    
    // Tell the scrollView how big its subview is
    [scrollView setContentSize:bigRect.size];

}


Zoom

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.

Delegate:           <UIScrollViewDelegate>
Instance variable: 	UIImageView *largeImageView


-(void) viewDidLoad 
{
    [super viewDidLoad];

    // Set the frame that is twice the size of the screen
    CGRect scrollFrame = CGRectMake(20, 90, 280, 280);
	
	// Create the UIScrollView to have the size of the window, matching its size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
	scrollView.minimumZoomScale = 0.5;
	scrollView.maximumZoomScale = 2.0;
	scrollView.delegate = self;
	
	// In this example, the UIImage size is greater than the scrollFrame size
	UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
	largeImageView = [[UIImageView alloc] initWithImage:bigImage];
	
	// Tell the scrollView how big its subview is
    scrollView.contentSize = largeImageView.frame.size;   // Important

    [scrollView addSubview:largeImageView]; 
   
    [self.view addSubview:scrollView];
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return largeImageView;
}

Initial URL

                                

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

Initial Title
Scrolling, Panning, Zooming with UIScrollView

Initial Tags
ios

Initial Language
Objective C