IOS basic UI (8) UIScrollView

Source: Internet
Author: User

IOS basic UI (8) UIScrollView
What is UIScrollView?

The screen size of a mobile device is extremely limited. When the displayed content is too large, it cannot be displayed when the screen is exceeded. Therefore, with UIScrollView, you can use the scroll gesture to view content outside the screen.

UIScrollView is a scrolling view control that can be used to display a large amount of content and view all the content through scrolling. Application Example: "Settings" on the mobile phone"

 

UIScrollView common attributes

 

// This attribute is used to indicate the scroll position of UIScrollView @ property (nonatomic) CGPoint contentOffset; // This attribute is used to indicate the size of UIScrollView content and the scroll range (how far can it be rolled) @ property (nonatomic) CGSize contentSize; // This property can add an additional rolling area in the four weeks of UIScrollView @ property (nonatomic) UIEdgeInsets contentInset; // set whether UIScrollView requires Spring Effect @ property (nonatomic) BOOL bounces; // set whether UIScrollView can be rolled @ property (nonatomic, getter = isScrollEnabled) BOOL scrollEnabled; // whether to display the horizontal scroll bar @ property (nonatomic) BOOL showsHorizontalScrollIndicator; // whether to display the vertical scroll bar @ property (nonatomic) BOOL showsVerticalScrollIndicator;


 

 

 

 

Common proxy methods for UIScrollView:

 

 

// When the user starts to drag the scrollView, it will call-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {NSLog (================ start to drag ============ );} // As long as scrollView is rolling, it will call-(void) scrollViewDidScroll :( UIScrollView *) scrollView {NSLog (======================%@, NSStringFromCGPoint (scrollView. contentOffset);} // call when the user stops dragging-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {NSLog (============= called when the user stops dragging ========== );} // As long as scrollView is being scaled, it will call-(void) scrollViewDidZoom :( UIScrollView *) scrollView {NSLog (============== scaling in progress ======== );} // when the user uses the kneading gesture, the system calls-(UIView *) viewForZoomingInScrollView (UIScrollView *) scrollView {NSLog (============== start zoom ===========); return self. imageView ;}

 

UIScrollView setting proxy procedure

 

1. Set the Controller delegate. Generally, set the controller of UIScrollView to the delegate of UIScrollView.

There are two ways to set the Controller to UIScrollView's delegate:

(1) using code (self is the Controller)

Self. scrollView. delegate = self;

(2) drag a thread through the storyboard (right-click UIScrollView)

 

2. The Controller should comply with the UIScrollViewDelegate protocol.

 

@interface ViewController ()
 

3. Implement the corresponding methods in the protocol to listen to the UIScrollView rolling process.

 

 

 

UIScrollView Scaling Principle:

When a user uses a kneading gesture on UIScrollView, UIScrollView sends a message to the proxy asking the proxy to zoom in or out its own child control (which part of the content)

 

When a user uses a kneading gesture on UIScrollView, UIScrollView calls the proxy's viewForZoomingInScrollView: method. The control returned by this method is the control that needs to be scaled.


Steps:

 

1. Set UIScrollView id Delegate proxy object

2. Set minimumZoomScale: minimum scale down

3. Set maximumZoomScale: maximum zoom-in ratio.

4. Let the proxy object implement the following method and return the view control to be scaled.

-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView;

 

Other proxy methods related to scaling

Called when scaling is complete

-(Void) scrollViewWillBeginZooming :( UIScrollView *) scrollView withView :( UIView *) view

Called when scaling

-(Void) scrollViewDidZoom :( UIScrollView *) scrollView

 

Image Scaling example:

1. Set storyboard and add UIScrollView and UIImageView

 

2. Set the scollView size, scale ratio, and let the Controller become a proxy, and use modern methods to achieve scaling.

 

/// ViewController. m // scrollview for image scaling # import ViewController. h @ interface ViewController ()
 
  
@ Property (weak, nonatomic) IBOutlet publish * scrollView; @ property (weak, nonatomic) IBOutlet UIImageView * imageView; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // set the size self. scrollView. contentSize = self. imageView. frame. size; // sets the proxy self. scrollView. delegate = self; // set the maximum and minimum zooming ratios of self. scrollView. maximumZoomScale = 2.0; self. scrollView. minimumZoomScale = 0.5 ;}# pragma mark proxy method // call-(void) scrollViewWillBeginDragging (UIScrollView *) when the user starts to drag the scrollView *) scrollView {NSLog (================ start to drag ============ );} // As long as scrollView is rolling, it will call-(void) scrollViewDidScroll :( UIScrollView *) scrollView {NSLog (======================%@, NSStringFromCGPoint (scrollView. contentOffset);} // call when the user stops dragging-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {NSLog (============= called when the user stops dragging ========== );} // As long as scrollView is being scaled, it will call-(void) scrollViewDidZoom :( UIScrollView *) scrollView {NSLog (============== scaling in progress ======== );} // when the user uses the kneading gesture, the system calls-(UIView *) viewForZoomingInScrollView (UIScrollView *) scrollView {NSLog (============== start zoom ===========); return self. imageView;} @ end
 


 

Himalayan example:

1. Set storyboard and add UIScrollView (including content), header view, and bottom view

 

2. Set scollView

 

//// ViewController. m // Himalayan # import ViewController. h @ interface ViewController () @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; @ property (weak, nonatomic) IBOutlet UIButton * lastBtn; @ property (weak, nonatomic) IBOutlet UIView * topView; @ property (weak, nonatomic) IBOutlet UIView * bottomView; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // set the size // CGFloat contentY = self. lastBtn. frame. origin. y + self. lastBtn. frame. size. height + 10; // CGRectGetMaxY obtains the maximum y value CGFloat contentY = CGRectGetMaxY (self. lastBtn. frame) + 10; self. scrollView. contentSize = CGSizeMake (0, contentY); CGFloat topH = self. topView. frame. size. height; CGFloat bottomH = self. bottomView. frame. size. height; // set the starting scroll position self. scrollView. contentOffset = CGPointMake (0,-topH); // Add a scroll area self. scrollView. contentInset = UIEdgeInsetsMake (topH, 0, bottomH + 10, 0);}-(void) didreceivemorywarning {[super didreceivemorywarning];} @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.