iOS使用核心动画和粒子发射器实现点赞按钮的方法使用核心动画和粒子发射器实现点赞按钮的方法
首先放上效果图,大家可以看一下
实现的方法如下
一、使用到的类一、使用到的类
CAKeyframeAnimation // 核心动画-关键帧动画
CAEmitterLayer // 粒子发射器(其实就是一个Layer,其父类是CALayer)
CAEmitterCell // 粒子
PS:核心动画应该不用多说了;
CAEmitterLayer和CAEmitterCell,其实可以比喻成“炮”和“炮弹”,应该不难理解;
二、直接上部分关键代码二、直接上部分关键代码 代码中会有详细的注释代码中会有详细的注释
2.1 .m中需要拥有的属性中需要拥有的属性
/** weak类型 粒子发射器 */
@property (nonatomic, weak) CAEmitterLayer *emitterLayer;
2.2 initWithFrame: 方法中方法中
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 配置粒子发射器方法
[self setupEmitter];
}
return self;
}
2.3 setSelected: 方法中方法中
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
// 开始关键帧动画
[self keyframeAnimation];
}
2.4 layoutSubviews 方法中方法中
- (void)layoutSubviews{
[super layoutSubviews];
/// 设置粒子发射器的锚点
_emitterLayer.position = self.imageView.center;
}
2.5 setupEmitter 方法中方法中( 配置粒子发射器方法配置粒子发射器方法 )
- (void)setup {
// 粒子使用CAEmitterCell初始化
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
// 粒子的名字,在设置喷射个数的时候会用到
emitterCell.name = @"emitterCell";
// 粒子的生命周期和生命周期范围
emitterCell.lifetime = 0.7;
emitterCell.lifetimeRange = 0.3;
// 粒子的发射速度和速度的范围
emitterCell.velocity = 30.00;
emitterCell.velocityRange = 4.00;
评论0
最新资源