iOS 实现倒计时

#import "ViewController.h"

#import "QSSDateHelper.h"

@interface ViewController ()

@property (nonatomic,strong) UIButton *btn;

@property (nonatomic,assign) BOOL isTouch;

@property (nonatomic,assign) NSInteger time;

@property (nonatomic,assign) int year;

@property (nonatomic,assign) int mouth;

@property (nonatomic,assign) int day;

@property (nonatomic,assign) int hour;

@property (nonatomic,assign) int minute;

@property (nonatomic,assign) int second;

@property (nonatomic,assign) int myTouchSecond;

@property NSDate *touchDate;

@property NSCalendar *cal;

@property NSTimer *timer ;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self initBtn];

self.isTouch=NO;

}

- (void)dealloc{

[self.timer invalidate];

self.timer=nil;

self.time=0;

}

- (void)initBtn{

self.btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

[self.btn setTitle:@"倒计时" forState:UIControlStateNormal];

[self.btn setBackgroundColor:[UIColor lightGrayColor]];

[self.view addSubview:self.btn];

[self.btn addTarget:self action:@selector(releaseTime) forControlEvents:UIControlEventTouchUpInside];

}

- (void)timerFireMethod:(NSTimer *)timer{

//NSDate *dd = [NSDate dateWithTimeIntervalSince1970:self.myTouchSecond];

int targetSecond = self.myTouchSecond + 10*60;//目标的时间

//把目标的秒数转化为固定格式的秒数

NSDate *dd = [NSDate dateWithTimeIntervalSince1970:targetSecond];

unsigned int time2=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

NSCalendar *cal=[NSCalendar currentCalendar];

NSDateComponents *t2=[cal components:time2 fromDate:dd];

//==================开始定时的时间=====================

unsigned int time=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

NSDateComponents *t=[self.cal components:time fromDate:self.touchDate]; //开始定时的时间

NSLog(@"开始倒计时的时间:%d天%d小时%d分钟%d秒",(int)t.day,(int)t.hour,(int)t.minute,(int)t.second);

//==================目标时间==========================

NSLog(@"目标时间:%d天%d小时%d分钟%d秒",(int)t2.day,(int)t2.hour,(int)t2.minute,(int)t2.second);

//目标时间

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];

[components setYear:t2.year];

[components setMonth:t2.month];

[components setDay:t2.day];

[components setHour:t2.hour];

[components setMinute:t2.minute];

NSLog(@"minute +++++ 10   %d",self.minute+10);

[components setSecond:self.second];

unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

//NSDate *fireDate = [calendar dateFromComponents:components];//目标时间

NSDate *nowhhhhh=[NSDate date];

NSDateComponents *d = [calendar components:unitFlags fromDate:nowhhhhh toDate:dd options:0];//计算时间差

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%d分钟%d秒",(int)[d minute],(int)[d second]] message:@"倒计时" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];

alert.delegate=self;

[alert show];

}

- (void)releaseTime{

self.time++;

self.isTouch=YES;

if (self.time==1){

NSLog(@"开始倒计时");

//当前时间 只调用一次这个方法。。。

NSDate *now=[NSDate date];

NSCalendar *cal=[NSCalendar currentCalendar];

unsigned int time=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

NSDateComponents *t=[cal components:time fromDate:now];

self.year=(int)[t year];

self.mouth=(int)[t month];

self.day=(int)[t day];

self.hour=(int)[t hour];

self.minute=(int)[t minute];

self.second=(int)[t second];

NSTimeInterval time2=[[NSDate date] timeIntervalSince1970];

self.myTouchSecond = time2;

NSLog(@"date\n%d", self.myTouchSecond);

self.touchDate=now;

self.cal=cal;

}else{

self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];

[self.timer setFireDate:[NSDate distantPast]];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

@end

时间: 2024-08-02 19:15:27

iOS 实现倒计时的相关文章

iOS 按钮倒计时功能

iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 1 __block int time = 60; 2 __block UIButton *verifybutton = _GetverificationBtn; 3 verifybutton.enabled = NO; 4 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 5 dispatch_so

iOS 商品倒计时 限时特价 限时优惠 功能的封装

最近项目中多个页面用到了 商品特价倒计时的功能  为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下: 定向价 限时特价 模型代码实现: #pragma mark 商品定向价模型 @interface STGoodsOrientationPrice : STBaseModel /**定向价**/ @property (nonatomic, copy) NSString *price; /**定向价开始时间**/ @property (nonatomic, copy) NSStr

【IOS】倒计时实现的两种方法

方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: 1 secondsCountDown = 60;//60秒倒计时 2 countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self

ios实现倒计时的两种方法

方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: [cpp] view plaincopyprint? secondsCountDown = 60;//60秒倒计时 countDownTimer = [NSTimer scheduledTimerWithTim

iOS验证码倒计时(GCD实现)

+ (void)verificationCode:(void(^)())blockYes blockNo:(void(^)(id time))blockNo { __block int timeout=60; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create

iOS中倒计时

方法一:使用NSTimer来实现(比较适用于发送短信验证码倒计时) 主要是利用NSTimer的scheduledTimerWithTimeInterval方法来每秒执行一次changeTime方法 //创建一个Timer,每秒执行一次changeTime:方法 NSTimer * timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime:) userInfo:nil r

iOS GCD倒计时

GCD倒计时的好处在于不用考虑是否定时器无法释放的问题,runloop的问题,还有精度更加高 使用GCD创建定时器方法 -(void)startCountDown:(NSInteger)maxTime{ __block int time = 0; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t timer = dispatch_sou

iOS开发 倒计时

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES]; - (void)timerFireMethod:(NSTimer*)theTimer{ id obj = [theTimer userInfo]; NSDateFormatter *f1 = [[NSDateFormatter alloc] init]; [f

iOS中如何实现准确的倒计时程序 · 九十里

iOS中倒计时程序,考虑线程暂停场景. iOS App进入后台时,GCD线程也会跟着暂停.当程序进入前台后,GCD线程恢复.因而倒计时程序需要考虑这一点,通过加入时间的比对来实现. + (void)countDownWithLapseTime:(int)lapseTime andBlock:(void(^)(int timeLapse)) countDownBlock{ __block dispatch_source_t timer; NSTimeInterval timeInterval=la