Write a small project to launch fireworks using OC and Swift in two languages

Source: Internet
Author: User

The two implementations of OC and Swift are largely indistinguishable, mainly in the way that some objects or methods are called differently

OC Code Style:

Self.view.backgroundColor = [Uicolor blackcolor];

Loading a grainy spark picture
Caemitterlayer *emitterla = [Caemitterlayer layer];
Emitterla.emitterposition = Cgpointmake (SELF.VIEW.BOUNDS.SIZE.WIDTH/2, self.view.bounds.size.height* (. 75));//* ( 0.75)
Emitterla.rendermode = kcaemitterlayeradditive;

Gradually hide the emission particles before the explosion
Caemittercell *emittercerocket = [Caemittercell Emittercell];
Emittercerocket.emissionlongitude =-M_PI/2;
Emittercerocket.emissionlatitude = 0;
Emittercerocket.lifetime = 1.6;
Emittercerocket.birthrate = 1;
Emittercerocket.velocity = 400;
Emittercerocket.velocityrange = 100;
Emittercerocket.yacceleration = 250;
Emittercerocket.emissionrange = M_PI/4;
Emittercerocket.color = Cgcolorcreatecopy ([uicolor colorwithred:0.5 green:0.5 blue:0.5 alpha:0.5]. Cgcolor);
Emittercerocket.redrange = 0.5;
Emittercerocket.greenrange = 0.5;
Emittercerocket.bluerange = 0.5;
Set the path name for the animation effect
[Emittercerocket setname:@ "Rocket"];

Add Path state
Caemittercell *emittercefly = [Caemittercell Emittercell];
Emittercefly.contents = (ID) [UIImage imagenamed:@ "Tspark.png"]. Cgimage;
Emittercefly.emissionlongitude = (4*M_PI)/2;
Emittercefly.scale = 0.4;
emittercefly.velocity = 100;
Emittercefly.birthrate = 45;
Emittercefly.lifetime = 1.5;
Emittercefly.yacceleration = 350;
Emittercefly.emissionrange = M_PI/7;
Emittercefly.alphaspeed =-0.7;
Emittercefly.scalespeed =-0.1;
Emittercefly.scalerange = 0.1;
Emittercefly.begintime = 0.01;
Emittercefly.duration = 0.7;

Set explosion
Caemittercell *emittercefirework = [Caemittercell Emittercell];
Emittercefirework.contents = (ID) [UIImage imagenamed:@ "Tspark.png"]. Cgimage;
Emittercefirework.birthrate = 9999;
Emittercefirework.scale = 0.6;
Emittercefirework.velocity = 130;
Emittercefirework.lifetime = 2;
Emittercefirework.alphaspeed =-0.2;
Emittercefirework.yacceleration = 80;
Emittercefirework.begintime = 1.5;
Emittercefirework.duration = 0.1;
Emittercefirework.emissionrange = 2*M_PI;
Emittercefirework.scalespeed =-0.1;
Emittercefirework.spin = 2;
Set explosion animation name
[Emittercefirework setname:@ "Firework"];

Adding a repeating process
Caemittercell *emitterceprespark = [Caemittercell Emittercell];
Emitterceprespark.birthrate = 80;
emitterceprespark.velocity = emittercefirework.velocity*0.7;
Emitterceprespark.lifetime = 1.7;
Emitterceprespark.yacceleration = emittercefirework.yacceleration*0.85;
Emitterceprespark.begintime = emittercefirework.begintime-0.2;
Emitterceprespark.emissionrange = Emittercefirework.emissionrange;
Emitterceprespark.greenspeed = 100;
Emitterceprespark.bluespeed = 100;
Emitterceprespark.redspeed = 100;
Set Duplicate animation name
[Emitterceprespark setname:@ "Prespark"];

The last flash of fireworks
Caemittercell *emittercesparkle = [Caemittercell Emittercell];
Emittercesparkle.contents = (ID) [UIImage imagenamed:@ "Tspark.png"]. Cgimage;
Emittercesparkle.lifetime = 0.05;
Emittercesparkle.yacceleration = 250;
Emittercesparkle.begintime = 0.8;
Emittercesparkle.scale = 0.4;
Emittercesparkle.birthrate = 10;

Emitterceprespark.emittercells = [Nsarray arraywithobjects:emittercesparkle, Nil];
Emittercerocket.emittercells = [Nsarray arraywithobjects:emittercefly,emittercefirework,emitterceprespark, Nil];
Emitterla.emittercells = [Nsarray arraywithobjects:emittercerocket, Nil];

[Self.view.layer Addsublayer:emitterla];

Code style for Swift:

Self.view.backgroundColor = Uicolor.blackcolor ();

Loading a grainy spark picture
Let Emitterla = Caemitterlayer ();
Emitterla.emitterposition = Cgpointmake (SELF.VIEW.BOUNDS.SIZE.WIDTH/2, self.view.bounds.size.height*0.75);
Emitterla.rendermode = kcaemitterlayeradditive;

Gradually hide the emission particles before the explosion
Let Emittercerocket = Caemittercell ();
Emittercerocket.emissionlongitude = CGFloat (-m_pi_2);
Emittercerocket.emissionlatitude = 0;
Emittercerocket.lifetime = 1.6;
Emittercerocket.birthrate = 1;
Emittercerocket.velocity = 400;
Emittercerocket.velocityrange = 100;
Emittercerocket.yacceleration = 250;
Emittercerocket.emissionrange = CGFloat (M_PI/4);
Emittercerocket.color = Cgcolorcreatecopy (Uicolor (red:0.5, green:0.5, blue:0.5, alpha:0.5). Cgcolor);
Emittercerocket.redrange = 0.5;
Emittercerocket.greenrange = 0.5;
Emittercerocket.bluerange = 0.5;
Set the path name for the animation effect
Emittercerocket.name = "Rocket";

Add Path state
Let Emittercefly = Caemittercell ();
Emittercefly.contents = UIImage (named: "Tspark.png")!. Cgimage;
Emittercefly.emissionlongitude = CGFloat ((4*M_PI)/2);
Emittercefly.scale = 0.4;
emittercefly.velocity = 100;
Emittercefly.birthrate = 45;
Emittercefly.lifetime = 1.5;
Emittercefly.yacceleration = 350;
Emittercefly.emissionrange = CGFloat (M_PI/7);
Emittercefly.alphaspeed =-0.7;
Emittercefly.scalespeed =-0.1;
Emittercefly.scalerange = 0.1;
Emittercefly.begintime = 0.01;
Emittercefly.duration = 0.7;

Set explosion
Let emittercefirework = Caemittercell ();
Emittercefirework.contents = UIImage (named: "Tspark.png")!. Cgimage;
Emittercefirework.birthrate = 9999;
Emittercefirework.scale = 0.6;
Emittercefirework.velocity = 130;
Emittercefirework.lifetime = 2;
Emittercefirework.alphaspeed =-0.2;
Emittercefirework.yacceleration = 80;
Emittercefirework.begintime = 1.5;
Emittercefirework.duration = 0.1;
Emittercefirework.emissionrange = CGFloat (2*M_PI);
Emittercefirework.scalespeed =-0.1;
Emittercefirework.spin = 2;
Set explosion animation name
Emittercefirework.name = "Firework";

Adding a repeating process
Let Emitterceprespark = Caemittercell ();
Emitterceprespark.birthrate = 80;
emitterceprespark.velocity = emittercefirework.velocity*0.7;
Emitterceprespark.lifetime = 1.7;
Emitterceprespark.yacceleration = emittercefirework.yacceleration*0.85;
Emitterceprespark.begintime = emittercefirework.begintime-0.2;
Emitterceprespark.emissionrange = Emittercefirework.emissionrange;
Emitterceprespark.greenspeed = 100;
Emitterceprespark.bluespeed = 100;
Emitterceprespark.redspeed = 100;
Set Duplicate animation name
Emitterceprespark.name = "Prespark";

The last flash of fireworks
Let Emittercesparkle = Caemittercell ();
Emittercesparkle.contents = UIImage (named: "Tspark.png")!. Cgimage;
Emittercesparkle.lifetime = 0.05;
Emittercesparkle.yacceleration = 250;
Emittercesparkle.begintime = 0.8;
Emittercesparkle.scale = 0.4;
Emittercesparkle.birthrate = 10;

Emitterceprespark.emittercells = Nsarray.init (Objects:emittercesparkle) as? [Caemittercell];
Emittercerocket.emittercells = Nsarray.init (Objects:emittercefly,emittercefirework,emitterceprespark) as? [Caemittercell];
Emitterla.emittercells = Nsarray.init (Objects:emittercerocket) as? [Caemittercell];

Self.view.layer.addSublayer (Emitterla);

:


Write a small project to launch fireworks using OC and Swift in two languages

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.