@interface ViewController ()
@property (nonatomic, strong) Person *person;
@property (strong, nonatomic) UIButton *likeBtn;
@property (nonatomic, strong) CAEmitterLayer *emitterLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self likeAnim];
}
- (void)likeAnim {
_likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_likeBtn setImage:[UIImage imageNamed:@"default@2x"] forState:UIControlStateNormal];
_likeBtn.frame = CGRectMake(100, 200, 37, 35);
[_likeBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_likeBtn];
[self explosion];
}
// CAEmitterLayer实现爆炸粒子
- (void)explosion{
_emitterLayer = [CAEmitterLayer layer];
CAEmitterCell *cell = [[CAEmitterCell alloc] init];
cell.name = @"explosionCell";
cell.lifetime = .7;
cell.birthRate = 4000;
cell.velocity = 50;
cell.velocityRange = 15;
cell.scale = .03;
cell.scaleRange = .02;
cell.contents = (id)[UIImage imageNamed:@"sparkle"].CGImage;
_emitterLayer.name = @"explosionLayer";
_emitterLayer.emitterShape = kCAEmitterLayerCircle;
_emitterLayer.emitterMode = kCAEmitterLayerOutline;
_emitterLayer.emitterSize = CGSizeMake(25, 0);
_emitterLayer.emitterCells = @[cell];
_emitterLayer.renderMode = kCAEmitterLayerOldestFirst;
_emitterLayer.masksToBounds = NO;
_emitterLayer.birthRate = 0;
_emitterLayer.zPosition = 0;
_emitterLayer.position = CGPointMake(CGRectGetWidth(_likeBtn.bounds)/2, CGRectGetHeight(_likeBtn.bounds)/2);
[_likeBtn.layer addSublayer:_emitterLayer];
}
- (void)clickAction:(UIButton *)sender {
sender.selected = !sender.selected;
//关键帧动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
if (sender.selected) {
anim.values = @[@1.5, @.8, @1, @1.2, @1];
anim.duration = .6;
// 调用
[self addExplosionAnim];
}else{
anim.values = @[@.8, @1.0];
anim.duration = .4;
}
[_likeBtn.layer addAnimation:anim forKey:nil];
}
- (void)addExplosionAnim{
_emitterLayer.beginTime = CACurrentMediaTime();
_emitterLayer.birthRate = 1;
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
weakSelf.emitterLayer.birthRate = 0;
});
}
iOS 实现点赞的动画
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- iOS动画系列,共十篇。现在写到第九篇啦。感兴趣的可以通过下面的传输门进到其他几篇文章里面。 ----------...
- 【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢...
- 1.使用到的类 2.直接上部分关键代码 代码中会有详细的注释 2.1 .m中需要拥有的属性 2.2 initWit...
- CAEmitterLayer 是一个高性能的粒子引擎,被用来创建复杂的粒子动画如:烟雾,火,雨等效果,并且很好地控...