iOS development UI--AutoLabel about the effect of the marquee

Source: Internet
Author: User

Core Ideas

1, start the animation through UIView [UIView beginanimations:@ "Scroll" context:nil]; To the commit animation [UIView commitanimations ]; To achieve the animation effect, in this code between the implementation of the effect to do animation, the middle can set the speed of the animation, whether uniform and monitoring.

2, by setting the offset to achieve the animation effect, you must scroll the text label beyond the parent view to scroll.

3, after the completion of the animation, to determine whether the animation is completed and scrolling text width is greater than the parent view (theoretically greater than, or how to enter the proxy method), add a timer to continue scrolling, the implementation of the stop effect.

4, put two labels, and set a certain spacing, so that you can achieve the effect of circular scrolling, through the array to create Uilabel *_label[2], when left, the initial position is 0,0; the end position is the width + spacing of the first label.

5. Core code

5.1 UIView   beginanimations animation

-(void) scroll{//Delete the previous animation, be sure to add, otherwise the outside will not be able to set the speed, spacing and external variables [Self.layer removeallanimations];//initial position if (_dirtiontype = = D    Irtiontypeleft) {self.contentoffset = cgpointmake (0, 0); }else if (_dirtiontype = = dirtiontyperight) {Self.contentoffset = Cgpointmake (_label[0].frame.size.width+_labelbetw    Eengap, 0);    }//start animation [UIView beginanimations:@ "scroll" context:nil];//animation settings parameters, agent, constant speed, velocity, etc. [UIView setanimationdelegate:self];    [UIView Setanimationcurve:uiviewanimationcurvelinear];    [UIView Setanimationduration:_label[0].frame.size.width/_speed];    [UIView setanimationdidstopselector: @selector (animationDidStop:finished:context:)]; animations//End Position if (_dirtiontype = = dirtiontypeleft) {Self.contentoffset = Cgpointmake (_label[0].frame.size    . width+_labelbetweengap, 0);    }else if (_dirtiontype = = dirtiontyperight) {self.contentoffset = cgpointmake (0, 0); }//complete animation [UIView commitanimations];} Animation completion call-(void) Animationdidstop: (NSString *) animationID Finished: (nsnumber *) Finish context: (NSString *) context{//animation ID Animationid, which is scroll above, you can set multiple animations, This animationid is used to determine the different animations, Finish.integervalue, if 0 indicates incomplete, and 1 indicates the completed if (Finish.integervalue = = 1 && _label[0]. Frame.size.width > Self.frame.size.width) {//pause time, when the animation is complete, add a timer, set the dwell times and then scroll [Nstimer scheduledtimerwit    Htimeinterval:_pausetime target:self selector: @selector (scroll) userinfo:nil repeats:no]; }}

5.2 Re-layout, set scroll position and decide whether you can scroll

-(void) rejustlabels{//set labels frame and self contentsize and is scroll cgfloat offset = 0.0f;        for (Nsinteger i = 0; i < numlabels; i++) {[_label[i] sizetofit];        Cgpoint Center = _label[i].center;        Center.y = SELF.CENTER.Y-SELF.FRAME.ORIGIN.Y;        _label[i].center = center;        CGRect frame = _label[i].frame;        frame.origin.x = offset;    _label[i].frame = frame;//The position of each label x offset + = _label[i].frame.size.width+_labelbetweengap;    } cgsize size;    Size.width = _label[0].frame.size.width+self.frame.size.width+_labelbetweengap;    Size.Height = Self.frame.size.height;    self.contentsize = size;  Determine if you can scroll if (_label[0].frame.size.width > Self.frame.size.width) {//show Lab for (Nsinteger i = 1; i <NumLabels;        i++) {_label[i].hidden = NO;    } [self scroll];        }else{//If you cannot scroll, reset the position of x for (Nsinteger i = 1;i < Numlabels; i++) {_label[i].hidden = YES; }       Cgpoint Center = _label[0].center;        center.x = self.center.x-self.frame.origin.x;    _label[0].center = center; }}

5.3 Cannot re-layout when there is text, because it will jitter

-(void) SetText: (NSString *) text{    //If the text equals the text of the current label, determines whether it is larger than the parent view width, decides whether to scroll, can scroll, set scrolling, and returns if    ([text Isequaltostring:_label[0].text]) {        if (_label[0].frame.size.width > Self.frame.size.width) {            [self scroll ];        }        return;    }    for (Nsinteger i = 0; i< numlabels; i++) {        _label[i].text = text;    }    [Self rejustlabels];}

 code GitHub address : Https://github.com/TheYouth/STRMarquee

iOS Development UI chapter-AutoLabel introduction of the marquee effect

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.