A simple particle effect implementation in iOS

Source: Internet
Author: User

1. Display

2. Realization of Ideas

1> first to achieve the above effect, the first step to deal with is a simple artboard, on the view with the mouse to slide the time to draw lines, this function can be implemented using Uibezierpath

2> on the implementation of particle effects, you can create a calayer and then copy the layer with Careplicatorlayer to achieve particle effects

3. Code implementation

Encapsulation and writing of Drawview class

////drawview.m//06-Particle effect////Created by Xiaomage on 15/6/24.//Copyright (c) 2015 xiaomage. All rights reserved.//#import "DrawView.h"@interfaceDrawview () @property (nonatomic, strong) Uibezierpath*path; @property (nonatomic, weak) Calayer*Dotlayer, @property (nonatomic, weak) Careplicatorlayer*RepL;@end@implementationDrawview#pragmaMark-Lazy Load point Layer-(Calayer *) dotlayer{if(_dotlayer = =Nil) {        //Create a layerCalayer *layer =[Calayer layer]; CGFloat WH=Ten; Layer.frame= CGRectMake (0, - +, WH, WH); Layer.cornerradius= WH/2; Layer.backgroundcolor=[Uicolor Bluecolor].        Cgcolor;                [_repl Addsublayer:layer]; _dotlayer=layer; }    return_dotlayer;}-(Uibezierpath *) path{if(_path = =Nil) {_path=[Uibezierpath Bezierpath]; }        return_path;}#pragmaMark-Start tapping call-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{        //Get Touch ObjectUitouch *touch =[touches anyobject]; //get current Touch pointCgpoint curp =[Touch locationinview:self]; //set the starting point[Self.path movetopoint:curp]; }Static int_instanscount =0;- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{    //Get Touch ObjectUitouch *touch =[touches anyobject]; //get current Touch pointCgpoint curp =[Touch locationinview:self]; //add a line to a point[_path addlinetopoint:curp]; //Redraw[self setneedsdisplay]; _instanscount++; }//Only override Drawrect:if perform custom drawing.//An empty implementation adversely affects performance during animation.- (void) DrawRect: (cgrect) rect {//Drawing Code[_path stroke];}#pragmaMark-Start Animation-(void) startanim{//Create a frame animationCakeyframeanimation *anim =[cakeyframeanimation animation]; Anim.keypath=@"position"; Anim.path=_path.        Cgpath; Anim.duration=3; Anim.repeatcount=maxfloat;        [Self.dotlayer Addanimation:anim Forkey:nil]; //Note: If the copied sub-layer has animations, first add the animation in the copy//Copy Child layer_repl.instancecount =_instanscount; //Delay Layer Animation_repl.instancedelay =0.2; }#pragmaMark-After loading the xib call, create the replication Layer-(void) awakefromnib{//Creating a replication layerCareplicatorlayer *REPL =[Careplicatorlayer layer]; Repl.frame=Self.bounds;      [Self.layer ADDSUBLAYER:REPL]; _repl=RepL;}#pragmaMark-Redraw-(void) redraw{//Clear Drawing Information_path =Nil;        [Self setneedsdisplay]; //removes the parent control from the layer, and the copy layer is removed. [_dotlayer Removefromsuperlayer]; _dotlayer=Nil; //total number of cleared empty layers_instanscount =0; }@end
Use of Drawview
// Click Start Animation -(Ibaction) Startanim: (ID) sender {        *view = (Drawview *) Self.view;    [View Startanim];    } // Reset button -(ibaction) ReDraw: (ID) sender {        *view = (Drawview *) Self.view;    [View ReDraw];}

A simple particle effect implementation in iOS

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.