Talking about how iOS does the bomb screen

Source: Internet
Author: User

iOS Bullet screen Demo Online There are many, and most are open source, a lot of source code we can see for ourselves.

What are the features of the barrage?

The most basic features:

1, the longer the text of the barrage, the faster the run, the speed of the barrage and the length of the text is related.

2, the barrage does not collide with each other.

3, if there is data, will be a next hop to play.

Based on the basic features above, we can easily think of the way iOS can be implemented:

1, in the view of a zhen-a-zhen draw

2, with the system of animation to do

The above kind of flexible is very strong, but the requirements for programming skills are relatively high, do not affect the performance, but also do not have a good effect.

The following is a very simple, only need to do the animation segmentation, to deal with the cohesion between the animation can be, but the relatively poor flexibility.

This is done with system animations, so we just need to do the following to be OK:

1, reuse or release;

2. Suspension and continuation;

3, load new data;

4, collision problem;

Here simply put the idea, hoping to open a door for you.

First we make a separate view of the drawing, as our barrage is basically trying, and we just need to put these together in order to be the one we want to play.

@interface Bulletview:uiview@property (nonatomic, copy) void (^moveblock) (commentmovestatus status);// The status callback Block@property (Nonatomic, assign) Nsinteger trajectory;//is located on the track number-(Instancetype) Initwithcommentdic: ( Bulletsettingdic *) commentdic;//here <span style= "font-family:arial, Helvetica, Sans-serif;" >bulletsettingdic</span><span style= "font-family:arial, Helvetica, Sans-serif;" > is a custom class, which mainly sets the background color, text color, font, rate, etc. </span>-(void) Reloaddatawithdic: (Bulletsettingdic *) reloaddic;// Writing this method facilitates reuse-(void) startanimation;-(void) stopanimation;-(void) pauseanimation;-(void) resumeanimation; @end

Commentmovestatus is an enumeration set with several states of animation:

typedef ns_enum (Nsinteger, commentmovestatus) {    Movein,    Enter,    moveout};

Bulletsettingdic is a set class that gives the various settings parameters for the screen view, with the header file as follows:

The contents of this inside, we can arbitrarily customize, and then modify the Write view is good.

@interface bulletsettingdic:nsobject{    nsmutabledictionary *_settingdic;} Set Word color-(void) Setbullettextcolor: (Uicolor *) color;-(Uicolor *) bullettextcolor;//Set background color-(void) Setbulletbackgroundcolor: (Uicolor *) color;-(Uicolor *) bulletbackgroundcolor;//set font-(void) Setbullettextfont: ( Uifont *) font;-(Uifont *) bullettextfont;//settings Content-(void) Setbullettext: (NSString *) text;-(NSString *) bullettext;// Set height-(void) Setbulletheight: (cgfloat) height;-(cgfloat) bulletheight;//set animation Duration-(void) Setbulletanimationduration: ( float) duration;-(float) bulletanimationduration;//set speed ratio-(void) Setbulletanimationspeedrate: (float) speedrate;-( float) bulletanimationspeedrate;-(nsmutabledictionary *) settingdic; @end

Here are the core ideas:

Start Animation-(void) startanimation{__block cgrect frame = self.frame;        __unsafe_unretained typeof (self) weakself = self;        Calculates the time of the move cgfloat dur = (Cgrectgetminx (frame)-_screenwidth)/_real_speed; [UIView Animatewithduration:dur delay:0 options:uiviewanimationoptioncurvelinear animations:^{frame.origin.x = _sc        Reenwidth;    Weakself.frame = frame;         } completion:^ (BOOL finished) {[Weakself.layer removeallanimations];         The barrage begins to enter the screen if (Weakself.moveblock) Weakself.moveblock (Movein);     [Weakself Beginmovein]; }];}    Start move-in full entry-(void) beginmovein{__block cgrect frame = self.frame;        __unsafe_unretained typeof (self) weakself = self;        Calculates the time of the move cgfloat dur = Cgrectgetwidth (frame)/_real_speed; [UIView Animatewithduration:dur delay:0 options:uiviewanimationoptioncurvelinear animations:^{frame.origin.x = _sc        Reenwidth-cgrectgetwidth (frame);    Weakself.frame = frame; } completion:^ (BOOL finished) {[Weakself.layer removeallanimations];         The barrage completely enters the screen if (weakself.moveblock) Weakself.moveblock (enter);     [Weakself Enterin]; }];}    Completely into-and-out-(void) enterin{__block cgrect frame = self.frame;        __unsafe_unretained typeof (self) weakself = self;        CGFloat dur = _screenwidth/_real_speed; [UIView Animatewithduration:dur delay:0 options:uiviewanimationoptioncurvelinear animations:^{frame.origin.x =-CG        Rectgetwidth (frame);    Weakself.frame = frame; } completion:^ (BOOL finished) {//The barrage completely leaves the screen if (Weakself.moveblock) Weakself.moveblock (Move         Out);         [Weakself.layer removeallanimations];     [Weakself Removefromsuperview]; }];}
In this, I've made three animations: start-and-move, move-in, fully move-in, fully-moved, and then have the corresponding block callback after each completion (this is done to make it easier for us to process data and views based on the state, which is used below).

_real_speed is the actual speed of the animation movement, which is obtained by multiplying the speed rate and speed of the animation movement, the speed of the animation is calculated by the screen width and text width as well as the animation time, the speed rate is set.

With regard to pausing and continuing, I use Calayer's camediatimming-related methods to change the speed, record the time, and do it. The code is as follows:

Pause Animation-(void) pauseanimation{    calayer *layer = self.layer;    Layer.fillmode = kcafillmodeforwards;    Cftimeinterval pausedtime = [Layer Converttime:cacurrentmediatime () Fromlayer:nil];    Layer.speed = 0.0;    Layer.timeoffset = Pausedtime;} Continue animation-(void) resumeanimation{    calayer*layer = self.layer;    Cftimeinterval pausedtime = [Layer Timeoffset];    Layer.speed = 1.0;    Layer.timeoffset = 0.0;    Layer.begintime = 0.0;    Cftimeinterval timesincepause = [Layer Converttime:cacurrentmediatime () Fromlayer:nil]-pausedtime;    Layer.begintime = Timesincepause;}
Stop and start are not detailed.

In view reuse, I use two arrays to reuse the view, and when there is a pop-up view moving out of the screen, I save the removed view in an array.

When the next view is made, it is set directly to reload the data instead of recreating the object.

The core operation is simple:

if (_reuseablebulletarray.count)    {        view = [_reuseablebulletarray firstobject];        [View Reloaddatawithdic:_bulletdic];        [_reuseablebulletarray removeobjectatindex:0];    } else    {        view = [[Bulletview alloc] initwithcommentdic:_bulletdic];    } <span style= "font-family:arial, Helvetica, Sans-serif;" >[_bulletarray addobject:view];</span>


And then in the right place (usually the place where the screen is removed from the barrage view):

[_bulletarray Removeobject:weakbulletview]; [_reuseablebulletarray Addobject:weakbulletview];

So you don't have to create a lot of objects.

The whole of a demo here, I hope everyone happy with: Click to jump to the project hosting address






Talking about how iOS does the bomb screen

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.