读秒倒计时

前些日子,项目中用到一个倒计时读秒的功能。其实实现起来很简单,就是通过改变 label 的transform。经过封装一句代码就可以引入项目中。

/**
 @param number       倒计时起始时间
 @param endTitle     倒计时结束字符串
 @param beginCount   倒计时开始回调
 @param finishCount  倒计时结束回调
 */
[GGCountDownLabel gg_countDownWithBeginNumber:5 endTitle:@"第一个控制器" begin:nil finished:nil];

Demo下载地址:https://github.com/Gunial/GGCountDownLabel

先看下效果:


读秒倒计时.gif

因为代码很简单,所以就直接贴出来了
.h 文件中的代码

#import <UIKit/UIKit.h>

typedef void(^countDownBeginCallBack)();
typedef void(^countDownFinishedCallBack)();

@interface GGCountDownLabel : UILabel

/**
 开始倒计时

 @param number   倒计时起始时间
 @param endTitle 倒计时结束字符串
 @param beginCount    倒计时开始回调
 @param finishCount 倒计时结束回调
 */
+ (instancetype)gg_countDownWithBeginNumber:(NSInteger)number
                                   endTitle:(NSString *)endTitle
                                      begin:(countDownBeginCallBack)beginCount
                                   finished:(countDownFinishedCallBack)finishCount;

/**
 隐藏
 */
+ (void)gg_hidden;

@end

.m文件中的代码

#import "GGCountDownLabel.h"
#import "AppDelegate.h"

#define GGScreenWith [UIScreen mainScreen].bounds.size.width
#define GGScreenHeight [UIScreen mainScreen].bounds.size.height
#define GGAppDelegate ((AppDelegate *)([UIApplication sharedApplication].delegate))

@interface GGCountDownLabel ()

@property (nonatomic, assign) NSInteger number;
@property (nonatomic, copy) NSString *endTitle;
@property (nonatomic, copy) countDownBeginCallBack beginCount;
@property (nonatomic, copy) countDownFinishedCallBack finishCount;

@end

@implementation GGCountDownLabel
BOOL isAnimating;

/** 单例初始化 */
+ (instancetype)shareCountDownLabel
{
    static GGCountDownLabel *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[GGCountDownLabel alloc] init];
    });
    return _instance;
}

+ (instancetype)gg_countDownWithBeginNumber:(NSInteger)number endTitle:(NSString *)endTitle begin:(countDownBeginCallBack)beginCount finished:(countDownFinishedCallBack)finishCount
{
    GGCountDownLabel *countDownLabel = [GGCountDownLabel shareCountDownLabel];
    
    //** 防止重叠动画 */
    if (isAnimating) return nil;
    
    //默认倒计时为三秒
    countDownLabel.number = 3;
    
    if (number && number > 0) countDownLabel.number = number;
    if (endTitle && endTitle.length > 0) countDownLabel.endTitle = endTitle;
    if (beginCount) countDownLabel.beginCount = beginCount;
    if (finishCount) countDownLabel.finishCount = finishCount;
    
    [self gg_configLabel:countDownLabel];
    [self gg_scaleActionWithLabel:countDownLabel beginBlock:beginCount finishedBlock:finishCount];
    
    return countDownLabel;
}

/** 配置 label 的基本属性 */
+ (void)gg_configLabel:(GGCountDownLabel *)label
{
    label.hidden = NO;
    label.frame = CGRectMake(0, 0, GGScreenWith, GGScreenHeight/2);
    label.center = CGPointMake(GGScreenWith/2, GGScreenHeight/2);
    label.transform = CGAffineTransformScale(label.transform, 15, 15);
    label.alpha = 0;
    label.text = [NSString stringWithFormat:@"%zd",label.number];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    
    [[label gg_getCurrentSuperView] addSubview:label];
}

// 获取当前显示的控制器的View
- (UIView *)gg_getCurrentSuperView
{
    return [self gg_getVisibleViewControllerFrom:(UIViewController *)GGAppDelegate.window.rootViewController].view;
}

- (UIViewController *)gg_getVisibleViewControllerFrom:(UIViewController*)vc
{
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self gg_getVisibleViewControllerFrom:[((UINavigationController*) vc) visibleViewController]];
    }else if ([vc isKindOfClass:[UITabBarController class]]){
        return [self gg_getVisibleViewControllerFrom:[((UITabBarController*) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self gg_getVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}

/** 倒计时动画 */
+ (void)gg_scaleActionWithLabel:(GGCountDownLabel *)label beginBlock:(countDownBeginCallBack)beginCount finishedBlock:(countDownFinishedCallBack)finishCount
{
    if (!isAnimating) {
        if (label.beginCount) {
            label.beginCount();
        }
    }
    
    if (label.number >= (label.endTitle.length ? 0 : 1 )) {
        isAnimating = YES;
        label.text = label.number == 0 ? label.endTitle : [NSString stringWithFormat:@"%zd",label.number];

        [UIView animateWithDuration:1.0 animations:^{
            label.alpha = 1;
            label.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            if (finished) {
                label.number --;
                label.alpha = 0;
                label.transform = CGAffineTransformScale(label.transform, 15, 15);
                [self gg_scaleActionWithLabel:label beginBlock:beginCount finishedBlock:finishCount];
            }
        }];
    }else {
        if (label.finishCount) label.finishCount();
        [self gg_hidden];
    }
}

/** 隐藏 */
+ (void)gg_hidden
{
    isAnimating = NO;
    [GGCountDownLabel shareCountDownLabel].transform = CGAffineTransformIdentity;
    [GGCountDownLabel shareCountDownLabel].hidden = YES;
    [[GGCountDownLabel shareCountDownLabel] removeFromSuperview];
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,208评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 脚本来源:邹肉肉 主人公:婷、辣辣、邹肉肉 在摇摇晃晃的火车上。 睡醒了一觉,醒来觉得无事可干,长达二十几个小时的...
    西柚和柠檬阅读 222评论 0 2
  • 看完了蜗居,想了很多,宋思明喜欢海藻,可是不爱她,如果爱她,不会继续经营20年的局,而把自己卷进去,也伤害到海藻和...
    林馨儿_d1d3阅读 155评论 0 0