原文地址:http://www.raywenderlich.com/6063/uikit-particle-systems-in-ios-5-tutorial?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+RayWenderlich+(Ray+Wenderlich+|+iPhone+Developer+and+Gamer)

You’ve probably seen particle systems used in many different iOS apps and games for explosions, fire effects, rain or snow falling, and more. However, you probably saw these types of effects most often in games, because UIKit didn’t provide a built-in capability to create particle systems – until iOS 5,that is!

Now with iOS 5, you can use particle systems directly in UIKit to bring a lot of exciting new eye-candy to your apps. Here are a few examples of where particle systems could be useful:

  • UIKit games: Yes, you can make games with plain UIKit (and some types of games work really well there, particularly card games and the like). But now, you can make them even better with explosions, smoke, and other goodies!
  • Slick UI effects: When your user moves around an object on the screen it can leave a trail of smoke, why not?
  • Stunning screen transitions: How about presenting the next screen in your app while the previous one disappears in a ball of fire?

Hopefully you’ve got some cool ideas of what you might want to use UIKit particle systems for. So let’s go ahead and try it out!

In this tutorial, we’re going to develop an app called “Draw with fire” that lets you (you guessed it) draw with fire on the screen.

I’ll take you along the process of creating the particle systems and having everything set on the screen, and you can develop the idea further into your own eye-candied drawing application. When the app is ready, you’ll be able to use it to draw a nice question mark of fire – like this one:

The New Particle APIs

The two classes you will need to use in order to create particle systems are located in the QuartzCore framework and are called CAEmitterLayer and CAEmitterCell.

The general idea is that you create a CAEmitterLayer, and add to it one or more CAEmitterCells. Each cell will then produce particles in the way it’s configured.

Also, since CAEmitterLayer inherits from CALayer, so you can easily inject it anywhere in your UIKit hierarchy!

I think the coolest thing about the new UIKit particle systems is that a single CAEmitterLayer can hold many CAEmitterCells. This allows you to achieve some really complicated and cool effects. For example, if you’re creating a fountain you can have one cell emitting the water and another emitting the vapor particles above the fountain!

Getting Started

Fire up Xcode (no pun intended) and from the main menu choose File\New\New Project. Select the iOS\Application\Single View Application template and click Next. Enter DrawWithFire for the product name, enter DWF for the class prefix, select iPhone for the Device Family, and make sure that “Use automatic reference counting” is checked (leave the other checkboxes unchecked). Click Next and save the project by clicking Create.

Select your project and select the DrawWithFire target. Open the Build Phases tab and open the Link Binary With Libraries fold. Click the plus button and double click QuartzCore.framework to add Quartz drawing capabilities to the project.

We’ll start the project by creating a custom UIView class which will have CAEmitterLayer as its layer. You can actually achieve this very very easy by overwriting the +(Class)layerClass method of the UIView class and returning a CAEmitter class. Pretty cool!

Create a new file with the iOS\Cocoa Touch\Objective-C class template, name the class DWFParticleView, and make it a subclass of UIView.

Open DWFParticleView.m and replace it with the following:

Let’s go over the initial code:

  • We create a single private instance variable to hold our CAEmitterLayer.
  • In awakeFromNib we set fireEmitter to be the view’s self.layer. We store it in the fireEmitter instance variable we created, because we’re going to set a lot of parameters on this later on.
  • +(Class)layerClass is the UIView method which tells UIKit which class to use for the root CALayer of the view. For more information on CALayers, check out the Introduction to CALayer Tutorial.

Next let’s our view controller’s root view to DWFParticleView. Open up DWFViewController.xib and perform the following steps:

  1. Make sure the Utilities bar is visible (the highlighted button on the image above should be pressed down).
  2. Select the gray area in the Interface builder – this is the view controller’s root view.
  3. Click the Identity Inspector tab
  4. In the Custom class panel enter DWFParticleView in the text field.

At this point we have the UI all set – good job! Let’s add some particles to the picture.

A Particle Examined

In order to emit fire, smoke, waterfalls and whatnot you’ll need a good PNG file to start with for your particles. You can make it yourself in any image editor program; have a look at the one I did for this tutorial (it’s zoomed in and on a dark background so you can actually see the shape):

My particle file is 32×32 pixels in size, it’s a transparent PNG file and I just used a little bit funkier brush to draw randomly with white color. For particles is best to use white color as the particle emitter will take care to tint the provided image in the colors we’d like to have. It’s also good idea to make particle image semi-transparent as the particle system can blend particles together by itself (you can figure out how it works by just trying out few different image files).

So, you can create a particle of your own or just use the one I made, but just be sure to add it to your Xcode project and have it named Particles_fire.png.

Let’s Start Emitting!

It’s time to add the code to make our CAEmitterLayer do its magic!

Open DWFParticleView.m, and add the following code at the end of awakeFromNib:

This sets the position of the emitter (in view local coordinates) and the size of the particles to spawn.

Next, add some more code to the bottom of awakeFromNib to add a CAEmitterCell to the CAEmitterLayer so we can finally see some particles on the screen!

We’re creating a cell instance and setting up few properties. Then we set the emitterCells property on the layer, which is just an NSArray of cells. The moment emitterCells is set, the layer starts to emit particles!

Next set set some properties on the CAEmitterCell. Let’s go over these one by one:

  • birthRate: The number of emitted particles per second. For a good fire or waterfall you need at least few hundred particles, so we set this to 200.
  • lifetime: The number of seconds before a particle should disappear. We set this to 3.0.
  • lifetimeRange: You can use this to vary the lifetime of particles a bit. The system will give each individual a random lifetime in the range (lifetime – lifetimeRange, lifetime + lifetimeRange). So in our case, a particle will live from 2.5-3.5 seconds.
  • color: The color tint to apply to the contents. We choose an orange color here.
  • contents: The contents to use for the cell, usually a CGImage. We set it to our particle image.
  • name: You can set a name for the cell in order to look it up and change its properties at a later point in time.

Run the app, and check out our new particle effect!

Well it works, but isn’t as cool as we might like. You might barely even be able to tell it’s doing something, it just looks like an orange splotch!

Let’s change this a bit to make the particle effect more dynamic. Add this code just before calling setName: on the cell:

Here we’re setting the following new properties on the CAEmitterCell:

  • velocity: The particles’s velocity in points per second. This will make our cell emit particles and send them towards the right edge of the screen
  • velocityRange: This is the range by which the velocity should vary, similar to lifetimeRange.
  • emissionRange: This is the angle range (in radians) in which the cell will emit. M_PI_2 is 45 degrees (and since this is the a range, it will be +/- 45 degrees).

Compile and run to check out the progress:

OK this is better – we’re not far from getting there! If you want to better understand how these properties affect the particle emitter – feel free to play and try tweaking the values and see the resulting particle systems.

Add two more lines to finish the cell configuration:

Here we set two more properties on the CAEmitterCell:

  • scaleSpeed: The rate of change per second at which the particle changes its scale. We set this to 0.3 to make the particles grow over time.
  • spin: Sets the rotation speed of each particle. We set this to 0.5 to give the particles a nice spin.

Hit Run one more time:

By now we have something like a rusty smoke – and guess what? CAEmitterCell has a lot more properties to tweak, so kind of the sky is the limit here. But we’re going to leave it like that and go on with setting some configuration on the CAEmitterLayer. Directly after setting fireEmitter.emitterSize in the code add this line:

This is the single line of code which turns our rusty smoke into a boiling ball of fire. Hit Run and check the result:

What’s happening? The additive render mode basically tells the system not to draw the particles one over each other as it normally does, but to do something really cool: if there are overlapping particle parts – their color intensity increases! So, in the area where the emitter is – you can see pretty much a boiling white mass, but at the outer ranges of the fire ball – where the particles are already dying and there’s less of them, the color tints to it’s original rusty color. Awesome!

Now you might think this fire is pretty unrealistic – indeed, you can have much better fire by playing with the cell’s properties, but we need such a thick one because we’re going to draw with it. When you drag your finger on the device’s screen there’s relatively few touch positions reported so we’ll use a thicker ball of fire to compensate for that.

1->雨的粒子效果    
ParticleRain *rain = [[ParticleRain alloc] init];
        rain.speed = 200;
        rain.speedVar = 30;
        rain.angle = -60;
        rain.angleVar = 10;
        rain.size = 5;
        rain.sizeVar = 1;
        rain.emissionRate = 120;
        rain.position = cpv(320, 160);
        rain.posVar = cpv(160 + 80, 0);
        [self addChild: rain z:10];

转载于:https://www.cnblogs.com/pengyingh/articles/2339466.html

UIKit Particle Systems in iOS 5 Tutorial ( 附雨的粒子效果 )相关推荐

  1. iOS之仿QQ点赞按钮粒子效果的实现

    效果展示 具体流程 一.封装YDWLikeButton 新建一个YDWLikeButton继承于UIButton,然后声明一个属性: @property (nonatomic, strong) CAE ...

  2. Unity【Project——beginner——Particle Systems(Shuriken)】

    长达3小时的project,遭重 unity个人笔记,资料来源官网 粒子系统听起来蛮有意思的,就是为什么会叫Shuriken(手里剑)?二次元的浓度提高了 这次视频没有cc字幕..听得懂一点算一点吧, ...

  3. Cesium中级教程8 - Introduction to Particle Systems 粒子系统入门

    Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ What is a particle system? 什么是粒子 ...

  4. 苹果+beta+软件测试计划,苹果发布 iOS 8.3 Beta 3,推出面向普通用户的 iOS Beta 计划附加入教程...

    原标题:苹果发布 iOS 8.3 Beta 3,推出面向普通用户的 iOS Beta 计划附加入教程 今天凌晨,苹果如期发布了iOS 8.3 第三个测试版,与此同时,还上线了新的Apple Beta ...

  5. Introduction to In-App Purchases in iOS 6 Tutorial

    From: http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial Note fro ...

  6. iOS动画开发之五——炫酷的粒子效果

    iOS动画开发之五--炫酷的粒子效果 在上几篇博客中,我们对UIView层的动画以及iOS的核心动画做了介绍,基本已经可以满足iOS应用项目中所有的动画需求,如果你觉得那些都还不够炫酷,亦或是你灵光一 ...

  7. iOS 封装跑马灯和轮播效果

    代码地址如下: http://www.demodashi.com/demo/14075.html 功能概述和预览 功能描述:WSL_RollView 是基于UICollectionView实现的支持水 ...

  8. 怎么在html中加入特效文字,如何使用HTML5+css3实现粒子效果文字动画特效(附完整代码)...

    我们在浏览web网页的时候会发现现在的网页做的越来越美观,很多动画特效做的越来越炫酷,这离不开HTML5和css3的深入开发.今天我们要来分享一款基于HTML5和css3的文字特效--粒子效果文字动画 ...

  9. html怎么把字做成动画效果,如何使用HTML5 css3实现粒子效果文字动画特效(附完整代码)...

    摘要 腾兴网为您分享:如何使用HTML5 css3实现粒子效果文字动画特效(附完整代码),学宝,小米社区,手机管家,神州专车等软件知识,以及小学英语点读机,便利宝,startos,工资宝,玩,大将军手 ...

最新文章

  1. 在CentOS 6.6 64bit上安装截图软件shutter
  2. Autojs自动化 实现自动删除公众号文章(通过订阅号助手删除)
  3. java多线程编程01---------基本概念
  4. Ubuntu14搭建配置青岛大学OJ系统
  5. 神经网络与机器学习 笔记—Rosenblatt感知机
  6. for in在python中什么意思_python for in中的in
  7. Android混淆模板与使用
  8. 2015年总结2016展望
  9. 他,先后担任4所大学校长!
  10. 【转载】特殊宏://{{AFX_MSG、//{{AFX_VIRTUAL、//{{AFX_MSG_MAP、//{{AFX_DATA_INIT
  11. phphstudy运行不了网站_传统企业网站运营分析:这些弊端你了解吗
  12. linux进程磁盘io监测,系统性能检测--磁盘io
  13. 阿里云 centos 安装图形化界面
  14. latex 图片和标题一起旋转
  15. mongodb 高可用分布式原理 ---------搭建高可用mongo集群前需要温习的知识-火
  16. 配置eclipse反编译
  17. 20212022最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)、前端面试题大全、前端进阶必知必会知识点1
  18. 基础数学4:导数、偏导数、方向导数、梯度、全微分回顾
  19. 光伏发电沦为白菜价 看光伏大佬们如何“割肉”
  20. FRI.Django 中的装饰器及 Auth 模块

热门文章

  1. python猴子吃桃子的问题_非人哉:明星带货却生意惨淡,猴哥心太大,这一帮猴子猴孙不省心...
  2. ios打不开html文件,在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接...
  3. cuda Synchronization
  4. linux chgrp
  5. Hadoop HIVE 创建表
  6. 1.4 满足和优化指标
  7. opencv 图像转换(傅里叶变换)
  8. openlayers属性数据mysql_OpenLayers学习笔记8——使用servlet从mysql获取数据并标注
  9. 虚拟化与私有云的区别
  10. Spring Boot学习总结(6)——SpringBoot解决ajax跨域请求问题的配置