iOS使用核心动画和粒子发射器实现点赞按钮的方法

2020-01-18 18:03:15王旭

注意:这里有一点需要详细解释一下, CAEmitterCell 的属性一般有两个参数:一个平均值和一个“Range”,比如:


// 粒子的生命周期和生命周期范围
 emitterCell.lifetime  = 0.7;
 emitterCell.lifetimeRange = 0.3;

这里苹果的官方文档是这样解释的:

每一个Layer都有它自己的随机数发生器,粒子的属性大部分都被定义为一个平均值和一个范围值,

如粒子的速度,这个属性的值分布的区间为:[ M - R / 2,M + R / 2 ]。

然后 这个公式里面

      M:均值(拿上面代码说就是 emitterCell.lifetime)

      R:范围值(mitterCell.lifetimeRange)

然后我们就可根据公式算出上面我设置的粒子的生命周期的范围是[0.7-0.3/2 , 0.7+0.3/2]

2.6 keyframeAnimation 方法中 (开始关键帧动画)


- (void)animation {
 // 创建关键帧动画 
 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
 if (self.selected) {
 animation.values = @[@1.5 ,@0.8, @1.0,@1.2,@1.0];
 animation.duration = 0.5;
 // 粒子发射器 发射
 [self startFire];
 }else
 {
 animation.values = @[@0.8, @1.0];
 animation.duration = 0.4;
 }
 // 动画模式
 animation.calculationMode = kCAAnimationCubic;
 [self.imageView.layer addAnimation:animation forKey:@"transform.scale"];
}

这段代码没什么说的,应该很容易理解。

2.7 startFire 方法中 (开炮)


- (void)startFire{
 // 每秒喷射的80个
 [self.emitterLayer setValue:@1000 forKeyPath:@"emitterCells.emitterCell.birthRate"];
 // 开始
 self.emitterLayer.beginTime = CACurrentMediaTime();
 // 执行停止
 [self performSelector:@selector(stopFire) withObject:nil afterDelay:0.1];

}

2.8 stopFire 方法中 (停火)


- (void)stopFire {
 //每秒喷射的个数0个 就意味着关闭了
 [self.emitterLayer setValue:@0 forKeyPath:@"emitterCells.emitterCell.birthRate"]; 
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。


注:相关教程知识阅读请移步到IOS开发频道。