IOS bullet Screen Effect

Source: Internet
Author: User

IOS bullet Screen Effect

Bullet screen effect display:

 

Steps:
1.
Create a View Controller in the AppDelegate class and add the created View Controller to the parent view.
[Self. window addSubview: viewController. view]
And self. window. rootViewController = viewController; see my previous article: http://blog.csdn.net/u012989536/article/details/47979875

2.
The next step requires us to think about it and make a good plan for everything. First, make sure to add the label in the view we created: set the label color. The Code has comments.

It is worth noting that our tag is not one. Instead, some labels are rolled cyclically. How can this effect be achieved. First, we cannot directly create it in viewDidLoad to avoid repeated generation. We should write it in a function to facilitate our calls .. So there is such a piece of code:

 

// Three areas to be changed: text color, position, and text content. -(Void) initDate {// as long as there is a view visible to the naked eye, a tag is instantiated starting with the ui // (for real text) UILabel * label = [[UILabel alloc] init]; // specify the location and size. Label. frame = CGRectMake (480, rand () % 290,250, 30); label. text = @ "hello world"; label. textColor = [UIColor redColor]; // Add the label to the current view. [Self. view addSubview: label];}

* 3.
Then we should call a method that will generate a label at intervals of time. This way

 

 

 

-(Void) viewDidLoad {[super viewDidLoad]; // you can set a timer to generate a label [nst1_scheduledtimerwithtimeinterval: 0.1 target: self selector: @ selector (initDate) at intervals) userInfo: nil repeats: YES];}

 

4.

By doing this, we can see some results, because the label will not stop increasing. So how can we make it roll?

At this time, we need to write a move method to control the movement of the label. In this case, the animateWithDuration of UIView will be used.
Method:

(1): First, we can create a plist file to store the labels to be displayed. plist is best formatted as an array.
(2): define an array. Store the data in plist and create an NSSstring string to accept the data. Then the label is displayed as a string to the view: the code is as follows:

// Three areas to be changed: text color, position, and text content. -(Void) initDate {NSString * str = [array objectAtIndex: rand () % array. count]; // as long as there is a view visible to the naked eye, a tag is instantiated starting with the ui (for real text) UILabel * label = [[UILabel alloc] init]; // specify the location and size. Label. frame = CGRectMake (480, rand () % 290,250, 30); label. text = str; label. textColor = [UIColor randomColor]; // Add the label to the current view. [Self. view addSubview: label]; // pass the generated label to the following move function. [Self move: label];}-(void) move :( UILabel *) _ label {[UIView animateWithDuration: 5 animations: ^ {_ label. frame = CGRectMake (-250, _ label. frame. origin. y, _ label. frame. size. width, _ label. frame. size. height);} completion: ^ (BOOL finished) {[_ label removeFromSuperview];}] ;}// the view added (decorated) from this method has been loaded into the memory. -(Void) viewDidLoad {[super viewDidLoad]; // read the plist file NSString * path = [[NSBundle mainBundle] pathForResource: @ bullet screen ofType: @ plist]; array = [[NSArray alloc] initWithContentsOfFile: path]; // you can set a timer to generate a label [nst1_scheduledtimerwithtimeinterval: 0.1 target: self selector: @ selector (initDate) userInfo: nil repeats: YES];}

 

(3): to improve the program, we have different colors for each generated label text. Method: create a classification file of UIColor, then write a method for changing colors:

 

+ (UIColor *) randomColor {/* The color has two forms: rgb rgba rgb 24 R, G, and B. Each color channel has 8-bit 8 binary 255 R, G, b. Each color is 0 ~ 255 120/255 .0 */CGFloat r = arc4random_uniform (256)/255.0; CGFloat g = round (256)/255.0; CGFloat B = arc4random_uniform (256)/255.0; return [UIColor colorWithRed: r green: g blue: B alpha: 1];}

 

 

Note: If a label is not generated, the memory space is occupied. Therefore, the label must be released after the label is generated. Otherwise, the memory will be occupied more and more when the program runs for a long time.

 

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.