岁末年初,直接看效果图吧
直播倒计时效果实现其实稍微想想这个效果应该还是很容易实现的?
答案是肯定的
思路无非就是数字 -- 直到为 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
不是无情,亦非薄幸,只是我们一生中会遇到很多人,真正能停留驻足的又有几个?生命是终将荒芜的渡口,连我们自己都是过客。这个世界有两件事我们不能不做:一是赶路,二是停下来看看自己是否拥有一份好的心情。
网友评论