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 the class Calayer and some use of methods, in detail here, we are working on the view of his layer to make changes, need to pay attention to several places, 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 an effect made using the Caemitterlayer particle emitter layer. This dynamic tool is also recommended by the following friends, the effect is very good very nice Mac version of the address, is a launch point to launch some particles (cell) effect.
The ability to "launch" some cells through this layer, and to be able to add different cells, set the direction, speed, etc., 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, which 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 will give us the initiative to generate particles (layers) and then through the properties we give to its own initiative to join the effect, the emitted particles are not a view, but just a layer above the display 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 starts
-  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 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; X. Acceleration inertia force of y,z axis
      • 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, in the same case of acceleration. Distribution range Likewise, the higher the initial speed, the greater the offset of the cell2. The rendering effect of the launcher can be a different way of trying different styles.

Then there's the effect of a layer gradient

    • 2. Gradient Color
      Cagradientlayer

You can see that the color of this layer is made up of 3 colors of the gradient color, the more to the center. The heavier the color, the lighter the closer the edge, the better the background. A subclass of Calayer is also used here 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 have set 3 colors. Let it do a gradient effect in these three colors.

All layer Properties He can use it.

 //    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, the end point is the lower-right corner. He has become such an effect.

This effect works well as a layer and has the same effect as a filter, except that the two are still different. Filters add effects to a picture, which adds effects to the layer. The content of the filter can point here

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

In the next say a reflection effect. Effects of similar images

    • 3. Duplicate Layer Careplicatorlayer

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

The important point of copying a layer. Suppose you want to use a duplicate layer, you must override the view Layerclass change the original calayer to Careplicatorlayer

That is, to use the Careplicatorlayer view, you must override its Layerclass method to copy the layer using Careplicatorlayer.

First look at the effect

Can see it is an image of the original layer, the color of what is relatively light some. This is, of course, some of the effects of mirroring (copying) that we give it. 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. Assuming that this method is not overridden, the initialization of the view will not be effective.

First you define a class, first named Canreplicatorview, inheritance and UIView

Then rewrite his method. Inside the. 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 join 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 understood to replicate several
    Layer.instancecount = 2;//Write a 2 copy out 1 copies, plus the original one has 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 cheap 400 and rotate 180 degrees, into a reflection-like
    Layer.instancetransform = Catransform3drotate (Transform, 180*m_pi/180, 1, 0, 0);

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

  • 5. Join the 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, the view has a variety of effects, and we're only changing the layers. Are not properties of the view, add some special effects to their layers, and make the view more beautiful when used. It is also some of the processing effect that comes with iOS.

Particle emitter
The other two are relatively simple, and in fact this is very easy (????)??

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.