IOS pull-down refresh-pull-up loading principle, ios Load

Source: Internet
Author: User

IOS pull-down refresh-pull-up loading principle, ios Load
Preface explain the attributes of UIScrollView before refresh and load down.

  • ContentSize is the area where UIScrollView can be rolled.
  • ContentOfinset the apple official documentation explains that "the distance between the Content View and the closed rolling View is actually the margin between the Content View of scrollView and the shell of scrollView, it is actually a bit similar to the pading attribute in CSS.
  • ContentOffset is the offset of the vertex in the current display area of UIScrollView to the frame vertex. For example, if the preceding example is pulled to the bottom, contentOffset is (0,480), that is, y offset is 480.
Pull-down refresh and pull-up Loading Principles
  • Pull-down refresh is actually to monitor the y value of the contentOffset of UIScrollView. When it is pulled down, the contentOffset of UIScrollView is reduced all the time, and then the contentInset value is controlled into a value through animation, then, the contentInset value of UIScrollView is restored to the origin by setting the delay. Let alone code
// Refresh if (scrollView. contentOffset. y <-100) {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (100, 0, 0);} completion: ^ (BOOL finished) {NSLog (@ "initiate pull-down refresh"); dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (0, 0, 0, 0) ;}] ;}
  • The principle of uploading is basically the same as that of pull-down refresh, except that the contentOffset value is different if scrollView. bounds. size. height + scrollView. contentOffset. y> scrollView. contentSize. height indicates that you have performed the pull-up operation, and then the implementation is basically the same as the pull-down refresh.
// Pull up and load if (scrollView. bounds. size. height + scrollView. contentOffset. y> scrollView. contentSize. height) {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (0, 0, 50, 0);} completion: ^ (BOOL finished) {NSLog (@ "initiate pull loading"); dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (0, 0, 0, 0) ;}] ;}
The complete demo code is provided below. If you don't understand it, you can talk to me privately. If you want to learn more, you can go to github to search for MJRefresh to see the complete third-party encapsulated by others.
# Import "ViewController. h "@ interface ViewController () <UIScrollViewDelegate> @ property (nonatomic, strong) UIScrollView * scrollView; @ end @ implementation ViewController-(UIScrollView *) scrollView {if (! _ ScrollView) {_ scrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; // contentSize is the slidable area _ scrollView. contentSize = CGSizeMake (0,900); _ scrollView. backgroundColor = [UIColor grayColor]; _ scrollView. delegate = self; // greenView is actually the content View UIView of UIScrollView * greenView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, self. view. bounds. size. width, 900)]; greenView. backgroundColo R = [UIColor greenColor]; [_ scrollView addSubview: greenView];} return _ scrollView;}-(void) scrollViewWillBeginDecelerating :( UIScrollView *) scrollView {// pull-down refresh-when the pull-down refresh is performed, contentOffset is actually the offset of greenView relative to the upper left corner of the screen. If (scrollView. contentOffset. y <-100) {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (100, 0, 0, 0);} completion: ^ (BOOL finished) {NSLog (@ "initiate pull-down refresh "); // set the latency to 2 seconds: dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), interval (), ^ {[UIView animateWithDuration: 1.0 animations: ^ {// restore the original contentInset and let greenView return to the original self. scrollView. contentInset = UIEdgeInsetsMake (0, 0, 0, 0) ;}] ;}/// pull up and load if (scrollView. bounds. size. height + scrollView. contentOffset. y> scrollView. contentSize. height) {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (0, 0, 50, 0);} completion: ^ (BOOL finished) {NSLog (@ "initiate pull loading"); dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {[UIView animateWithDuration: 1.0 animations: ^ {self. scrollView. contentInset = UIEdgeInsetsMake (0, 0, 0, 0) ;}] ;}}- (void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self. view addSubview: self. scrollView];}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ 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.