码迷,mamicode.com
首页 > 编程语言 > 详细

使用OC和Swift两种语言写一个发射烟花的小项目

时间:2016-07-22 19:29:31      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

OC与Swift两种实现方式基本上区别不大,主要是在一些对象或方法的调用方式不同

OC代码样式:

self.view.backgroundColor = [UIColor blackColor];
    
    //加载颗粒状的火花图片
    CAEmitterLayer *emitterLa = [CAEmitterLayer layer];
    emitterLa.emitterPosition = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height*(.75));//*(0.75)
    emitterLa.renderMode = kCAEmitterLayerAdditive;
    
    //在爆炸前逐渐隐藏发射颗粒
    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;
    //设置动画效果的路径名称
    [emitterCeRocket setName:@"rocket"];
    
    //添加路径状态
    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;
    
    //设置爆炸
    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;
    //设置爆炸动画名称
    [emitterCeFirework setName:@"firework"];
    
    //添加重复过程
    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;
    //设置重复动画名称
    [emitterCePreSpark setName:@"preSpark"];
    
    //烟花最后的闪光
    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];

Swift的代码样式:

self.view.backgroundColor = UIColor.blackColor();
        
        //加载颗粒状的火花图片
        let emitterLa = CAEmitterLayer();
        emitterLa.emitterPosition = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height*0.75);
        emitterLa.renderMode = kCAEmitterLayerAdditive;
        
        //爆炸前逐渐隐藏发射颗粒
        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;
        //设置动画效果的路径名称
        emitterCeRocket.name = "rocket";
        
        //添加路径状态
        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;
        
        //设置爆炸
        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;
        //设置爆炸动画名称
        emitterCeFirework.name = "firework";
        
        //添加重复过程
        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;
        //设置重复动画名称
        emitterCePreSpark.name = "preSpark";
        
        //烟花最后的闪光
        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);

效果图:

技术分享                  技术分享

使用OC和Swift两种语言写一个发射烟花的小项目

标签:

原文地址:http://blog.csdn.net/hbblzjy/article/details/51997442

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!