html读秒倒计时,读秒倒计时

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

/**

@param number 倒计时起始时间

@param endTitle 倒计时结束字符串

@param beginCount 倒计时开始回调

@param finishCount 倒计时结束回调

*/

[GGCountDownLabel gg_countDownWithBeginNumber:5 endTitle:@"第一个控制器" begin:nil finished:nil];

先看下效果:

c04742a227c6

读秒倒计时.gif

因为代码很简单,所以就直接贴出来了

.h 文件中的代码

#import

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值