最近项目中多个页面用到了 商品特价倒计时的功能  为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下:

定向价 限时特价 模型代码实现:

#pragma mark 商品定向价模型
@interface STGoodsOrientationPrice : STBaseModel /**定向价**/
@property (nonatomic, copy) NSString *price; /**定向价开始时间**/
@property (nonatomic, copy) NSString *started_at; /***定向价结束时间*/
@property (nonatomic, copy) NSString *expired_at; /**商品分润**/
@property (nonatomic, copy) NSString *rebate; /*** 特价提前曝光中需要的字段 推荐价*/
@property (nonatomic,copy)NSString * promotion_price; /** *特价提前曝光中需要的字段 原价*/
@property (nonatomic,copy)NSString * origin_price; /*** 特价提前曝光需要的背景图片字段*/
@property (nonatomic,copy)NSString * image; /***自定义属性 判断活动是否已经结束 在sku里面会用到*/
@property (nonatomic,assign,getter=isActivityEnd)BOOL activityEnd; @end

@implementation STGoodsOrientationPrice

@end

 

限时特价 view实现代码

#import <UIKit/UIKit.h>
/**
* 定向价格的view
*/ //定义活动进行中的回调
typedef void (^orientationPriceViewStartBlock)(); //定义活动结束的回调
typedef void (^orientationPriceViewEndBlock)(); @class STGoodsOrientationPrice; //定向价模型 @interface STOrientationPriceView : UIView /**定向价模型**/
@property(nonatomic,strong)STGoodsOrientationPrice *orientation; /**活动进行中的回调**/
@property(nonatomic,strong)orientationPriceViewStartBlock orientationPriceViewStart; /**活动结束的回调**/
@property(nonatomic,strong)orientationPriceViewEndBlock orientationPriceViewEndBlock; /***创建定时器*/
@property(nonatomic,strong)NSTimer *timer; /***创建定时器*/
@property(nonatomic,strong)NSTimer *timer1; /***是否需要右侧展示原价view*/
@property(nonatomic,assign)BOOL showOldPrice; /**重新开启定时器**/
- (void)startTimer; @end
#import "STOrientationPriceView.h"
#import "STGoodsOrientationPrice.h"
#import "NSDate+SY.h"
#define orientationItemW 18

@interface STOrientationPriceView()

/***  底色图*/
@property(nonatomic,strong)UIImageView *bgImageView; /*** 限时优惠label*/
@property(nonatomic,strong)UIButton *saleButton ; /* * 价格的图 */
@property(nonatomic,strong)UIImageView *moneyLabel; /** * 价格的label*/
@property(nonatomic,strong)UILabel *priceLabel; /** * 原价label*/
@property(nonatomic,strong)UILabel *oldPriceLabel; /** * 右侧时间提示label*/
@property(nonatomic,strong) UILabel*rightLabel; /*** 右侧时间提示label上面正式开始的label*/
@property(nonatomic,strong) UILabel*startLabel; /***活动结束的label*/
@property(nonatomic,strong) UILabel*endLabel; /*** 小时*/
@property(nonatomic,strong)UIButton *hourLabel; /*** 点 左 */
@property(nonatomic,strong)UIImageView *pointLeft; /*** 分钟*/
@property(nonatomic,strong)UIButton *minuteLabel; /*** 点 右 */
@property(nonatomic,strong)UIImageView *pointRight; /*** 秒*/
@property(nonatomic,strong)UIButton *secondLabel; @end @implementation STOrientationPriceView #pragma mark lazy
- (NSTimer *)timer
{
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(modifyDate) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
return _timer;
} #pragma mark lazy
- (NSTimer *)timer1
{
if (!_timer1) {
_timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(modifEndDate) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer1 forMode:NSRunLoopCommonModes];
}
return _timer1;
} #pragma mark /**重新开启定时器**/
- (void)startTimer
{
[self setOrientation:_orientation];
} - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUpView];
} return self;
} #pragma mark init
- (void)awakeFromNib
{
[super awakeFromNib]; [self setUpView]; } - (void)setUpView
{
self.userInteractionEnabled = NO; _bgImageView = [[UIImageView alloc] init];
[self addSubview:_bgImageView]; _saleButton = [[UIButton alloc] init];
_saleButton.titleLabel.font = [UIFont systemFontOfSize:];
[_saleButton setTitle:@"限时优惠" forState:UIControlStateNormal];
[self addSubview:_saleButton]; _moneyLabel = [[UIImageView alloc] init];
_moneyLabel.image = [UIImage imageNamed:@"goods_orientation_money"];
[self addSubview:_moneyLabel]; _priceLabel = [[UILabel alloc] init];
_priceLabel.text = @"0.00";
_priceLabel.font = [UIFont systemFontOfSize:];
_priceLabel.textColor = [UIColor whiteColor];
[self addSubview:_priceLabel]; _oldPriceLabel = [[UILabel alloc] init];
_oldPriceLabel.text = @"0.00";
_oldPriceLabel.hidden = YES;
_oldPriceLabel.font = [UIFont systemFontOfSize:];
_oldPriceLabel.textColor = [UIColor colorWithHexString:@"fbe47a"];
[self addSubview:_oldPriceLabel];
NSString *oldPrice = @"¥ 12345";
NSUInteger length = [oldPrice length];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(, length)];
[attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor colorWithHexString:@"fbe47a"] range:NSMakeRange(, length)];
[_oldPriceLabel setAttributedText:attri]; _rightLabel = [[UILabel alloc] init];
_rightLabel.font = [UIFont systemFontOfSize:];
_rightLabel.textColor = [UIColor colorWithHexString:@""];
[self addSubview:_rightLabel]; _startLabel = [[UILabel alloc] init];
_startLabel.font = [UIFont systemFontOfSize:];
_startLabel.textColor = [UIColor colorWithHexString:@""];
[self addSubview:_startLabel]; _endLabel = [[UILabel alloc] init];
_endLabel.font = [UIFont systemFontOfSize:];
_endLabel.textColor = [UIColor colorWithHexString:@"b3b3b3"];
[self addSubview:_endLabel]; _hourLabel = [[UIButton alloc] init];
_hourLabel.titleLabel.font = [UIFont systemFontOfSize:];
[_hourLabel setTitle:@"" forState:UIControlStateNormal];
[_hourLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_hourLabel setBackgroundImage:[UIImage imageNamed:@"goods_orientation_blackRectangle"] forState:UIControlStateNormal];
[self addSubview:_hourLabel]; _pointLeft = [[UIImageView alloc] init];
_pointLeft.image = [UIImage imageNamed:@"goods_orientation_point"];
[self addSubview:_pointLeft]; _minuteLabel = [[UIButton alloc] init];
_minuteLabel.titleLabel.font =[UIFont systemFontOfSize:];
[_minuteLabel setTitle:@"" forState:UIControlStateNormal];
[_minuteLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_minuteLabel setBackgroundImage:[UIImage imageNamed:@"goods_orientation_blackRectangle"] forState:UIControlStateNormal];
[self addSubview:_minuteLabel]; _pointRight = [[UIImageView alloc] init];
_pointRight.image = [UIImage imageNamed:@"goods_orientation_point"];
[self addSubview:_pointRight]; _secondLabel = [[UIButton alloc] init];
_secondLabel.titleLabel.font =[UIFont systemFontOfSize:];
[_secondLabel setTitle:@"" forState:UIControlStateNormal];
[_secondLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_secondLabel setBackgroundImage:[UIImage imageNamed:@"goods_orientation_blackRectangle"] forState:UIControlStateNormal];
[self addSubview:_secondLabel]; [self hideTime]; //布局
[self layout];
} #pragma mark layout
- (void)layout
{ CGFloat margin = ; [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.mas_width);
make.height.equalTo(self.mas_height);
make.left.equalTo(self.mas_left);
make.top.equalTo(self.mas_top);
}]; [_saleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(margin);
make.centerY.equalTo(self.mas_centerY);
make.width.mas_equalTo();
make.height.mas_equalTo();
}]; [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo();
make.height.mas_equalTo();
make.left.equalTo(_saleButton.mas_right).offset(margin);
make.centerY.equalTo(self.mas_centerY);
}]; CGFloat priceMargin = ;
[_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_moneyLabel.mas_right).offset(priceMargin);
make.centerY.equalTo(self.mas_centerY);
}]; CGFloat rigthMargin = ;
[_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).offset(-rigthMargin);
make.top.equalTo(self.mas_top).offset(); }]; [_startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).offset(-rigthMargin);
make.top.equalTo(_rightLabel.mas_bottom).offset();
}]; [_endLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.mas_right).offset(-rigthMargin);
make.centerY.equalTo(self.mas_centerY);
}]; CGFloat timeW = orientationItemW;
CGFloat timeMargin =;
CGFloat timeTopMargin = ;
[_secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).offset(-);
make.top.equalTo(_rightLabel.mas_bottom).offset(timeTopMargin);
make.width.mas_equalTo(timeW);
}]; [_pointRight mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo();
make.height.mas_equalTo();
make.right.equalTo(_secondLabel.mas_left).offset(-timeMargin);
make.centerY.equalTo(_secondLabel.mas_centerY);
}]; [_minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_pointRight.mas_left).offset(-timeMargin);
make.top.equalTo(_rightLabel.mas_bottom).offset(timeTopMargin);
make.width.mas_equalTo(timeW);
}]; [_pointLeft mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo();
make.height.mas_equalTo();
make.right.equalTo(_minuteLabel.mas_left).offset(-timeMargin);
make.centerY.equalTo(_minuteLabel.mas_centerY);
}]; [_hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_pointLeft.mas_left).offset(-timeMargin);
make.top.equalTo(_rightLabel.mas_bottom).offset(timeTopMargin);
make.width.mas_equalTo(timeW);
}]; [_oldPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_hourLabel.mas_left).offset(-);
make.centerY.equalTo(self.mas_centerY);
}]; } #pragma mark 是否需要展示原价 显示原价 说明是首页的卡片cell
- (void)setShowOldPrice:(BOOL)showOldPrice
{ _showOldPrice = showOldPrice; if (showOldPrice) { self.priceLabel.font = [UIFont boldSystemFontOfSize:]; if (IS_IPHONE_4_OR_LESS || IS_IPHONE_5) {
self.oldPriceLabel.hidden = YES;
}else{
self.oldPriceLabel.hidden = NO;
} [_saleButton setTitle:@"特价预告" forState:UIControlStateNormal]; }else{ self.oldPriceLabel.hidden = YES;
[_saleButton setTitle:@"限时优惠" forState:UIControlStateNormal]; }
} #pragma mark 设置数据 在这里判断当前定向价展示的状态
- (void)setOrientation:(STGoodsOrientationPrice *)orientation
{
_orientation = orientation; _priceLabel.text = orientation.price?orientation.price:@"0.00"; _rightLabel.text = orientation.started_at; //判断活动是否已经结束
if ([self validEndDate]) { //活动结束
[self setupOrientationEnd]; //活动已经结束
self.orientation.activityEnd = YES; return;
} //判断活动开始时间
[self validStartate]; } #pragma mark 判断活动结束时间
- (BOOL)validEndDate
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 活动结束时间
NSDate *create = [fmt dateFromString:_orientation.expired_at]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区 NSTimeInterval time = [zone secondsFromGMTForDate:create];//以秒为单位返回当前时间与系统格林尼治时间的差 NSDate *createDate = [create dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间 // 截止时间data格式
NSDate *expireDate = [fmt dateFromString:[fmt stringFromDate:createDate]];
// 当前时间data格式
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[self getCurrentDate]]];
// 当前日历
NSCalendar *calendar = [NSCalendar currentCalendar];
// 需要对比的时间数据
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
| NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate:nowDate toDate:expireDate options:]; if (dateCom.day>) { return NO; }else{ if (dateCom.month>) { return NO; }else{ if (dateCom.second>) {
return NO;
}else{ if (dateCom.minute>) { return NO; }else if (dateCom.second>){ return NO;
} return YES;
} } } return NO; } #pragma mark 判断活动开始时间差距
- (void)validStartate
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 活动开始时间
NSDate *create = [fmt dateFromString:_orientation.started_at]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区 NSTimeInterval time = [zone secondsFromGMTForDate:create];//以秒为单位返回当前时间与系统格林尼治时间的差 NSDate *createDate = [create dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间 //获取当前时间
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[self getCurrentDate]]]; // 当前日历
NSCalendar *calendar = [NSCalendar currentCalendar]; if (createDate.isThisYear) { // 今年 // 需要对比的时间数据
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
| NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate:nowDate toDate:createDate options:]; if (dateCom.day>) { [self setupOrientation12Out]; }else{ if (dateCom.month>) { [self setupOrientation12Out]; }else{ if (dateCom.hour>) { //12小时之外 [self setupOrientation12Out]; }else if (dateCom.hour<= && dateCom.hour>){ //12小时之内 if (dateCom.hour==) { if (dateCom.second>) {
[self setupOrientation12Out]; }else{ if (dateCom.minute>) {
[self setupOrientation12Out];
}else{ [self setupOrientation12In];
}
} }else{ [self setupOrientation12In]; } }else{ //小于0 表示活动已经开始 if (dateCom.minute<= && dateCom.second<=) { [self setupActiveStart]; }else{
[self setupOrientation12In];
} } } } }else{ [self setupOrientation12Out]; } } #pragma mark 设置活动开始信息
- (void)setupActiveStart
{
//设置不能添加到购物车
weakifySelf
if(self.orientationPriceViewStart){
weakSelf.orientationPriceViewStart();
}
[self setupOrientationStart];
} - (void)dealloc
{
[self.timer invalidate];
self.timer = nil;
} #pragma mark 设置12小时内开购的信息
- (void)setupOrientation12In
{
_endLabel.hidden = YES; if (self.showOldPrice) { _bgImageView.image = [UIImage imageNamed:@"homeRrientationGreenbg"]; }else{ _bgImageView.image = [UIImage imageNamed:@"goods_orientation_greenbg"]; }
_rightLabel.text = @"距离开始仅剩";
[_saleButton setTitleColor:[UIColor colorWithHexString:@"76a505"] forState:UIControlStateNormal];
[_saleButton setBackgroundImage:[UIImage imageNamed:@"goods_orientation_yellowRectang"] forState:UIControlStateNormal]; [self showTime]; //开启定时器
[self.timer fire]; } #pragma mark 获取当前的准确时间
- (NSDate*)getCurrentDate
{
NSDate *date = [NSDate date]; //获得时间对象 NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区 NSTimeInterval time = [zone secondsFromGMTForDate:date];//以秒为单位返回当前时间与系统格林尼治时间的差 return [date dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间
} #pragma mark 修改日期 每一秒掉用一次 修改开始时间
- (void)modifyDate
{ // 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 活动开始时间
NSDate *create = [fmt dateFromString:_orientation.started_at]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区 NSTimeInterval time = [zone secondsFromGMTForDate:create];//以秒为单位返回当前时间与系统格林尼治时间的差 NSDate *createDate = [create dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间 //获取当前时间
NSDate *nowDate = [self getCurrentDate]; //判断开始时间和 当前时间的差值
NSDateComponents *cmps = [nowDate deltaFrom:createDate]; //设置十分秒 信息
NSInteger hour = ;
NSInteger minute = ;
NSInteger second = ; if (cmps.hour<= || cmps.minute<= || cmps.second<=) { //判断开始时间和 当前时间的差值
NSDateComponents *cmps = [createDate deltaFrom:nowDate]; //设置十分秒 信息
hour = cmps.hour;
minute = cmps.minute;
second = cmps.second; }else{ //设置十分秒 信息
hour = cmps.hour;
minute = cmps.minute;
second = cmps.second; } if (hour== && minute== && second==) { //掉用活动开始
[self setupOrientationStart]; [self.timer invalidate];
self.timer = nil; } NSString *houreT = [NSString stringWithFormat:@"%02zd",hour>?hour:];
NSString *minuteT = [NSString stringWithFormat:@"%02zd",minute>?minute:];
NSString *secondT = [NSString stringWithFormat:@"%02zd",second>?second:]; CGFloat houreW = [houreT boundingRectWithSize:CGSizeMake(MAXFLOAT, orientationItemW) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:]} context:nil].size.width; if (houreW>=orientationItemW) {
[_hourLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(houreW+);
}];
} [self.hourLabel setTitle:houreT forState:UIControlStateNormal];
[self.minuteLabel setTitle:minuteT forState:UIControlStateNormal];
[self.secondLabel setTitle:secondT forState:UIControlStateNormal]; } #pragma mark 修改日期 每一秒掉用一次 修改结束时间
- (void)modifEndDate
{ // 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 活动开始时间
NSDate *create = [fmt dateFromString:_orientation.expired_at]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区 NSTimeInterval time = [zone secondsFromGMTForDate:create];//以秒为单位返回当前时间与系统格林尼治时间的差 NSDate *createDate = [create dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间 //获取当前时间
NSDate *nowDate = [self getCurrentDate]; //判断开始时间和 当前时间的差值
NSDateComponents *cmps = [nowDate deltaFrom:createDate]; //设置十分秒 信息
NSInteger hour = ;
NSInteger minute = ;
NSInteger second = ; if (cmps.hour<= || cmps.minute<= || cmps.second<=) { //判断开始时间和 当前时间的差值
NSDateComponents *cmps = [createDate deltaFrom:nowDate]; if (cmps.day>) { //设置十分秒 信息
hour = cmps.hour+(cmps.day*); }else{
//设置十分秒 信息
hour = cmps.hour;
} minute = cmps.minute;
second = cmps.second; }else{ //设置十分秒 信息
hour = cmps.hour;
minute = cmps.minute;
second = cmps.second; } if (hour== && minute== && second==) { //掉用活动结束
[self setupOrientationEnd]; [self.timer1 invalidate];
self.timer1 = nil; } NSString *houreT = [NSString stringWithFormat:@"%02zd",hour>?hour:];
NSString *minuteT = [NSString stringWithFormat:@"%02zd",minute>?minute:];
NSString *secondT = [NSString stringWithFormat:@"%02zd",second>?second:]; CGFloat houreW = [houreT boundingRectWithSize:CGSizeMake(MAXFLOAT, orientationItemW) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:]} context:nil].size.width; if (houreW>orientationItemW) {
[_hourLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(houreW+);
}];
} [self.hourLabel setTitle:houreT forState:UIControlStateNormal];
[self.minuteLabel setTitle:minuteT forState:UIControlStateNormal];
[self.secondLabel setTitle:secondT forState:UIControlStateNormal]; } #pragma mark 设置12小时外开购的信息
- (void)setupOrientation12Out
{
if (self.showOldPrice) { _bgImageView.image = [UIImage imageNamed:@"homeRrientationGreenbg"]; }else{ _bgImageView.image = [UIImage imageNamed:@"goods_orientation_greenbg"]; }
[_saleButton setTitleColor:[UIColor colorWithHexString:@"76a505"] forState:UIControlStateNormal];
[_saleButton setBackgroundImage:[UIImage imageNamed:@"goods_orientation_yellowRectang"] forState:UIControlStateNormal]; [self hideTime]; _endLabel.hidden = YES;
_rightLabel.hidden = NO;
_rightLabel.text =[self formatterStartTime];
_startLabel.text = @"正式开始"; } #pragma mark 格式化开始时间字符串
- (NSString*)formatterStartTime
{ // 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:];
[fmt setTimeZone:GTMzone]; // 活动开始时间
NSDate *create = [fmt dateFromString:_orientation.started_at]; if (create.isThisYear) { // 今年 // 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"MM-dd HH:mm:ss"; NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:];
[fmt setTimeZone:GTMzone]; return [fmt stringFromDate:create]; }else{ return _orientation.started_at;
} } #pragma mark 设置1活动开始的信息
- (void)setupOrientationStart
{
_endLabel.hidden = YES; [self showTime]; if (self.showOldPrice) { _bgImageView.image = [UIImage imageNamed:@"homeRrientationRedbg"]; }else{ _bgImageView.image = [UIImage imageNamed:@"goods_orientation_redbg"]; } [_saleButton setTitleColor:[UIColor colorWithHexString:@"e72646"] forState:UIControlStateNormal];
[_saleButton setBackgroundImage:[UIImage imageNamed:@"goods_orientation_yellowRectang"] forState:UIControlStateNormal]; _rightLabel.text = @"距离结束仅剩"; //掉用定时器
[self.timer1 fire];
} #pragma mark 设置1活动结束的信息
- (void)setupOrientationEnd
{ //设置可以添加到购物车
weakifySelf
if(self.orientationPriceViewEndBlock){
weakSelf.orientationPriceViewEndBlock();
} _bgImageView.image = [UIImage imageNamed:@"goods_orientation_graybg"];
[_saleButton setTitleColor:[UIColor colorWithHexString:@"b3b3b3"] forState:UIControlStateNormal];
[_saleButton setBackgroundImage:[UIImage imageNamed:@"goods_orientation_grayRectang"] forState:UIControlStateNormal];
_endLabel.text = @"活动已结束"; _rightLabel.hidden = YES;
_startLabel.hidden = YES; [self hideTime]; _endLabel.hidden = NO; } #pragma mark 显示时间控件
- (void)showTime
{
_rightLabel.hidden = NO;
_hourLabel.hidden = NO;
_pointLeft.hidden = NO;
_pointRight.hidden = NO;
_minuteLabel.hidden = NO;
_secondLabel.hidden = NO;
} #pragma mark 隐藏时间控件
- (void)hideTime
{
_rightLabel.hidden = YES;
_hourLabel.hidden = YES;
_pointLeft.hidden = YES;
_pointRight.hidden = YES;
_minuteLabel.hidden = YES;
_secondLabel.hidden = YES;
} @end

NSDate+SY 分类代码实现如下:

#import <Foundation/Foundation.h>

@interface NSDate (SY)

/**
* 获取当前区域的当前时间
*/
+ (NSDate *)getCurrentDate; /**
* 比较from和self的时间差值
*/
- (NSDateComponents *)deltaFrom:(NSDate *)from; /**
* 是否为今年
*/
- (BOOL)isThisYear; /**
* 是否为今天
*/
- (BOOL)isToday; /**
* 是否为昨天
*/
- (BOOL)isYesterday; @end
#import "NSDate+SY.h"

@implementation NSDate (SY)

/**
* 获取当前区域的当前时间
*/
+ (NSDate *)getCurrentDate
{
NSDate *date = [NSDate date]; //获得时间对象 NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区 NSTimeInterval time = [zone secondsFromGMTForDate:date];//以秒为单位返回当前时间与系统格林尼治时间的差 return [date dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间
} - (NSDateComponents *)deltaFrom:(NSDate *)from
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar]; // 比较时间
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; return [calendar components:unit fromDate:from toDate:self options:];
} - (BOOL)isThisYear
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self]; return nowYear == selfYear;
} - (BOOL)isToday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd"; NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self]; return [nowString isEqualToString:selfString];
} - (BOOL)isYesterday
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd"; NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]]; NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:]; return cmps.year ==
&& cmps.month ==
&& cmps.day == ;
} @end

到这里限时特价的一个自定义view就封装好了  我这里集成到项目中的 效果图如下:

iOS 商品倒计时 限时特价 限时优惠 功能的封装的更多相关文章

  1. iOS 按钮倒计时功能

    iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...

  2. iOS活动倒计时的两种实现方式

    代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...

  3. PHP商品倒计时 php实现当前用户在线人数

    //PHP商品倒计时 date_default_timezone_set("Asia/shanghai");//地区 //配置每天的活动时间段 $starttimestr = &q ...

  4. 《Office 365开发入门指南教程》正式上线,限时优惠和邀请分享推广

    我很高兴地通知大家,<Office 365 开发入门指南教程>已经正式在网易云课堂上线,你可以通过直接访问  https://aka.ms/office365devlesson 这个短地址 ...

  5. 再谈iOS 7的手势滑动返回功能

    本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147  之前随手写过一篇<使用UIScreenEdgePanGestureR ...

  6. Vue实现仿淘宝商品详情属性选择的功能

    Vue实现仿淘宝商品详情属性选择的功能 先看下效果图:(同个属性内部单选,属性与属性之间可以多选) 主要实现过程: 所使用到的数据类型是(一个大数组里面嵌套了另一个数组)具体格式如下:   attrA ...

  7. iOS 14.5 有啥新功能?Apple Watch 也能解锁 iPhone 了

    转: iOS 14.5 有啥新功能?Apple Watch 也能解锁 iPhone 了 苹果今天发布了即将发布的 iOS 14.5 和 iPadOS 14.5 更新的第一个 Beta 版本,我们在其中 ...

  8. VSPackge插件系列:常用IDE功能的封装

    继上一篇VSPackge插件系列简单介绍如何正确的获取DTE之后,就一直没发VSPackge插件系列的文章了,最近同事也想了解如何在代码中与VS交互,特发一篇文章示例一些简单功能是如何调用,也以备以后 ...

  9. 实现AOP功能的封装与配置的小框架

    内容 java基础巩固笔记 - 实现AOP功能的封装与配置的小框架 设计(目录): XXX = java.util.ArrayList中 代码 Advice接口 MyAdvice类 BeanFacto ...

随机推荐

  1. Linux中bashshell的一些知识

    数据流重导向 重导向redirect:就是将当前的所得数据输出到其他地方: 三种输出输入的状况,分别是: -标准输入stdin:代码为0:使用<或<< -标准输出stdout:代码为 ...

  2. 导向矢量(Steering Vector)

    导向矢量是阵列天线的所有阵元对具有单位能量窄带信源的响应. 由于阵列响应在不同方向上是不同的,导向矢量与信源的方向是相互关联的,这种关联的独特性依赖于阵列的几何结构.对于同一阵元阵列,导向矢量的每一个 ...

  3. Hadoop学习笔记—14.ZooKeeper环境搭建

    从字面上来看,ZooKeeper表示动物园管理员,这是一个十分奇妙的名字,我们又想起了Hadoop生态系统中,许多项目的Logo都采用了动物,比如Hadoop采用了大象的形象,所以我们可以猜测ZooK ...

  4. 从Knockout到Angular的架构演变

    2008年第一次在WPF中使用MVVM模式之后,就一直热衷于耦合隔离.模块化与重构.UI和逻辑分离.单元测试以及后面的领域模型.谈及MVVM模式,自己也开发过一套框架,但没有长期更新和维护,所以索性就 ...

  5. 几个SQL小知识

    写在前面的话:之前做的一个项目,数据库及系统整体构架设计完成之后,和弟兄们经过一段时间的编码,系统如期上线,刚开始运行一切良好,后来随着数据量的急剧膨胀,慢慢出现了很多莫名其妙的问题,经过调试,修改了 ...

  6. ASP.NET MVC 5 - 查询Details和Delete方法

    在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...

  7. Angular 2 最终版正式发布

    9月15日,Angular 2 的最终版正式发布了. 作为 Angular 1 的全平台继任者 -- Angular 2 的最终版,意味着什么? 意味着稳定性已经得到了大范围用例的验证: 意味着已经针 ...

  8. 2013 duilib入门简明教程 -- 简单控件介绍 (12)

        前面的教程应该让大家对duilib的整体有所映像了,下面就来介绍下duilib具体控件的使用.     由于官方没有提供默认的控件样式,所以我就尽量使用win7或者XP自带的按钮样式了,虽然界 ...

  9. Linux核心源码阅读方法

    首次阅读Linux4.0.5内核源代码时,一脸茫然,它的代码不仅复杂,而且庞大,找不到从哪里开始阅读. 通常Linux会有以下目录 arch 子目录包括所有和体系结构相关的核心代码.它还有更深的子目录 ...

  10. ASP.NET WebAPi之断点续传下载(中)

    前言 前情回顾:上一篇我们遗留了两个问题,一个是未完全实现断点续传,另外则是在响应时是返回StreamContent还是PushStreamContent呢?这一节我们重点来解决这两个问题,同时就在此 ...