IOS development-UIScrollView details, ios-uiscrollview
UIScrollView
The screen size of mobile devices is extremely limited, so the content displayed directly in front of the user is quite limited.
When more content is displayed than one screen, you can use the scroll gesture to view content outside the screen.
Normal UIView does not support scrolling and cannot display too much content
UIScrollView is a scrolling view control that can be used to display a large amount of content and view all the content through scrolling.
Example: "Settings" and other sample programs on mobile phones
UIScrollView usage
Add the content to be displayed to UIScrollView
Set the contentSize attribute of UIScrollView to tell UIScrollView the size of all content, that is, to tell it the rolling range (how far can it be rolled, where is the end)
UIScrollView cannot be rolled
If UIScrollView cannot be rolled, it may be due to the following reasons: 1. contentSize2.scrollEnabled = NO3. no touch event is received: userInteractionEnabled = NO4. autolayout function is not canceled (autolayout must be canceled to scroll scrollView)
UIScrollView proxy (delegate)
Most of the time, we want to perform some specific operations when UIScrollView is rolling or rolling to a certain position or stopping the rolling, the precondition is that you can monitor the entire Rolling Process of UIScrollView. When a series of rolling operations occur in UIScrollView, its proxy (delegate) object is automatically notified and corresponding messages are sent to its proxy, let the proxy know its rolling status. That is to say, to listen to the rolling process of UIScrollView, you must first define the methods to be implemented by delegate in the UIScrollViewDelegate protocol, therefore, to become the delegate of UIScrollView, you must abide by the UIScrollViewDelegate protocol and then implement the corresponding methods in the protocol to listen to the rolling process of UIScrollView.
UIScrollView and Controller
Generally, there are two ways to set the UIScrollView controller to UIScrollView's delegate controller to UIScrollView: by code (self is the Controller) self. scrollView. delegate = self;
Drag a thread through the storyboard (right-click UIScrollView)
Common UIScrollView attributes
@ Property (nonatomic) CGPoint contentOffset; // This attribute is used to indicate the rolling position of UIScrollView @ property (nonatomic) CGSize contentSize; // This attribute is used to indicate the size of UIScrollView content, rolling range (how far can be rolled) @ property (nonatomic) UIEdgeInsets contentInset; // This property can add an additional rolling area in four weeks of UIScrollView @ property (nonatomic) BOOL bounces; // set whether UIScrollView requires Spring Effect @ property (nonatomic, getter = isScrollEnabled) BOOL scrollEnabled; // set whether UIScrollView can scroll @ property (nonatomic) BOOL showsHorizontalScrollIndicator; // whether to display the horizontal scroll bar @ property (nonatomic) BOOL showsVerticalScrollIndicator; // whether to display the vertical scroll bar
Common proxy methods for UIScrollView
// Call-(void) scrollViewWillBeginDragging (UIScrollView *) scrollView when you start dragging; // call-(void) scrollViewDidScroll :( UIScrollView *) scrollView when you scroll to a certain position; // call-(void) scrollViewDidEndDragging when the user stops dragging: (UIScrollView *) scrollView willDecelerate :( BOOL) decelerate;
UIScrollView Scaling
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)
1. Implement UIScrollViewDelegate Protocol
// Call-(UIView *) viewForZoomingInScrollView (UIScrollView *) scrollView when you use the kneading gesture;
Compliance with the agreement and implementation of appropriate methods
id<UIScrollViewDelegate> delegate
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.
Scaling steps
Set UIScrollView id <UISCrollViewDelegate> delegate proxy object setting minimumZoomScale: min scale down setting maximumZoomScale: the maximum scale of the zoom in allows the proxy object to implement the following method, return the view control to be scaled-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView; Call (void) scrollViewWillBeginZooming :( UIScrollView *) when other scaling-related proxy methods are completed *) scrollView withView :( UIView *) Call-(void) scrollViewDidZoom when the view is being scaled: (UIScrollView *) scrollView
UIScrollView and UIPageControl pages
As long as the pageEnabled attribute of UIScrollView is set to YES, UIScrollView is divided into multiple independent pages, and the content in it can be displayed by page. Generally, the pageEnabled attribute is used with UIPageControl to enhance the paging effect, UIPageControl common attributes: // total number of pages @ property (nonatomic) NSInteger numberOfPages; // currently displayed page number @ property (nonatomic) NSInteger currentPage; // only one page is displayed, whether to hide the page number indicator @ property (nonatomic) BOOL hidesForSinglePage; // color of other page number indicators @ property (nonatomic, retain) UIColor * pageIndicatorTintColor; // The Color of the current page number indicator @ property (nonatomic, retain) UIColor * currentPageIndicatorTintColor;
Use of NSTimer
NSTimer is called a "timer". Its function is as follows:
Execute the specified task at the specified time
Execute a specified task at intervals
Call the following method to enable a scheduled task + (NSTimer *) scheduledTimerWithTimeInterval :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo;
Call the aSelector method of aTarget every ti seconds. yesOrNo determines whether to execute this task repeatedly.
The invalidate method can be used to stop the timer. Once the timer is stopped, the task cannot be executed again. Only a new timer can be created to execute a new task.
- (void)invalidate;
UIScrollView instance
/*** 1. condition for proxy: UIScrollView defines all the methods to be implemented by delegate in the UIScrollViewDelegate protocol. Therefore, to become the delegate of UIScrollView, you must abide by the UIScrollViewDelegate protocol and then implement the corresponding methods in the Protocol, you can monitor the rolling process of UIScrollView. 2. set UIScrollView id <UISCrollViewDelegate> delegate proxy object setting minimumZoomScale: min scale down setting maximumZoomScale: the maximum scale of the zoom in allows the proxy object to implement the following method, return the view control to be scaled-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView; */# import "ViewController. h "@ interface ViewController () <UIScrollViewDelegate> @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; @ property (weak, nonatomic) IBOutlet UIImageView * minionView; @ end // delegate proxy mode @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // set the content Size self. scrollView. contentSize = self. minionView. frame. size; // set self. scrollView. delegate = self; // set the maximum and minimum zooming ratios of self. scrollView. maximumZoomScale = 2.0; self. scrollView. minimumZoomScale = 0.2;}/*** when a user starts to drag a scrollView, the system calls */-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {NSLog (@ "start to drag .... ");}/*** as long as the scrollView is rolling, it will call */-(void) scrollViewDidScroll :( UIScrollView *) scrollView {NSLog (@" rolling ------ % @", NSStringFromCGPoint (scrollView. contentOffset);}/*** when a user uses a kneading gesture, the control returned by * @ return is called as the control to be scaled */-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView {NSLog (@ "start scaling -----"); return self. minionView;}-(void) scrollViewDidZoom :( UIScrollView *) scrollView {NSLog (@ "zooming ----");}