新建一個 xcode 項目,然后在 ViewController.m 編寫代碼實現效果。
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. 設置CAEmitterLayer
self.emitterLayer = [CAEmitterLayer layer];
[self.view.layer addSublayer:self.emitterLayer];
//發射源的尺寸大小
self.emitterLayer.emitterSize = self.view.frame.size;
//發射源的形狀
self.emitterLayer.emitterShape = kCAEmitterLayerPoint;
//發射模式
self.emitterLayer.emitterMode = kCAEmitterLayerPoints;
//粒子發射形狀的中心點
self.emitterLayer.emitterPosition = CGPointMake(self.view.frame.size.width, 0);
// 2. 配置CAEmitterCell
CAEmitterCell * emitterCell = [CAEmitterCell emitterCell];
//粒子名稱
emitterCell.name = @"emitterCell";
//粒子產生率,默認為0
emitterCell.birthRate = 20.0f;
//粒子生命周期
emitterCell.lifetime = 10.0f;
//粒子速度,默認為0
emitterCell.velocity = 40.0f;
//粒子速度平均量
emitterCell.velocityRange = 100.0f;
//x,y,z方向上的加速度分量,三者默認都是0
emitterCell.yAcceleration = 15.0f;
//指定緯度,緯度角代表了在x-z軸平面坐標系中與x軸之間的夾角,默認0:
emitterCell.emissionLongitude = M_PI; //向左
//發射角度范圍,默認0,以錐形分布開的發射角度。角度用弧度制。粒子均勻分布在這個錐形范圍內;
emitterCell.emissionRange = M_PI_4; //圍繞X軸向左90度
//縮放比例, 默認是1
emitterCell.scale = 0.2f;
//縮放比例范圍,默認是0
emitterCell.scaleRange = 0.1f;
//在生命周期內的縮放速度,默認是0
emitterCell.scaleSpeed = 0.02f;
//粒子的內容,為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能改變的范圍,默認0
emitterCell.redRange = 1.0f;
emitterCell.greenRange = 1.0f;
emitterCell.alphaRange = 0.8f;
//粒子顏色red,green,blue,alpha在生命周期內的改變速度,默認都是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 - 移動發射源到某個點上
-(void)setBallInPosition:(CGPoint)point
{
//創建基礎動畫
CABasicAnimation * basicAnimation = [CABasicAnimation animationWithKeyPath:@"emitterCells.emitterCell.scale"];
//fromValue
basicAnimation.fromValue = @0.2f;
//toValue
basicAnimation.toValue = @0.5f;
//duration
basicAnimation.duration = 1.0f;
//線性起搏,使動畫在其持續時間內均勻地發生
basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//用事務包裝隱式動畫
[CATransaction begin];
//設置是否禁止由于該事務組內的屬性更改而觸發的操作
[CATransaction setDisableActions:YES];
//為 emitterLayer 添加動畫
[self.emitterLayer addAnimation:basicAnimation forKey:nil];
//為 emitterLayer 指定位置添加動畫效果
[self.emitterLayer setValue:[NSValue valueWithCGPoint:point] forKey:@"emitterPosition"];
//提交動畫
[CATransaction commit];
}
@end
運行效果如下:
粒子.png