Pull-down refresh and pull-up loading without third-party plug-ins leveraging ScrollView itself delegate

Source: Internet
Author: User

The drop-down refresh feature is basically used in all apps, and this feature has been integrated into the apple, but it has to be in the Tableviewcontroller , is a called Uirefreshcontrol the control, want to see the effect can see the phone QQ above the contact list after the refresh. Not much to introduce here.

This blog mainly describes how to achieve the effect of pull-down refresh in ScrollView. Because there are times when we may want to show more directly in ScrollView, not necessarily confined to tableviewcontroller.

Of course there are a lot of on-line pull-down refresh and pull-up loaded third-party controls, but I this person still like to use the System native API to do their own, on the one hand more can thoroughly understand principle, on the other hand to facilitate their own definition.

Well, the nonsense is not much to say, directly on the code:


Preparatory work:

Set the drop-down refresh process and label    Self.indicatorview = [[Uiactivityindicatorview alloc]initwithframe:cgrectmake ( SELF.FRAME.SIZE.WIDTH/2- -25, (+)];    [Self.indicatorview Setcolor:[uicolor Blackcolor];    Self.pullrefreshlabel = [[UILabel alloc]initwithframe:cgrectmake (Self.frame.size.width/2 -20, -30, +)];    Self.pullRefreshLabel.font = [Uifont fontwithname:@ "Heiti SC" size:14];    [Self.pullrefreshlabel settext:@ "drop-down Refresh"];    [Self.scroll_view AddSubview:self.indicatorView];    [Self.scroll_view AddSubview:self.pullRefreshLabel];    [Self.scroll_view BringSubviewToFront:self.indicatorView];    [Self.scroll_view BringSubviewToFront:self.pullRefreshLabel];


the preparatory work here is actually to add a activityindicator and a label to the ScrollView first.

Drop-down Refresh

Drop-down Refresh-(void) scrollviewwillbegindecelerating: (Uiscrollview *) scrollview{if (Scrollview.contentoffset.y <-50) { [UIView animatewithduration:1.0 animations:^{//frame offset, distance from top 50 (can be set by itself) [ScrollView setc            Ontentinset:uiedgeinsetsmake (30, 0, 0, 0)];        [Self.indicatorview startanimating];            } completion:^ (BOOL finished) {//Initiate network request ...            [Self.indicatorview stopanimating];            [Self.pullrefreshlabel settext:@ "drop-down refresh"];            [ScrollView setcontentinset:uiedgeinsetsmake (0, 0, 0, 0)];        Place the current page at 1 currentpage = 1;    }];  }}-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{//Hold indecator position always at the top if (Scrollview.contentoffset.y <        -50) {[Self.pullrefreshlabel settext:@ "release Refresh"];        Self.indicatorView.frame = CGRectMake (self.frame.size.width/2-50, scrollview.contentoffset.y+20, 30, 30); Self.pullRefreshLabel.frame = CGRectMake (self.frame.size.width/2-20, SCROLLVIEW.CONTENTOFFSET.Y+20, 100, 30);        }else{self.indicatorView.frame = CGRectMake (self.frame.size.width/2-50,-30, 30, 30);    Self.pullRefreshLabel.frame = CGRectMake (self.frame.size.width/2-20,-30, 100, 30); }}

Note Two agents do not use the wrong one. One is W illbegindecelerating , one is didscroll

Willbegindecelerating is when we pull down scrollview and then let go, this proxy method will detect the current ScrollView Contentoffset, and then depending on the extent of the drop-down to decide whether to refresh the operation. Here I define a threshold value of 50

Then in order to make the prompt refresh of the label and Activityindicator at a fixed height, is not with the scrollview down and down to go down, in the Didscroll agent to calculate their position.


Pull-up loading:

/pull continue to get-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{/     * *  Key--     *  ScrollView does not have an offset at first, but it sets the size of the contentsize, So contentsize.height will always be higher than the contentoffset.y of a cell phone screen     *  height; the effect of pull-up loading is to ask for more when you swipe to the bottom, and then when you pull up, the offset is generated, can make CONTENTOFFSET.Y + mobile screen size taller than this     *  scroll view of Contentsize.height *    /if (Scrollview.contentoffset.y + ScrollView.frame.size.height >= scrollview.contentsize.height+50) {//        [UIView commitanimations];        [UIView animatewithduration:1.0 animations:^{            //  frame offset, up to the bottom up to 50 (self-setting)            Scrollview.contentinset = uiedgeinsetsmake (0, 0, 0);        } completion:^ (BOOL finished) {            scrollview.contentinset = uiedgeinsetsmake (0, 0, 0, 0);            Send Network Request            CurrentPage + +;            ...        }];    }}














Pull-down refresh and pull-up loading without third-party plug-ins leveraging ScrollView itself delegate

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.