iOS 動畫 —— 點贊

一個點贊放大的效果,并且有爆炸粒子外射的感覺

點贊 Button

此處分為三步實現(xiàn)

  • 爆炸性動畫
  • button 的放大放小
  • 具體使用(需要自己配上三張圖片)

下面代碼具體實現(xiàn):

#import <UIKit/UIKit.h>

@interface FireworksView : UIView

@property (nonatomic, strong) UIImage *particleImage;
@property (nonatomic, assign) CGFloat particleScale;
@property (nonatomic, assign) CGFloat particleScaleRange;

- (void)animate;

@end
#import "FireworksView.h"

@interface FireworksView ()

@property (nonatomic, strong) CAEmitterLayer *explosionLayer;
@property (nonatomic, strong) CAEmitterCell *explosionCell;

@end

@implementation FireworksView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    self.clipsToBounds = NO;
    self.userInteractionEnabled = NO;
    /**
     *  爆炸效果
     */
    _explosionCell = [CAEmitterCell emitterCell];
    _explosionCell.name = @"explosion";
    _explosionCell.alphaRange = 0.2f; // 透明度改變的范圍
    _explosionCell.alphaSpeed = -1.0f; // 透明度改變的速度
    _explosionCell.lifetime = 0.7f;
    _explosionCell.lifetimeRange = 0.3f;
    _explosionCell.birthRate = 0.f; // 粒子產(chǎn)生系數(shù)
    _explosionCell.velocity = 40.0f;
    _explosionCell.velocityRange = 10.0f;
    
    _explosionLayer = [CAEmitterLayer layer];
    _explosionLayer.name = @"emitterLayer";
    _explosionLayer.emitterShape = kCAEmitterLayerCircle; // 發(fā)射形狀
    _explosionLayer.emitterMode = kCAEmitterLayerOutline;
    _explosionLayer.emitterSize = CGSizeMake(25.f, 0.f);
    _explosionLayer.emitterCells = @[_explosionCell];
    _explosionLayer.renderMode = kCAEmitterLayerOldestFirst; // 渲染模式
    _explosionLayer.masksToBounds = NO;
    [self.layer addSublayer:_explosionLayer];

}

//布局
- (void)layoutSubviews {
    [super layoutSubviews];
    // 設(shè)置位置
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    self.explosionLayer.emitterPosition = center;
}

#pragma mark - Animate Methods
- (void)animate {
    dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC);
    dispatch_after(delay, dispatch_get_main_queue(), ^{
        self.explosionLayer.beginTime = CACurrentMediaTime();
        //爆炸效果
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"emitterCells.explosion.birthRate"];
        animation.fromValue = @0;
        animation.toValue = @500;
        [self.explosionLayer addAnimation: animation forKey: nil];
    });
}

#pragma mark - Properties Method
- (void)setParticleImage:(UIImage *)particleImage {
    _particleImage = particleImage;
    self.explosionCell.contents = (id)[particleImage CGImage];
}

- (void)setParticleScale:(CGFloat)particleScale {
    _particleScale = particleScale;
    self.explosionCell.scale = particleScale;
}

- (void)setParticleScaleRange:(CGFloat)particleScaleRange {
    _particleScaleRange = particleScaleRange;
    self.explosionCell.scaleRange = particleScaleRange;

}

@end
@interface PraiseButton : UIButton

@property (nonatomic, strong) UIImage *particleImage;
@property (nonatomic, assign) CGFloat particleScale;
@property (nonatomic, assign) CGFloat particleScaleRange;

- (void)animate;
- (void)popOutsideWithDuration:(NSTimeInterval)duration;
- (void)popInsideWithDuration:(NSTimeInterval)duration;

@end
#import "PraiseButton.h"
#import "FireworksView.h"

@interface PraiseButton()

@property (nonatomic, strong) FireworksView *fireworksView;

@end

@implementation PraiseButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.clipsToBounds = NO;
    _fireworksView = [[FireworksView alloc] init];
    [self insertSubview:_fireworksView atIndex:0];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.fireworksView.frame = self.bounds;
    [self insertSubview:self.fireworksView atIndex:0];
}

#pragma mark - Methods
- (void)animate {
    [self.fireworksView animate];
}

//彈出
- (void)popOutsideWithDuration:(NSTimeInterval)duration {

    self.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 3.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(2.0f, 2.0f); // 放大
        }];
        [UIView addKeyframeWithRelativeStartTime:1/3.0 relativeDuration:1/3.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(0.8f, 0.8f); // 放小
        }];
        [UIView addKeyframeWithRelativeStartTime:2/3.0 relativeDuration:1/3.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //恢復(fù)原樣
        }];
    } completion:nil];
}
//彈進(jìn)
- (void)popInsideWithDuration:(NSTimeInterval)duration {

    self.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 2.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(0.7f, 0.7f); // 放小
        }];
        [UIView addKeyframeWithRelativeStartTime:1/2.0 relativeDuration:1/2.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //恢復(fù)原樣
        }];
    } completion:nil];
}

#pragma mark - Properties
//獲取粒子圖像
- (UIImage *)particleImage {
    return self.fireworksView.particleImage;
}
//設(shè)置粒子圖像
- (void)setParticleImage:(UIImage *)particleImage {
    self.fireworksView.particleImage = particleImage;
}
//獲取縮放
- (CGFloat)particleScale {
    return self.fireworksView.particleScale;
}
//設(shè)置縮放
- (void)setParticleScale:(CGFloat)particleScale {
    self.fireworksView.particleScale = particleScale;
}
//獲取縮放范圍
- (CGFloat)particleScaleRange {
    return self.fireworksView.particleScaleRange;
}
//設(shè)置縮放范圍
- (void)setParticleScaleRange:(CGFloat)particleScaleRange {
    self.fireworksView.particleScaleRange = particleScaleRange;
}

@end

#import "ViewController.h"
#import "PraiseButton.h"

@interface ViewController ()

@property (nonatomic, strong) PraiseButton *praiseButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.praiseButton.center = self.view.center;
    [self.view addSubview:self.praiseButton];
}

- (void)praiseAction:(PraiseButton *)button {
    if (button.selected) {
        [button popInsideWithDuration:0.4f];
    }
    else {
        [button popOutsideWithDuration:0.5f];
        [button animate];
    }
    button.selected = !button.selected;
}

- (PraiseButton *)praiseButton {
    if (!_praiseButton) {
        _praiseButton = [[PraiseButton alloc] init];
        _praiseButton.frame = CGRectMake(0.f, 0.f, 60.f, 60.f);
        _praiseButton.particleImage = [UIImage imageNamed:@"blue_circle"];
        _praiseButton.particleScale = 0.05f;
        _praiseButton.particleScaleRange = 0.02f;
        [_praiseButton addTarget:self action:@selector(praiseAction:) forControlEvents:UIControlEventTouchUpInside];
        [_praiseButton setImage:[UIImage imageNamed:@"gray_like"] forState:UIControlStateNormal];
        [_praiseButton setImage:[UIImage imageNamed:@"blue_like"] forState:UIControlStateSelected];
    }
    return _praiseButton;
}

@end

注意點1: CAEmitterLayer 中 Key 的獲取

[self.explosionLayer setValue:@0 forKeyPath:@"emitterCells.explosion.birthRate"];

一般是由 “emitterCells” + Cell.name + 屬性構(gòu)成

注意點2:CGAffineTransformMakeScale

//位移仿射  ---- 理解為平移 (CGFloat tx,CGFloat ty) 
CGAffineTransformMakeTranslation 
CGAffineTransformTranslate 
//旋轉(zhuǎn)仿射 ---- 理解為旋轉(zhuǎn) (CGFloat angle)
CGAffineTransformMakeRotation
CGAffineTransformRotate 
//縮放仿射  --- 理解縮放大小 (CGFloat sx, CGFloat sy)
CGAffineTransformMakeScale 
CGAffineTransformScale 

我們常用的就是 位置铺浇、縮放逛漫、旋轉(zhuǎn)這三個啦,深入以及其他的可以看看iOS動畫和特效(七)仿射變換-CGAffineTransform锤灿。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末挽拔,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子但校,更是在濱河造成了極大的恐慌螃诅,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件状囱,死亡現(xiàn)場離奇詭異术裸,居然都是意外死亡,警方通過查閱死者的電腦和手機亭枷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門袭艺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人叨粘,你說我怎么就攤上這事猾编。” “怎么了升敲?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵答倡,是天一觀的道長。 經(jīng)常有香客問我驴党,道長瘪撇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任鼻弧,我火速辦了婚禮设江,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘攘轩。我一直安慰自己叉存,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布度帮。 她就那樣靜靜地躺著歼捏,像睡著了一般稿存。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瞳秽,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天瓣履,我揣著相機與錄音,去河邊找鬼练俐。 笑死袖迎,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的腺晾。 我是一名探鬼主播燕锥,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼悯蝉!你這毒婦竟也來了归形?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤鼻由,失蹤者是張志新(化名)和其女友劉穎暇榴,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蕉世,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡蔼紧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了讨彼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片歉井。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖哈误,靈堂內(nèi)的尸體忽然破棺而出哩至,到底是詐尸還是另有隱情,我是刑警寧澤蜜自,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布菩貌,位于F島的核電站,受9級特大地震影響重荠,放射性物質(zhì)發(fā)生泄漏箭阶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一戈鲁、第九天 我趴在偏房一處隱蔽的房頂上張望仇参。 院中可真熱鬧,春花似錦婆殿、人聲如沸诈乒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怕磨。三九已至喂饥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肠鲫,已是汗流浹背员帮。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留导饲,地道東北人捞高。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像帜消,于是被迫代替她去往敵國和親棠枉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容