新建一个 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
运行效果如下: