Implementation of the iOS bullet screen

Source: Internet
Author: User
<span id="Label3"></p><p><p>Simple bullet screen to achieve a bad place to write please advise us</p></p><p><p>In the project settings to set the horizontal screen;</p></p><p><p>Implementation Principle: with the timer to constantly produce lable, the position of the lable produced in the right side of the screen we can not see, we are writing a mobile method to make the lable move to the leftmost place, and then remove the lable, so that the realization of a simple barrage;</p></p><p><p></p></p><p><p>About Timer Nstimer</p></p><p><p>Create</p></p><p><p>nstimer* MyTimer = [nstimer scheduledtimerwithtimeinterval:<# (nstimeinterval) #> target:<# (id) #> Selector : <# (SEL) #> userinfo:<# (id) #> repeats:<# (BOOL) #>];</p></p><p><p>Timeinterval: wait 1 seconds before executing, as set 1</p></p><p><p>Target: the object being sent</p></p><p><p>Selector: the method of execution in this case we are going to execute the method of randomly creating lable;</p></p><p><p>Userinfo: This parameter can be nil;</p></p><p><p>Repeats: whether the loop, when yes, loops to actual effect or release must be added at this time</p></p><p><p>[[nsrunloop mainrunloop] Addtimer:mytimer formode:nsdefaultrunloopmode];</p></p><p><p>When no is only run once will automatically add the timer to the Mainrunloop Nsdefaultrunloopmode in the use of the notice when the notification can be used;</p></p><p><p></p></p><p><p>Code:</p></p><p><p>#import "MainViewController.h"</p></p><p><p>#import the classification of "uicolor+random.h"//uicolor produce random colors</p></p><p><p>@interface Mainviewcontroller ()</p></p><p><p>{</p></p><p><p>nstimer* mytimer;</p></p><p><p>nsmutablearray* arrays; Store the string to eject</p></p><p><p>uibutton* start; Start button</p></p><p><p>uibutton* close; Close button</p></p><p><p>}</p></p><p><p>@end</p></p><p><p></p></p><p><p>@implementation Mainviewcontroller</p></p><p><p>-(void) Viewdidload</p></p><p><p>{</p></p><p><p>[super viewdidload];</p></p><p><p>arrays = [[nsmutablearray alloc]initwithcapacity:10];</p></p><p><p>Below is a plist file I find from other small code, from which I read the data to do the barrage you can simply build an array to add some strings;</p></p><p><p>nsstring* Path = [[nsbundle mainbundle] pathforresource:@ "dishdata" oftype:@ "plist"];</p></p><p><p>nsarray* array = [[nsarray alloc]initwithcontentsoffile:path];</p></p><p><p>For (nsdictionary* arr in Array) {</p></p><p><p>nsstring* name = [arr objectforkey:@ "name"];</p></p><p><p>nsnumber* Price = [arr objectforkey:@ ' Price '];</p></p><p><p>nsstring* str = [nsstring stringwithformat:@ "%@", price];</p></p><p><p>nsstring* Description = [arr objectforkey:@ "description"];</p></p><p><p>[arrays addobject:name];</p></p><p><p>[arrays addobject:str];</p></p><p><p>[arrays addobject:description]</p></p><p><p>}</p></p><p><p>Some settings for the button</p></p><p><p>Start = [[UIButton alloc] initwithframe:cgrectmake (10, 0, 100, 50)];</p></p><p><p>[start addtarget:self action: @selector (start:) forcontrolevents:uicontroleventtouchupinside];</p></p><p><p>[start settitle:@ "open" forstate:uicontrolstatenormal];</p></p><p><p>[start Setbackgroundcolor:[uicolor greencolor]];</p></p><p><p>[self.view addsubview:start];</p></p><p><p>Close = [[UIButton alloc] initwithframe:cgrectmake (10, 0, 100, 50)];</p></p><p><p>[close addtarget:self action: @selector (endtimer:) forcontrolevents:uicontroleventtouchupinside];</p></p><p><p>[close settitle:@ "off" forstate:uicontrolstatenormal];</p></p><p><p>[close Setbackgroundcolor:[uicolor greencolor];</p></p><p><p>Close.hidden = YES;</p></p><p><p>[self.view addsubview:close];</p></p><p><p>}</p></p><p><p>-(void) start: (id) sender{</p></p><p><p>If (mytimer) {</p></p><p><p>Turn on timer</p></p><p><p>[mytimer setfiredate:[nsdate distantpast];</p></p><p><p>}else{</p></p><p><p>MyTimer = [nstimer scheduledtimerwithtimeinterval:0.5 target:self selector: @selector (initlable:) Userinfo:nil Repeats : YES];</p></p><p><p>}</p></p><p><p>And the Implement button in the Close button hides and reproduces</p></p><p><p>Start.hidden = YES;</p></p><p><p>Close.hidden = NO;</p></p><p><p>}</p></p><p><p>Close Button Implementation method</p></p><p><p><span style="line-height: 1.5;">-(void) endtimer: (id) sender{</span></p></p><p><p>Turn off the timer</p></p><p><p>[mytimer setfiredate:[nsdate distantfuture];</p></p><p><p>Cancel Timer Delete Timer This is the only way to go here.</p></p><p><p>[mytimer invalidate];</p></p><p><p>Close.hidden = YES;</p></p><p><p>Start.hidden = NO;</p></p><p><p>}</p></p><p><p>Create lable</p></p><p><p>-(void) initlable: (id) sender{</p></p><p><p>lable display on the right side of the screen is not displayed on the screen</p></p><p><p>uilabel* lable = [[UILabel alloc] initwithframe:cgrectmake (self.view.frame.size.height, rand ()%30*10, 100, 20)];</p></p><p><p>Randomcolor is a class method that I implemented using Classification.</p></p><p><p>[lable Settextcolor:[uicolor randomcolor];</p></p><p><p>[lable settext:[arrays Objectatindex:rand ()%[arrays count]];</p></p><p><p>[self.view addsubview:lable];</p></p><p><p>[self move:lable];</p></p><p><p>}</p></p><p><p>Move the lable to the left of the screen</p></p><p><p>-(void) move: (uilabel*) lable{</p></p><p><p>[UIView Animatewithduration:2 animations:^{</p></p><p><p>Lable.frame = CGRectMake (0-lable.frame.size.width, lable.frame.origin.y, lable.frame.size.width, lable.frame.size.height);</p></p><p><p>}completion:^ (BOOL Finished) {</p></p><p><p>[lable removefromsuperview];</p></p><p><p>}];</p></p><p><p>}</p></p><p><p>Uicolor classification for generating random colors</p></p><p><p>Method of creating classification: new Objective-c category; Category on uicolor;</p></p><p><p>#import <UIKit/UIKit.h></p></p><p><p>@interface Uicolor (Random)</p></p><p><p>Generate Random Colors</p></p><p><p>+ (uicolor*) randomcolor;</p></p><p><p>@end</p></p><p><p>#import "uicolor+random.h"</p></p><p><p>@implementation Uicolor (Random)</p></p><p><p>+ (uicolor*) randomcolor{</p></p><p><p>static BOOL seed = NO;</p></p><p><p>If (!seed) {</p></p><p><p>Seed = YES;</p></p><p><p>Srandom (time (NULL));</p></p><p><p>}</p></p><p><p>Three primary colors can be mixed out all the colors, and added to the white</p></p><p><p>CGFloat red = (cgfloat) random ()/(cgfloat) rand_max;</p></p><p><p>CGFloat green = (cgfloat) random ()/(cgfloat) rand_max;</p></p><p><p>CGFloat blue = (cgfloat) random ()/(cgfloat) rand_max;</p></p><p><p>return [uicolor colorwithred:red green:green blue:blue Alpha:1.0f];//alpha for transparency 0 fully transparent 1 opaque</p></p><p><p></p></p><p><p>}</p></p><p><p>@end</p></p><p><p></p></p><p><p>Implementation of the iOS bullet screen</p></p></span>

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.