Use Uilabel to achieve scrolling caption movement effects

Source: Internet
Author: User

Use Uilabel to achieve scrolling caption movement effects

The code in this link also implements this effect.

Https://github.com/cbpowell/MarqueeLabel

The final effect is as follows:

The principle is as follows:

1. Get text

2. Calculate Text width

3. Put this label in ScrollView

4. Set the width of the ScrollView contentsize to the width of the text

5. Making animations

*6. Edge fade effect use a PNG image with transparent pixels

////ROOTVIEWCONTROLLER.M////Copyright (c) 2014 y.x. All rights reserved.//#import "RootViewController.h"#import "YXKit.h"#import "FontPool.h"@interfaceRootviewcontroller ()@end@implementationRootviewcontroller- (void) viewdidload{[Super Viewdidload]; Self.view.backgroundColor=[Uicolor Blackcolor]; //Registering FontsRegister_font (Bundlefont (@"new Pedicle Small meatball body. TTF"),@"new Pedicle Small Meatball body"); //Get textNSString *string=@"like this song of sentiment faint, as if how far away, in lamenting the love of past lives, is so soft, in the prayers of the afterlife of lingering. "Lotus's mind", you like the crystal-like glass, soft and soft to touch my sentimental heartstrings. I, Lotus's mind, who knows?  I, Lotus's reserved, who knows? "; //Initialize labelUILabel *label = [UILabelNew]; Label.text=string; Label.numberoflines=0; Label.textcolor=[Uicolor Cyancolor]; Label.font= [Uifont Fontwithname:custom_font (@"new Pedicle Small Meatball body",0) Size: -. f]; //Calculate DimensionsCgsize size = [Label Boundingrectwithsize:cgsizemake (0,0)]; Label.frame=(cgrect) {cgpointzero, size}; //Initialize ScrollViewUiscrollview *showview =[[Uiscrollview alloc] Initwithframe:cgrectmake (0, -, the, Size.Height)]; Showview.contentsize=size; Showview.showshorizontalscrollindicator=NO;    [ShowView Addsubview:label];        [Self.view Addsubview:showview]; //masks that form edgesUiimageview *imageview =[[Uiimageview alloc] Initwithframe:cgrectmake (0, -, the, Size.Height)]; Imageview.image= [UIImage imagenamed:@"BG"];        [Self.view Addsubview:imageview]; //Animation[UIView animatekeyframeswithduration:TenDelay:7options:uiviewkeyframeanimationoptionallowuserinteraction Animations:^{                                  //calculate the distance to moveCgpoint point =Showview.contentoffset; Point.x= Size.width- the. F; Showview.contentoffset=Point ; } Completion:^(BOOL finished) {}];}@end

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.