IOS Fireworks scatter effect, layer gradient, layer reflection effect. Caemitterlayer particle Emitter

Source: Internet
Author: User

IOS Fireworks scatter effect, layer gradient, layer reflection effect. Caemitterlayer particle Emitter

In the previous section, I wrote a related class on the view layer, about the use of calayer and some usage, specifically here, we are working on the view of his layer to be modified, there are several points to note, as well as the introduction of the anchor Point, And then this one is mainly about using a class from Calayer to achieve a special effect on fireworks.

This is the use of caemitterlayer particle emitter layer to make an effect, this dynamic tool is recommended by the following friends, the effect is very good very good Mac version of the address, is a launch point to launch some particles (cell) effect.
This layer can be used to "launch" some cells, and can add different cells, set the direction, speed and so on, he is also a subclass inherited from the Calayer, are the layers of the view to do some processing, with animation effect, not much to say on the code.

 @interface ViewController () @property (nonatomic ,strong)CAEmitterLayer *emitterLayer; @end - (CAEmitterLayer *)emitterLayer{      if (_emitterLayer) {         return _emitterLayer;     }      _emitterLayer = [[CAEmitterLayer alloc]init];      [self.view.layer addSublayer:_emitterLayer]; //    _emitterLayer.position = CGPointMake(200, 200);     return _emitterLayer;  }

Initializes a particle emitter object that is added to the parent view.

 //使他的中心点为父视图的中心点,   self.emitterLayer.emitterPosition = self.view.center;   self.view.backgroundColor = [UIColor blackColor]; //先设置粒子发送器的属性 //设置粒子发送器每秒钟发送粒子数量self.emitterLayer.birthRate = 2;//设置粒子发送器的样式self.emitterLayer.renderMode = kCAEmitterLayerAdditive;  //初始化要发射的cell CAEmitterCell *cell = [CAEmitterCell emitterCell]; //contents:粒子的内容  cell.contents = (id)[UIImage imageNamed:@"星星"].CGImage;//他所需要对象类型的和图层类似 //接着设置cell的属性  //    粒子的出生量cell.birthRate = 2;  //    存活时间cell.lifetime = 3;cell.lifetimeRange = 1;   //    设置粒子发送速度   cell.velocity = 50;cell.velocityRange = 30;   //    粒子发送的方向cell.emissionLatitude = 90*M_PI/180;  //    发送粒子的加速度cell.yAcceleration = -100;  //    散发粒子的范围  ->  弧度cell.emissionRange = 180*M_PI/180;//最后把粒子的cell添加到粒子发送器  数组内可以添加多个cell对象self.emitterLayer.emitterCells = @[cell];

It automatically generates particles (layers) and then automatically adds effects to the properties we give it, and the emitted particles are not view, but just a layer above the layer.

And then introduce some of the properties of the emitter and the cell.

1. Particle transmitter Layer
-Caemitterlayer: Transmitter
-Number of particles sent per second: birthrate

  • Send shape's style: Emittershape
 -  CA_EXTERN NSString * const kCAEmitterLayerPoint  点 -  CA_EXTERN NSString * const kCAEmitterLayerLine   线 -  CA_EXTERN NSString * const kCAEmitterLayerRectangle  举行 -  CA_EXTERN NSString * const kCAEmitterLayerCuboid 立方体 -  CA_EXTERN NSString * const kCAEmitterLayerCircle 曲线 -  CA_EXTERN NSString * const kCAEmitterLayerSphere 圆形
  • Style Sent: Emittermode//120 line Start
-  CA_EXTERN NSString * const kCAEmitterLayerPoints以点的方式  默认样式-   CA_EXTERN NSString * const kCAEmitterLayerOutline线的样式-   CA_EXTERN NSString * const kCAEmitterLayerSurface  以面的形式-   CA_EXTERN NSString * const kCAEmitterLayerVolume    以团的样式
  • Pattern of particles appearing: rendermode//point go in line 129th start
    -   CA_EXTERN NSString * const kCAEmitterLayerOldestFirst最后一个出生的粒子在第一个    -   CA_EXTERN NSString * const kCAEmitterLayerOldestLast最后出生的就在最后一个    -   CA_EXTERN NSString * const kCAEmitterLayerBackToFront把后面的放到上面    -   CA_EXTERN NSString * const kCAEmitterLayerAdditive叠加效果
    • Adding particles to particle transmitters: emittercells

      Some properties of the cell

      • Represents the particle: Caemittercell He's not a layer.
      • Contents: The content of particles
      • Lifetime: Survival time
      • Lifetimerange: The range of survival time
      • Birthrate: The number of particles generated per second
      • Emissionlatitude: Emitted dimensions He represents a radian up and down
      • Emissionlongitude: Longitude, radian, and so forth
      • Velocity: The faster the transmit speed, the farther it is sent, the more power
      • Velocityrange: range of transmit speed
      • Xacceleration; Acceleration inertia dynamics of x, Y, Z axes
      • Yacceleration;
      • Zacceleration;
      • Emissionrange: Range, Radian, range, distribution
      • Name: Particle names can be found by name

And then we put it in a cell and set different properties.

 CAEmitterCell *cell2 = [CAEmitterCell emitterCell];     cell2.contents = (id)[UIImage imageNamed:@"点"].CGImage;//    粒子的出生量cell2.birthRate = 4;//    存活时间cell2.lifetime = 3;cell2.lifetimeRange = 1;//    设置粒子发送速度cell2.velocity = 80;cell2.velocityRange = 50;//    粒子发送的方向cell2.emissionLatitude = 90*M_PI/180;//    发送粒子的加速度cell2.yAcceleration = -100;//    散发粒子的范围  ->  弧度cell2.emissionRange = 180*M_PI/180;//把粒子的cell添加到粒子发送器//重新设置cell的emitterCellsself.emitterLayer.emitterCells = @[cell,cell2];

For the effect of me. The birth and initial speed of the Little red dot is set a bit larger, with the same acceleration, with the same range, the higher the initial speed, the greater the offset of the cell2. The rendering effect of the launcher can be a different way to try out the different styles.

Then there's the effect of a layer gradient

    • 2. Gradient Color
      Cagradientlayer

You can see the color of this layer is composed of 3 colors of the gradient, the more to the center, the heavier the color, the closer to the edge of the lighter color, as the background of the effect is very good, here is the calayer of a sub-class Cagradientlayer

  CAGradientLayer *layer = [CAGradientLayer layer];layer.frame = self.view.frame; //设置颜色数组layer.colors = @[(id)[UIColor colorWithRed:1.000 green:0.377 blue:0.624 alpha:1.000].CGColor,  (id)[UIColor colorWithRed:0.597 green:1.000 blue:0.657 alpha:1.000].CGColor,(id)[UIColor colorWithRed:0.649 green:0.729 blue:1.000 alpha:1.000].CGColor];

Here I set 3 colors, let it do a gradient effect in these three colors. All layer properties he can use.

 //    layer.opacity = 0.7;//设置透明度可以看到背景的图片,看上去就像一层滤镜 //渐变的起始点layer.startPoint = CGPointMake(0, 0); //终止点layer.endPoint = CGPointMake(1, 1); //渐变的区间      颜色所在的比例,第一个在十分之二的位置,第二个在十分之五的位置,第三个在十分之八的位置//    layer.locations = @[0.2,0.5,0.8];[self.view.layer addSublayer:layer];

When I set his starting point to the upper-left corner and the end point to the lower-right corner, he becomes such an effect. This effect as a processing layer effect is very good, there is equivalent to the effect of the filter, but there are still different, the filter is to add special effects to the picture, this is to add special effects to the layer, the filter content can point here

This class has only so many properties, it is relatively simple to use, the effect is very good.

In the next one the effect of a reflection, similar to the image of effects

    • 3. Duplicate Layer Careplicatorlayer

Copying a layer means making a copy of the layer and adding it to the effect, making some transform changes.

Duplicate layer important point, if you want to use a duplicate layer, you must override the view Layerclass to modify the original calayer to Careplicatorlayer

This means that a view that uses Careplicatorlayer must override its Layerclass method to use Careplicatorlayer to copy the layer.

First look at the effect

You can see that it is an image of the original layer, the color of what is relatively light some, of course, this is the image we give it (copy) some effect. Next see how to use.

    • 1. Layerclass method for overriding views
      ????????????
      First we create a new UIView class, rewrite his method, the first step is very important, if you do not override this method, the initialization of the view will not be effective.

First customize a class, first named Canreplicatorview, Inherit and UIView

Then rewrite his method, inside. m

  //只有这一步才能复制视图上面的layer   //要想赋值视图的图层就必须重写这个视图的layerClass   + (Class)layerClass{      return [CAReplicatorLayer class];  }
  • 2. Initialize the view

    Canreplicatorview *repview = [[Canreplicatorview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];

    Write a view that we want to display the picture to add up
    Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (0, 0, 200, 200)];
    Imageview.contentmode = Uiviewcontentmodescaleaspectfit;
    Imageview.image = [UIImage imagenamed:@ "dragon Slayer Knife"];

  • 3. Initializing a duplicate layer

    Careplicatorlayer layer = (Careplicatorlayer ) Repview.layer;

    Defining properties for a replication layer
    The number of instances of the InstanceCount layer must be explicitly duplicated
    Layer.instancecount = 2;//Write a 2 copy out of 1 copies, plus the original image of a total of 2 copies

  • 4. Set the transform value for the copy view

    Instancetransform represents the style of the copied layer transform is the style of the original view
    Define a transform value that lets the view pan down by 400
    Catransform3d transform = catransform3dmaketranslation (0, 400, 0);
    Let the view down 400 and rotate 180 degrees, into a reflection
    Layer.instancetransform = Catransform3drotate (Transform, 180*m_pi/180, 1, 0, 0);

    You can also set the color offset transparency, and so on
    Layer.instanceredoffset = -0.2;//Red offset
    Layer.instanceblueoffset = -0.2;//Blue
    Layer.instancegreenoffset = -0.2;//Green
    Layer.instancealphaoffset = -0.4;//Transparency Offset

  • 5. Add a View
    [Self.view Addsubview:repview];
    Add a view of the displayed picture to our view that can use a duplicate layer
    [Repview Addsubview:imageview];

With these three effects we can make the view have various effects, we change only the layer, not the properties of the view, add some special effects to their layer, when used to make the view more beautiful, but also some of the processing effect of iOS.

Particle emitter
The other two relatively simple is not released, in fact, this is also very simple (????)??

IOS Fireworks scatter effect, layer gradient, layer reflection effect. Caemitterlayer particle Emitter

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.