核心動畫 - 粒子效果

新建一個 xcode 項(xiàng)目,然后在 ViewController.m 編寫代碼實(shí)現(xiàn)效果。

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)CAEmitterLayer * emitterLayer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    // 1. 設(shè)置CAEmitterLayer
    self.emitterLayer = [CAEmitterLayer layer];
    [self.view.layer addSublayer:self.emitterLayer];
    
    //發(fā)射源的尺寸大小
    self.emitterLayer.emitterSize = self.view.frame.size;
    //發(fā)射源的形狀
    self.emitterLayer.emitterShape = kCAEmitterLayerPoint;
    //發(fā)射模式
    self.emitterLayer.emitterMode = kCAEmitterLayerPoints;
    //粒子發(fā)射形狀的中心點(diǎn)
    self.emitterLayer.emitterPosition = CGPointMake(self.view.frame.size.width, 0);
    
    // 2. 配置CAEmitterCell
    CAEmitterCell * emitterCell = [CAEmitterCell emitterCell];
    //粒子名稱
    emitterCell.name = @"emitterCell";
    //粒子產(chǎn)生率,默認(rèn)為0
    emitterCell.birthRate = 20.0f;
    //粒子生命周期
    emitterCell.lifetime = 10.0f;
    //粒子速度,默認(rèn)為0
    emitterCell.velocity = 40.0f;
    //粒子速度平均量
    emitterCell.velocityRange = 100.0f;
    //x,y,z方向上的加速度分量,三者默認(rèn)都是0
    emitterCell.yAcceleration = 15.0f;
    //指定緯度,緯度角代表了在x-z軸平面坐標(biāo)系中與x軸之間的夾角,默認(rèn)0:
    emitterCell.emissionLongitude = M_PI; //向左
    //發(fā)射角度范圍,默認(rèn)0,以錐形分布開的發(fā)射角度。角度用弧度制。粒子均勻分布在這個錐形范圍內(nèi);
    emitterCell.emissionRange = M_PI_4; //圍繞X軸向左90度
    //縮放比例, 默認(rèn)是1
    emitterCell.scale = 0.2f;
    //縮放比例范圍,默認(rèn)是0
    emitterCell.scaleRange = 0.1f;
    //在生命周期內(nèi)的縮放速度,默認(rèn)是0
    emitterCell.scaleSpeed = 0.02f;
    //粒子的內(nèi)容,為CGImageRef的對象
    emitterCell.contents = (id)[UIImage imageNamed:@"circle_white"].CGImage;
    //顏色
    emitterCell.color = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f].CGColor;
    //粒子顏色red,green,blue,alpha能改變的范圍,默認(rèn)0
    emitterCell.redRange = 1.0f;
    emitterCell.greenRange = 1.0f;
    emitterCell.alphaRange = 0.8f;
    //粒子顏色red,green,blue,alpha在生命周期內(nèi)的改變速度,默認(rèn)都是0
    emitterCell.blueSpeed = 1.0f;
    emitterCell.alphaSpeed = -0.1f;
    
    //添加
    self.emitterLayer.emitterCells = @[emitterCell];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint  point = [touch locationInView:self.view];
    [self setBallInPosition:point];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint  point = [touch locationInView:self.view];
    [self setBallInPosition:point];
}

#pragma mark - 移動發(fā)射源到某個點(diǎn)上
-(void)setBallInPosition:(CGPoint)point
{
    //創(chuàng)建基礎(chǔ)動畫
    CABasicAnimation * basicAnimation = [CABasicAnimation animationWithKeyPath:@"emitterCells.emitterCell.scale"];
    //fromValue
    basicAnimation.fromValue = @0.2f;
    //toValue
    basicAnimation.toValue = @0.5f;
    //duration
    basicAnimation.duration = 1.0f;
    //線性起搏,使動畫在其持續(xù)時間內(nèi)均勻地發(fā)生
    basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    //用事務(wù)包裝隱式動畫
    [CATransaction begin];
    //設(shè)置是否禁止由于該事務(wù)組內(nèi)的屬性更改而觸發(fā)的操作
    [CATransaction setDisableActions:YES];
    //為 emitterLayer 添加動畫
    [self.emitterLayer addAnimation:basicAnimation forKey:nil];
    //為 emitterLayer 指定位置添加動畫效果
    [self.emitterLayer setValue:[NSValue valueWithCGPoint:point] forKey:@"emitterPosition"];
    //提交動畫
    [CATransaction commit];
}

@end

運(yùn)行效果如下:

粒子.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容