Received 3: drop-down Refresh control 1

Source: Internet
Author: User

This article focuses on a drop-down refresh control that was encountered in a recent project, such as the effect of this control:

Here we will use two posts to parse the control, the first parsing the control's frame, and the second parsing the animation. The source code can be downloaded at the following link:

Testpulltorefresh.zip

1. This control consists of the following files: Gmpulltoaction, Circleprogressview, Gmactivityview, Where the Gmpulltoaction file contains two classes: Gmpulltorefresh and Uiscrollview (gmpulltoaction), Circleprogressview and Gmactivityview each contain a class of the same name.

Of these 4 classes, Gmpulltorefresh and Uiscrollview (gmpulltoaction) are the controls ' frames, and Circleprogressview and Gmactivityview are responsible for animation.

2. This control is defined within Uiscrollview (gmpulltoaction), so the consumer must be an instance of Uiscrollview or its subclasses, and 3 methods need to be called when used (assuming that the current controller self has a property ScrollView):

[Self.scrollview addpulltorefreshwithactionhandler:^{    // drop-down code executed during refresh ...     }];

Then you need to implement a proxy method for uiscrollviewdelegate:

// This method is constantly called when ScrollView is dragged -(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {     if (ScrollView = = Self.scrollview) {        [ScrollView didscroll];}    }

Finally, after the load is complete, call the following method to deactivate the control:

[Self.scrollView.pullToRefreshView stopanimating];

We parse the control with these 3 methods as portals.

3. First, the first method-(void) Addpulltorefreshwithactionhandler: (void (^) (void)) Actionhandler method:

(1), this method is defined in the Uiscrollview (Gmpulltoaction) class, its code is as follows:

-(void) Addpulltorefreshwithactionhandler: (void (^) (void)) Actionhandler {    *pulltorefreshview = [[Gmpulltorefresh alloc] initwithscrollview:self];     = Actionhandler;     = Pulltorefreshview;}

It instantiates an object Pulltorefreshview of a Gmpulltorefresh class using the Initwithscrollview method, Assigning a block parameter to Pulltorefreshview.actionhandler allows Pulltorefreshview to use the code later, and finally assigns Pulltorefreshview to its own property self.pulltorefreshview.

It is important to note that the Uiscrollview (Gmpulltoaction) class is a classification that does not allow the direct definition of attributes, so it is necessary to use a different method to implement the effect of the property, the code is as follows:

@interfaceUiscrollview (gmpulltoaction) @property (nonatomic, strong) Gmpulltorefresh*Pulltorefreshview;@end...#import<objc/runtime.h>Static CharUiscrollviewpulltorefreshview;@implementationUiscrollview (gmpulltoaction) @dynamic Pulltorefreshview;- (void) Setpulltorefreshview: (Gmpulltorefresh *) Pulltorefreshview {[Self Willchangevalueforkey:@"Pulltorefreshview"]; Objc_setassociatedobject (Self,&Uiscrollviewpulltorefreshview, Pulltorefreshview, Objc_associa    Tion_assign); [Self Didchangevalueforkey:@"Pulltorefreshview"];} -(Gmpulltorefresh *) Pulltorefreshview {returnObjc_getassociatedobject (Self, &uiscrollviewpulltorefreshview);} ...@end

(2), and then continue to see the Gmpulltorefresh class instantiation method Initwithscrollview:

- (ID) Initwithscrollview: (Uiscrollview *) ScrollView {//InitializeSelf =[Super Initwithframe:cgrectzero]; Self.scrollview=ScrollView; Self.frame= CGRectMake (0, -kframeheight, ScrollView.bounds.size.width, kframeheight);    [_scrollview addsubview:self]; //Customizing hint textSelf.titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (kcontent_width/2. f the/4. F, self.bounds.size.height*0.5-Ten, the, -)]; _titlelabel.font= [Uifont boldsystemfontofsize: -]; _titlelabel.backgroundcolor=[Uicolor Clearcolor]; _titlelabel.textcolor=Ktextcolor;       [Self Addsubview:_titlelabel]; //Rectangle rising Animation diagram    ...       //Circle Rotation Animation    ...       //Specify StateSelf.state =Gmpulltorefreshstatehidden; returnSelf ;}

Of these, two of the classes responsible for animation are not discussed first, mainly see Self.state. It is an enumeration value that is used to record the state of the control, which is defined as follows:

enum {    1,  / / Hide    gmpulltorefreshstatevisible,/     /  Visible    gmpulltorefreshstatetriggered,   // triggered refresh    Gmpulltorefreshstateloading      // has been loading };typedef nsuinteger gmpulltorefreshstate;

4, then see the second entry method-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView:

(1), this method will be called when ScrollView drag, in this method is mainly executed code [ScrollView Didscroll]. -(void) The Didscroll method is also defined in the Uiscrollview (Gmpulltoaction) class, and its code is as follows:

-(void) didscroll {    // This self means scrollview    cgpoint point=  Self.contentoffset;     if (Self.pulltorefreshview) {        [Self.pulltorefreshview scrollviewdidscroll:point];}    }

In this method, the ScrollView real-time Contentoffset is passed to the Gmpulltorefresh method-(void) Scrollviewdidscroll: (Cgpoint) Contentoffset;

(2), Gmpulltorefresh-(void) Scrollviewdidscroll: (Cgpoint) The Contentoffset method code is as follows:

- (void) Scrollviewdidscroll: (cgpoint) Contentoffset {if(Self.state = =gmpulltorefreshstateloading) {        return; }       //The y value (negative number) of the starting point, which is also the height of the Pulltorefreshview and the height that triggers the refresh state.CGFloat Scrolloffsetthreshold =SELF.FRAME.ORIGIN.Y; //has triggered a refresh and let go of the drag, it becomes the loading state    if(Self.state = = gmpulltorefreshstatetriggered &&!)self.scrollView.isDragging) {Self.state=gmpulltorefreshstateloading; //ScrollView starts dragging down (<0) and does not reach the height of the trigger refresh (>scrolloffsetthreshold), which is the visible state}Else if(Contentoffset.y <0&& contentoffset.y > Scrolloffsetthreshold && self.scrollView.isDragging && self.state! =gmpulltorefreshstateloading) {Self.state=gmpulltorefreshstatevisible; //ScrollView Drag down to the height of the trigger refresh (<scrolloffsetthreshold) and not let go, for the trigger state}Else if(Contentoffset.y <= scrolloffsetthreshold && self.scrollView.isDragging && self.state = =gmpulltorefreshstatevisible) {Self.state=gmpulltorefreshstatetriggered; //ScrollView push up, for hidden state}Else if(Contentoffset.y >=0&& Self.state! =Gmpulltorefreshstatehidden) {Self.state=Gmpulltorefreshstatehidden; }       //animation effects during drag    ...}

This method determines the state self.state based on the ScrollView real-time Contentoffset, which invokes the Self.state set method.

(3), Gmpulltorefresh SetState: The method code is as follows:

- (void) SetState: (gmpulltorefreshstate) newstate {_state=newstate; Switch(newstate) { CaseGmpulltorefreshstatehidden: ...//Animation[Self setscrollviewcontentinsettop:0];  Break;  Casegmpulltorefreshstatevisible: _titlelabel.text= Nslocalizedstring (@"Drop- down refresh ...",); ...             Break;  Casegmpulltorefreshstatetriggered: _titlelabel.text= Nslocalizedstring (@"Release Refresh ...",); ...             Break;  Casegmpulltorefreshstateloading: _titlelabel.text= Nslocalizedstring (@"Loading in ...",);            ...                       [Self setScrollViewContentInsetTop:self.frame.size.height]; if(_actionhandler) _actionhandler ();  Break; }}

As you can see, for different states, a different text is specified (the statement for the specified animation is omitted, the next one is discussed), and in the loading state, the block code passed in by the first entry method is invoked, that is, the statement that executes the load.

(4), in the above method, the hidden state and the load state will also call a method Setscrollviewcontentinset: To specify the contentinset of ScrollView. When the status is hidden, the top of the Contentinset is set to 0, and the top of the Contentinset is set to the height of Pulltorefreshview when the state is loaded. And animate the process:

-(void) Setscrollviewcontentinsettop: (cgfloat) Top {    = self.scrollView.contentInset;     = top;    [UIView animatewithduration: 0.3 delay:0 options:uiviewanimationoptionallowuserinteraction| Uiviewanimationoptionbeginfromcurrentstate animations:^{        = inset;           } completion:^ (BOOL finished) {    }];}

5, the last entry method, is called when the data is loaded, call this method will put the state to hide, so that the loading screen animation to revert to the pre-pull status:

-(void) stopanimating {    = Gmpulltorefreshstatehidden;}

6. This completes the frame of the dropdown refresh control, and the next post will analyze the animation effect in this control.

Received 3: drop-down Refresh control 1

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.