美文网首页iOS快速开发总结iOS DeveloperiOS开发常用知识点
iOS开发之映客直播倒计时3-2-1效果的实现

iOS开发之映客直播倒计时3-2-1效果的实现

作者: 键盘上的演绎者 | 来源:发表于2017-01-01 23:40 被阅读748次

岁末年初,直接看效果图吧

直播倒计时效果实现

其实稍微想想这个效果应该还是很容易实现的?
答案是肯定的

思路无非就是数字 -- 直到为 0
然后在显示的时候文字由大变小,并且透明度由1到0便可实现。

玩过的人都知道
文字缩放用: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))
//这里设置文字大小根据屏幕大小不一样而不一样
#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

不是无情,亦非薄幸,只是我们一生中会遇到很多人,真正能停留驻足的又有几个?生命是终将荒芜的渡口,连我们自己都是过客。这个世界有两件事我们不能不做:一是赶路,二是停下来看看自己是否拥有一份好的心情。

相关文章

网友评论

  • 暖风惜人:测试的时候,有两个情况,一是倒计时内有闪屏的情况,二是有时候倒计时不显示。我想问一下这种情况是咋回事
  • 暖风惜人:怎么调用啊,这个
  • YoNotes:公司代码外网进进不去……改日把倒计时btn ,倒计时View 发到简书。。闲了再搞。。最近很忙哦
    键盘上的演绎者:@暖风惜人 直接调用就可以 亲测无问题的
    暖风惜人:你的这个倒计时view好了吗,
    键盘上的演绎者:@Slience520 可以的,最近大家都忙,年终了
  • YoNotes:你用GCD开倒计时。。爽多了。我是用GCD➕Xib。动画简单。。没什么复杂的。。我就搞不明白,为啥已经开了个直播,非要倒计时。真不知道咋想的。
    键盘上的演绎者:@万古书生 在技术层面,无非就是加载,数据传输等。不是这个,已经链接成功了,才倒计时。如果没有链接成功,可能导致,倒计时完成了,但是还没有开始直播。所有一开始进入页面的时候就已经是连接成功的状态了
    王bu留行: @SlienceTGo 自己直播有个倒计时在心理上会给人一种感觉,就好像是以前拍照之前我们都会倒数3 2 1,这是在用户心里层面。在技术层面,无非就是加载,数据传输等。
    键盘上的演绎者:@Slience520 GCD倒计时确实很爽,我也搞不懂为什么需要这个需求,直播都已经开了。也许是为了用户体验比较好吧。能否交流一下您的方法。GCD倒计时我也写过,封装过获取验证码倒计时,估计思路是一样样的

本文标题:iOS开发之映客直播倒计时3-2-1效果的实现

本文链接:https://www.haomeiwen.com/subject/gleevttx.html