iOS開發(fā)之映客直播倒計時3-2-1效果的實現(xiàn)

歲末年初,直接看效果圖吧

直播倒計時效果實現(xiàn)

其實稍微想想這個效果應該還是很容易實現(xiàn)的?
答案是肯定的

思路無非就是數(shù)字 -- 直到為 0
然后在顯示的時候文字由大變小,并且透明度由1到0便可實現(xiàn)。

玩過的人都知道
文字縮放用:CGAffineTransformScale
透明度用:alpha

下面直接上代碼:

//
//  BeginLiveCountDown.h
//  ZXL
//
//  Created by apple on 16/12/31.
//  Copyright ? 2016年 apple. All rights reserved.
//

#import <UIKit/UIKit.h>
@class BeginLiveCountDown;
typedef void(^CountdownBeginBlock)(BeginLiveCountDown *label);
typedef void(^CountdownEndBlock)(BeginLiveCountDown *label);

@interface BeginLiveCountDown : UILabel
+ (instancetype)playWithNumber:(NSInteger)number endTitle:(NSString *)endTitle begin:(CountdownBeginBlock)begin end:(CountdownEndBlock)end;
+ (void)hiddenCountDown;
@end
//
//  BeginLiveCountDown.m
//  ZXL
//
//  Created by apple on 16/12/31.
//  Copyright ? 2016年 apple. All rights reserved.
//

#import "BeginLiveCountDown.h"
#import "AppDelegate.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define AppDelegate ((AppDelegate *)([UIApplication sharedApplication].delegate))
//這里設置文字大小根據(jù)屏幕大小不一樣而不一樣
#define FontSize ScreenWidth/250
#define Font(size) [UIFont boldSystemFontOfSize:(size * FontSize)]

@interface BeginLiveCountDown ()
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, copy) NSString *endTitle;
@property (nonatomic, copy) CountdownBeginBlock beginBlock;
@property (nonatomic, copy) CountdownEndBlock endBlock;
@end

@implementation BeginLiveCountDown

static BOOL isAnimationing;

+ (instancetype)share {
    static BeginLiveCountDown *label = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        label = [[BeginLiveCountDown alloc] init];
        isAnimationing = NO;
    });
    return label;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        isAnimationing = NO;
    }
    return self;
}

+ (void)hiddenCountDown {
    isAnimationing = NO;
    //重置
    [BeginLiveCountDown share].transform = CGAffineTransformIdentity;
    [BeginLiveCountDown share].hidden = YES;
}

+ (instancetype)playWithNumber:(NSInteger)number endTitle:(NSString *)endTitle begin:(CountdownBeginBlock)begin end:(CountdownEndBlock)end {
 
    if (isAnimationing) return nil;
    BeginLiveCountDown *label = [BeginLiveCountDown share];
    label.hidden = NO;
    // 默認三秒
    label.number = 3;
    if (number && number > 0) label.number = number;
    if (endTitle) label.endTitle = endTitle;
    if (begin) label.beginBlock = begin;
    if (end) label.endBlock = end;
    [self createLblAbout:label];
    // 動畫倒計時
    [self scaleActionWithBeginBlock:begin andEndBlock:end label:label];
    return label;
}

+ (void)createLblAbout:(BeginLiveCountDown *)label
{
    label.frame = (CGRect){0, 0, 50, ScreenWidth};
    label.transform = CGAffineTransformScale(label.transform, 10, 10);
    label.alpha = 0;
    label.text = [NSString stringWithFormat:@"%zd", label.number];
    label.textColor = [UIColor whiteColor];
    label.font = Font(20.0f);
    [[label getCurrentView] addSubview:label];
    label.center = CGPointMake(ScreenWidth / 2, ScreenHeight / 2);
    label.textAlignment = NSTextAlignmentCenter;
}

+ (void)scaleActionWithBeginBlock:(CountdownBeginBlock)begin andEndBlock:(CountdownEndBlock)end label:(BeginLiveCountDown *)label {
    if (!isAnimationing) {
        if (begin) begin(label);
    }
    if (label.number >= (label.endTitle ? 0 : 1)) {
        isAnimationing = YES;
        label.text = label.number == 0 ? label.endTitle : [NSString stringWithFormat:@"%zd", label.number];
        [UIView animateWithDuration:1 animations:^{
            label.transform = CGAffineTransformIdentity;
            label.alpha = 1;
        } completion:^(BOOL finished) { 
            if (finished) {
                label.number--;
                label.alpha = 0;
                label.transform = CGAffineTransformScale(label.transform, 10, 10);
                [self scaleActionWithBeginBlock:begin andEndBlock:end label:label];
            }  
        }];
    } else {
        if (end) end(label);
        [self hiddenCountDown];
    }
}

//當前顯示的控制器的View
- (UIView *)getCurrentView {
    return [self getNowViewController:(UIViewController *)AppDelegate.window.rootViewController].view;
}

///獲取當前正在顯示的控制器
- (UIViewController *)getNowViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self getNowViewController:[((UINavigationController *) vc) visibleViewController]];
    }else if ([vc isKindOfClass:[UITabBarController class]]){
        return [self getNowViewController:[((UITabBarController *) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self getNowViewController:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}
@end

不是無情,亦非薄幸,只是我們一生中會遇到很多人,真正能停留駐足的又有幾個?生命是終將荒蕪的渡口,連我們自己都是過客。這個世界有兩件事我們不能不做:一是趕路,二是停下來看看自己是否擁有一份好的心情。

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

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

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