iOS开发之实现商品倒计时效果

一.倒计时(如:发送验证码)

#define countDownTime 119  //倒计时时间
#pragma mark - 倒计时
- (void)myTimer
{
    if (countDownTime) {
        __block int timeout= countDownTime; //倒计时时间
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(timer, ^{
            if(timeout < 0){ //倒计时结束,关闭
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                });
            }else{
                int hours = timeout / (60 * 60);
                int minute = timeout % (60 * 60) / 60;
                int seconds = timeout % 60;
                if (hours < 10) {
                    hours = [[NSString stringWithFormat:@"0%d",hours] intValue];
                }
                if (minute < 10) {
                    minute = [[NSString stringWithFormat:@"0%d",minute] intValue];
                }
                if (seconds < 10) {
                    seconds = [[NSString stringWithFormat:@"0%d",seconds] intValue];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    [UIView beginAnimations:nil context:nil];
                    [UIView setAnimationDuration:1];
                    hourLabel.text = [NSString stringWithFormat:@"%d",hours];
                    minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
                    secondsLabel.text = [NSString stringWithFormat:@"%d",seconds];
                    if (hours < 10) {
                        hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
                    }
                    if (minute < 10) {
                        minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
                    }
                    if (seconds < 10) {
                        secondsLabel.text = [NSString stringWithFormat:@"0%d",seconds];
                    }
                    [UIView commitAnimations];
                });
                timeout--;
            }
        });
        dispatch_resume(timer);
    }
}

二.倒计时(并计算时间差)

全局变量:dispatch_source_t _timer;
#pragma mark - 倒计时
- (void)countDown{
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd";
    NSString *dayStr = [formatDay stringFromDate:now];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *endDate = [dateFormatter dateFromString:dayStr];
    NSDate *endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate] + 24 * 3600)];
    NSDate *startDate = [NSDate date];
    NSTimeInterval timeInterval =[endDate_tomorrow timeIntervalSinceDate:startDate];
    
    if (_timer == nil) {
        __block int timeout = timeInterval; //倒计时时间
        if (timeout != 0) {
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 * NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout <= 0){ //倒计时结束,关闭
                    dispatch_source_cancel(_timer);
                    _timer = nil;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        hourLabel.text = @"00";
                        minuteLabel.text = @"00";
                        secondsLabel.text = @"00";
                    });
                }else{
                    int hours = (int)(timeout / 3600);
                    int minutes = (int)(timeout - hours * 3600) / 60;
                    int seconds = timeout - hours * 3600 - minutes * 60;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (hours < 10) {
                            hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
                        }else{
                            hourLabel.text = [NSString stringWithFormat:@"%d",hours];
                        }
                        if (minutes < 10) {
                            minuteLabel.text = [NSString stringWithFormat:@"0%d",minutes];
                        }else{
                            minuteLabel.text = [NSString stringWithFormat:@"%d",minutes];
                        }
                        if (seconds < 10) {
                            secondsLabel.text = [NSString stringWithFormat:@"0%d",seconds];
                        }else{
                            secondsLabel.text = [NSString stringWithFormat:@"%d",seconds];
                        }
                        
                    });
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        }
    }
}

例子三

__block int timeout=59; //倒计时时间 
   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
   dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); 
   dispatch_source_set_timer(aSelf.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行 
   dispatch_source_set_event_handler(aSelf.timer, ^{ 
       if(timeout<=0){ //倒计时结束,关闭 
           dispatch_source_cancel(aSelf.timer); 
           aSelf.timer = nil; 
           dispatch_async(dispatch_get_main_queue(), ^{ 
               //设置界面的按钮显示 根据自己需求设置 
           }); 
       }else{ 
           //            int minutes = timeout / 60; 
           int seconds = timeout % 60; 
           NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds]; 
           dispatch_async(dispatch_get_main_queue(), ^{ 
               //设置界面的按钮显示 根据自己需求设置 
           }); 
           timeout--; 
       } 
   }); 

dispatch_resume(aSelf.timer); 

好了以上就是关于ios倒计时的一个实现方法了,这个和js中的倒计时非常的接近的哦各位应该懂了吧。

时间: 2024-08-30 09:13:00

iOS开发之实现商品倒计时效果的相关文章

iOS开发:UIView的Animation效果

  所谓动画效果,就是会动的画,到iOS App中来说的话,就是各种UIView的移动. 想想看,如果我们自己来实现所有UIView的动画效果,需要考虑些什么东西呢? * 该UIView现在在哪儿? * 该UIView最后会动到哪儿? * 该UIView以什么样的方式移动到那儿? * 该动画持续多长时间? * 每次移动的最小时间间隔? * 每次最小时间间隔的移动的应该移动到哪儿? * -. 想想这是一个多么杀脑细胞的过程,尤其是每一次的动画过程都要重复这一折磨的过程. 还好,现实比想象的美好,

iOS 开发之动画篇 - 从 UIView 动画说起

毋庸置疑的:在iOS开发中,制作动画效果是最让开发者享受的环节之一.一个设计严谨.精细的动画效果能给用户耳目一新的效果,吸引他们的眼光 -- 这对于app而言是非常重要的. 本文作为动画文集的第一篇,最开始是想做个qq下拉刷新的水滴动画的制作讲解,但这几天研读<iOS Animations by Tutorials>一书,对iOS动画开发有了更为深刻的了解,于是决定动画篇将从UIView动画开始讲起,以后还会有Layer.Transitioning等在内的动画,希望本文能抛砖引玉,带给大家不一

iOS开发之各种动画各种页面切面效果

因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一些干货,切勿错过哦.今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发中常用的动画.说道动画其中有一个是仿射变换的概念,至于怎么仿射的怎么变换的,原理如何等在本篇博客中不做赘述.今天要分享的是如和用动画做出我们要做的效果. 今天主要用到的动画类是CALayer下的CATransition至于各种动画类中如何继承的在这也不做赘述,网上的资料是一抓一大把.好废话少说切入今天的正题. 一.封装动画

iOS开发中Quartz2D控制圆形缩放和实现刷帧效果_IOS

Quartz2D简要回顾一.什么是Quartz2D  Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统  Quartz 2D能完成的工作:   绘制图形 : 线条\三角形\矩形\圆\弧等   绘制文字   绘制\生成图片(图像)   读取\生成PDF   截图\裁剪图片   自定义UI控件 二.Quartz2D在iOS开发中的价值 为了便于搭建美观的UI界面,iOS提供了UIKit框架,⾥⾯有各种各样的UI控件 UILabel:显⽰文字 UIImageView:显示图片 UIBu

iOS开发--仿新闻首页效果WMPageController的使用详解_IOS

这一篇记录的是iOS开发中第三方库WMPageController控件的使用方法,主要是用来分页显示内容的,可以通过手势滑动来切换页面,也可以通过点击标题部分来切换页面,如下图所示: 使用方法: 新建工程DemoTest1,然后通过cocoapods引入WMPageController到项目中,Podfile文件的内容如下: platform :ios,'7.0' target 'DemoTest1' do pod 'WMPageController', '~> 1.6.4' end 方法一:

iOS开发中常用的各种动画、页面切面效果_IOS

今天主要用到的动画类是CALayer下的CATransition至于各种动画类中如何继承的在这也不做赘述,网上的资料是一抓一大把.好废话少说切入今天的正题. 一.封装动画方法 1.用CATransition实现动画的封装方法如下,每句代码是何意思,请看注释之. #pragma CATransition动画实现 - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIVi

IOS开发代码分享之用nstimer实现倒计时功能_IOS

用nstimer实现倒计时功能,废话不多说,直接上代码,详细解释请参照注释 // [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];   // - (void)timerFireMethod:(NSTimer *)theTimer {     BOOL timeStart = YES;     NSCalend

iOS开发系列--地图与定位

概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个陌生的地方想要查找附近的酒店.超市等就可以打开软件搜索周边;类似的,还有很多团购软件可以根据你所在的位置自动为你推荐某些商品.总之,目前地图和定位功能已经大量引入到应用开发中.今天就和大家一起看一下iOS如何进行地图和定位开发. 定位 地图 定位 要实现地图.导航功能,往往需要先熟悉定位功能,在iO

《iOS开发指南》第二版 iOS7版-源码-样章-目录,感谢大家一直以来的支持

<iOS开发指南-从0基础到AppStore上线>第二版 iOS7版正式出版了 感谢大家一直以来的支持! 改版后采用全新的ios 7 api,详细介绍了最新的ios 7 开发相关的知识点,全部案例以iOS7版本SDK重新编译. 新增:iOS 7中文字排版和渲染引擎--Text Kit:      iOS6升级到iOS7遇到的问题与解决方法:      着重讲解iOS分层架构设计: 更新无处不在,更多新增内容请详细阅读本书 京东销售地址:http://item.jd.com/11419483.h