[iOS]教你自定制含動(dòng)畫(huà)的TabBar

** 效果如下**
1.有圖片、有title、有動(dòng)畫(huà)

AnimationTabBar.gif

2.有圖片、無(wú)title、無(wú)動(dòng)畫(huà)

AnimationTabBar1.gif

usage:

** 1.初始化方法 **
+ (instancetype)initWithNormalImages:(NSArray *)normalImages selectedImages:(NSArray *)selectedImages titles:(NSArray *)titles height:(CGFloat)height;
*** 可以自定制tabbar高度、允許無(wú)文字、但是照片還是要傳的,如果有無(wú)圖片的需求,也可以自行改源碼 ***

**2. 可以自定義以下這些屬性,當(dāng)然你也可以不賦值,這些屬性都有默認(rèn)值 **

/** tabbar背景顏色*/
@property (nonatomic, strong) UIColor *backgroundColor;
/** tabbar背景圖片*/
@property (nonatomic, strong) UIImage *backgroundImage;
/** 文字未選中顏色*/
@property (nonatomic, strong) UIColor *itemNormalColor;
/** 文字選中顏色*/
@property (nonatomic, strong) UIColor *itemSelectedColor;
/** 文字字體*/
@property (nonatomic, strong) UIFont *itemFont;
/** 文字和圖片間距 */
@property (nonatomic, assign) CGFloat titleMargin;
/** tabbarDelegate*/
@property (nonatomic, weak) id <TabarDelegate>delegate;
/** 是否展示動(dòng)畫(huà)*/
@property (nonatomic, assign) BOOL animation;

**3. 每個(gè)按鈕都是個(gè)tabbarItem **
初始化:+ (instancetype)initWithImage:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title;

當(dāng)item 選中,并且設(shè)置顯示動(dòng)畫(huà):animation = YES時(shí),展示動(dòng)畫(huà)。
動(dòng)畫(huà)代碼:
/**
 *  配置WclEmitterButton
 */
- (void)setup {
    CAEmitterCell *explosionCell = [CAEmitterCell emitterCell];
    explosionCell.name           = @"explosion";
    explosionCell.alphaRange     = 0.10;
    explosionCell.alphaSpeed     = -1.0;
    explosionCell.lifetime       = 0.7;
    explosionCell.lifetimeRange  = 0.3;
    explosionCell.birthRate      = 0;
    explosionCell.velocity       = 40.00;
    explosionCell.velocityRange  = 10.00;
    explosionCell.scale          = 0.03;
    explosionCell.scaleRange     = 0.02;
    explosionCell.contents       = (id)[UIImage imageNamed:@"Sparkle"].CGImage;
    
    _explosionLayer               = [CAEmitterLayer layer];
    _explosionLayer.name          = @"emitterLayer";
    _explosionLayer.emitterShape  = kCAEmitterLayerCircle;
    _explosionLayer.emitterMode   = kCAEmitterLayerOutline;
    _explosionLayer.emitterSize   = CGSizeMake(10, 0);
    _explosionLayer.emitterCells  = @[explosionCell];
    _explosionLayer.renderMode    = kCAEmitterLayerOldestFirst;
    _explosionLayer.masksToBounds = NO;
    _explosionLayer.position      = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
    _explosionLayer.zPosition     = -1;
    [self.layer addSublayer:_explosionLayer];
    
    CAEmitterCell *chargeCell = [CAEmitterCell emitterCell];
    chargeCell.name           = @"charge";
    chargeCell.alphaRange     = 0.10;
    chargeCell.alphaSpeed     = -1.0;
    chargeCell.lifetime       = 0.3;
    chargeCell.lifetimeRange  = 0.1;
    chargeCell.birthRate      = 0;
    chargeCell.velocity       = -40.0;
    chargeCell.velocityRange  = 0.00;
    chargeCell.scale          = 0.03;
    chargeCell.scaleRange     = 0.02;
    chargeCell.contents       = (id)[UIImage imageNamed:@"Sparkle"].CGImage;
    
    _chargeLayer               = [CAEmitterLayer layer];
    _chargeLayer.name          = @"emitterLayer";
    _chargeLayer.emitterShape  = kCAEmitterLayerCircle;
    _chargeLayer.emitterMode   = kCAEmitterLayerOutline;
    _chargeLayer.emitterSize   = CGSizeMake(20, 0);
    _chargeLayer.emitterCells  = @[chargeCell];
    _chargeLayer.renderMode    = kCAEmitterLayerOldestFirst;
    _chargeLayer.masksToBounds = NO;
    _chargeLayer.position      = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
    _chargeLayer.zPosition     = -1;
    [self.layer addSublayer:_chargeLayer];
}

/**
 *  開(kāi)始動(dòng)畫(huà)
 */
- (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 startAnimate];
    }else
    {
        animation.values = @[@0.8, @1.0];
        animation.duration = 0.4;
    }
    animation.calculationMode = kCAAnimationCubic;
    [self.layer addAnimation:animation forKey:@"transform.scale"];
}

/**
 *  開(kāi)始噴射
 */
- (void)startAnimate {
    //chareLayer開(kāi)始時(shí)間
    self.chargeLayer.beginTime = CACurrentMediaTime();
    //chareLayer每秒噴射的80個(gè)
    [self.chargeLayer setValue:@80 forKeyPath:@"emitterCells.charge.birthRate"];
    //進(jìn)入下一個(gè)動(dòng)作
    [self performSelector:@selector(explode) withObject:nil afterDelay:0.2];
}

/**
 *  大量噴射
 */
- (void)explode {
    //讓chareLayer每秒噴射的個(gè)數(shù)為0個(gè)
    [self.chargeLayer setValue:@0 forKeyPath:@"emitterCells.charge.birthRate"];
    //explosionLayer開(kāi)始時(shí)間
    self.explosionLayer.beginTime = CACurrentMediaTime();
    //explosionLayer每秒噴射的2500個(gè)
    [self.explosionLayer setValue:@2500 forKeyPath:@"emitterCells.explosion.birthRate"];
    //停止噴射
    [self performSelector:@selector(stop) withObject:nil afterDelay:0.1];
}

/**
 *  停止噴射
 */
- (void)stop {
    //讓chareLayer每秒噴射的個(gè)數(shù)為0個(gè)
    [self.chargeLayer setValue:@0 forKeyPath:@"emitterCells.charge.birthRate"];
    //explosionLayer每秒噴射的0個(gè)
    [self.explosionLayer setValue:@0 forKeyPath:@"emitterCells.explosion.birthRate"];
}

詳情請(qǐng)去GitHub下載代碼吧!如果覺(jué)得有用可以star我呀~

AnimationTabBar

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容