123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // FKProCountDownView.m
- // FirstLink
- //
- // Created by ascii on 2016/11/14.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKProCountDownView.h"
- @implementation FKTimeUnitView
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.layer.cornerRadius = 3;
- self.clipsToBounds = YES;
- self.backgroundColor = UIColorFromRGB(0xff2c6b);
-
- [self addSubview:self.timeLabel];
- [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self);
- }];
- }
- return self;
- }
- - (UILabel *)timeLabel {
- if (_timeLabel == nil) {
- _timeLabel = [[UILabel alloc]init];
- _timeLabel.textColor = UIColorFromRGB(0xffffff);
- _timeLabel.font = [UIFont systemFontOfSize:13];
- _timeLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _timeLabel;
- }
- @end
- @interface FKProCountDownView ()
- @property (nonatomic, strong) FKTimeUnitView *dayUnitView;
- @property (nonatomic, strong) FKTimeUnitView *hourUnitView;
- @property (nonatomic, strong) FKTimeUnitView *minuteUnitView;
- @property (nonatomic, strong) FKTimeUnitView *secondsUnitView;
- @property (nonatomic, assign) NSTimeInterval totalTime;
- @property (nonatomic, assign) NSTimeInterval elapseTime;
- @property (nonatomic, strong) NSTimer *timer;
- @end
- @implementation FKProCountDownView
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.dayUnitView = [self makeTimeUnitView];
- self.hourUnitView = [self makeTimeUnitView];
- self.minuteUnitView = [self makeTimeUnitView];
- self.secondsUnitView = [self makeTimeUnitView];
-
- self.backgroundColor = UIColorFromRGB(0xffeae9);
-
- [self addAllSubviews];
- }
- return self;
- }
- - (void)dealloc {
- [self.timer invalidate];
- self.timer = nil;
- }
- - (void)startWithTimeInterval:(NSTimeInterval)ti {
- if (ti != self.totalTime) {
- [self.timer invalidate];
- self.timer = nil;
-
- self.totalTime = ti;
- self.elapseTime = 0;
-
- [self refreshTimeWithTimeInterval:ti];
- self.timer = [NSTimer timerWithTimeInterval:1.0
- target:self
- selector:@selector(timeCountDownCallback)
- userInfo:nil
- repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
- }
- }
- - (void)timeCountDownCallback {
- self.elapseTime = (self.elapseTime + 1);
-
- NSTimeInterval remainTime = (self.totalTime - self.elapseTime);
- if (remainTime <= 0) {
- [self.timer invalidate];
- self.timer = nil;
-
- [[NSNotificationCenter defaultCenter] postNotificationName:PRODUCT_DETAIL_COUNT_TIME_ZERO
- object:nil];
- }
- [self refreshTimeWithTimeInterval:MAX(0, remainTime)];
- }
- - (void)refreshTimeWithTimeInterval:(NSTimeInterval)ti {
- NSDateComponents *componets = [FLStringHelper convertSecondToComponents:ti];
-
- self.dayUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.day];
- self.hourUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.hour];
- self.minuteUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.minute];
- self.secondsUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.second];
- }
- #pragma mark - Layout
- - (void)addAllSubviews {
- [self addSubview:self.descLabel];
- [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self);
- make.bottom.equalTo(self.mas_centerY).offset(-5);
- }];
-
- UIView *centerDotView = [self makeDotView];
- [self addSubview:centerDotView];
- [centerDotView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self);
- make.top.equalTo(self.mas_centerY).offset(-3);
- make.size.mas_equalTo(CGSizeMake(8, 19));
- }];
-
- [self addSubview:self.hourUnitView];
- [self.hourUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(centerDotView).offset(1);
- make.right.equalTo(centerDotView.mas_left);
- make.size.mas_equalTo(CGSizeMake(19, 19));
- }];
-
- [self addSubview:self.minuteUnitView];
- [self.minuteUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(self.hourUnitView);
- make.left.equalTo(centerDotView.mas_right);
- make.size.mas_equalTo(CGSizeMake(19, 19));
- }];
-
- UIView *leftDotView = [self makeDotView];
- [self addSubview:leftDotView];
- [leftDotView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(centerDotView);
- make.right.equalTo(self.hourUnitView.mas_left);
- make.size.equalTo(centerDotView);
- }];
-
- [self addSubview:self.dayUnitView];
- [self.dayUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(self.hourUnitView);
- make.right.equalTo(leftDotView.mas_left);
- make.size.mas_equalTo(CGSizeMake(19, 19));
- }];
-
- UIView *rightDotView = [self makeDotView];
- [self addSubview:rightDotView];
- [rightDotView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(centerDotView);
- make.left.equalTo(self.minuteUnitView.mas_right);
- make.size.equalTo(centerDotView);
- }];
-
- [self addSubview:self.secondsUnitView];
- [self.secondsUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(self.hourUnitView);
- make.left.equalTo(rightDotView.mas_right);
- make.size.mas_equalTo(CGSizeMake(19, 19));
- }];
- }
- #pragma mark - Property
- - (UILabel *)descLabel {
- if (_descLabel == nil) {
- _descLabel = [[UILabel alloc]init];
- _descLabel.textColor = UIColorFromRGB(0xff2c6b);
- _descLabel.font = [UIFont systemFontOfSize:12];
- }
- return _descLabel;
- }
- - (FKTimeUnitView *)makeTimeUnitView {
- return [[FKTimeUnitView alloc] init];
- }
- - (UILabel *)makeDotView {
- UILabel *label = [UILabel new];
- label.textColor = UIColorFromRGB(0xff2c6b);
- label.font = [UIFont systemFontOfSize:13];
- label.textAlignment = NSTextAlignmentCenter;
- label.text = @":";
- return label;
- }
- @end
|