2016-02-19 20:57:09 qq_31810357 阅读数 13832
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18851 人正在学习 去看看 邹积超

示例代码简单易懂:

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
@property (weak, nonatomic) IBOutlet UILabel *hourLabel;
@property (weak, nonatomic) IBOutlet UILabel *minuteLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;

@end


#import "ViewController.h"

@interface ViewController ()
{
     dispatch_source_t _timer;
}
@end

@implementation ViewController
/**
 *  获取当天的年月日的字符串
 *  这里测试用
 *  @return 格式为年-月-日
 */
-(NSString *)getyyyymmdd{
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd";
    NSString *dayStr = [formatDay stringFromDate:now];
    
    return dayStr;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *endDate = [dateFormatter dateFromString:[self getyyyymmdd]];
    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(), ^{
                        self.dayLabel.text = @"";
                        self.hourLabel.text = @"00";
                        self.minuteLabel.text = @"00";
                        self.secondLabel.text = @"00";
                    });
                }else{
                    int days = (int)(timeout/(3600*24));
                    if (days==0) {
                        self.dayLabel.text = @"";
                    }
                    int hours = (int)((timeout-days*24*3600)/3600);
                    int minute = (int)(timeout-days*24*3600-hours*3600)/60;
                    int second = timeout-days*24*3600-hours*3600-minute*60;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (days==0) {
                            self.dayLabel.text = @"0天";
                        }else{
                            self.dayLabel.text = [NSString stringWithFormat:@"%d天",days];
                        }
                        if (hours<10) {
                            self.hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
                        }else{
                            self.hourLabel.text = [NSString stringWithFormat:@"%d",hours];
                        }
                        if (minute<10) {
                            self.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
                        }else{
                            self.minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
                        }
                        if (second<10) {
                            self.secondLabel.text = [NSString stringWithFormat:@"0%d",second];
                        }else{
                            self.secondLabel.text = [NSString stringWithFormat:@"%d",second];
                        }
                        
                    });
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        }
    }


}

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

效果:


 
                  iOS开发者交流群:446310206

Demo下载地址GitHubhttps://github.com/XiaoHanGe/CountDown

2015-04-16 11:50:08 u010328250 阅读数 462
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18851 人正在学习 去看看 邹积超
#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        _titleLabel.center = self.view.center;
        _titleLabel.backgroundColor = [UIColor orangeColor];
        _titleLabel.font = [UIFont systemFontOfSize:17.0];
        _titleLabel.textColor = [UIColor whiteColor];
        [self.view addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
    //将timer添加到运行循环,模式:NSRunLoopCommonModes监听滚动模式
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

//获取到到期的时间,还需要转化一个取得时分秒.
- (NSString *)getCurrentTime
{
    NSString *date = @"2015-04-16 11:54:50";

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:date];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

- (void)updateTimer
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date1 = [formatter dateFromString:[self getCurrentTime]];
    NSDate *date2 = [NSDate date];

    NSTimeZone *zone = [NSTimeZone systemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date2];

    NSDate *localeDate = [date2  dateByAddingTimeInterval: interval];

    NSTimeInterval aTimer = [localeDate timeIntervalSinceDate:date1];
    //aTimer为负的 aTimer1把aTimer转为正的
    NSTimeInterval aTimer1 = - aTimer;
    NSInteger day = (NSInteger)(aTimer1/(3600*24));
    NSInteger hour = (NSInteger)(aTimer1/3600);
    NSInteger minute = (NSInteger)(aTimer1 - hour*3600)/60;
    NSInteger second = (aTimer1 - hour*3600 - minute*60);
    NSLog(@" %f   %ld %ld  %ld",aTimer1, hour,minute,second);

    if (hour >= 24) {
        hour %= 24;
    }
    NSLog(@" %f   %ld %ld  %ld",aTimer1, hour,minute,second);

    NSString *dural = [NSString stringWithFormat:@"%ld天%ld时%ld分%ld秒", day,hour, minute,second];
    if (hour <= 0 && minute <= 0 && second <= 0) {
        self.titleLabel.text = @"活动已过期,谢谢关注";

        [self pause];
    } else {
        self.titleLabel.text = dural;
    }


}

- (void)pause
{
    //停止时钟,invalidate是唯一的方法,一调用就干掉timer了,想再用只能重新实例化
    [self.timer invalidate];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果

2017-05-09 16:02:16 qq_19678579 阅读数 3634
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18851 人正在学习 去看看 邹积超

IOS关于大型网站抢购、距活动结束,剩余时间倒计时的实现代码,代码比较简单,大家根据需求适当的添加修改删除代码
这里写图片描述

1.定义4个 Label 来接收倒计时:

@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
@property (weak, nonatomic) IBOutlet UILabel *hourLabel;
@property (weak, nonatomic) IBOutlet UILabel *minuteLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;

2.在实现文件中实现方法:

//时间戳转换为日期格式(毫秒的时间戳)
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    NSLog(@"时间 === %@",dateString);
    return dateString;
}
-(void)downSecondHandle:(NSString *)aTimeString{

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


    NSDate *endDate = [dateFormatter dateFromString:[self timeWithTimeIntervalString:aTimeString]]; //结束时间
    NSDate *endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate])];
    NSDate *startDate = [NSDate date];
        NSString* dateString = [dateFormatter stringFromDate:startDate];
    NSLog(@"现在的时间 === %@",dateString);
    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(), ^{
                        self.dayLabel.text = @"";
                        self.hourLabel.text = @"00";
                        self.minuteLabel.text = @"00";
                        self.secondLabel.text = @"00";
                    });
                }else{
                    int days = (int)(timeout/(3600*24));
                    if (days==0) {
                        self.dayLabel.text = @"";
                    }
                    int hours = (int)((timeout-days*24*3600)/3600);
                    int minute = (int)(timeout-days*24*3600-hours*3600)/60;
                    int second = timeout-days*24*3600-hours*3600-minute*60;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (days==0) {
                            self.dayLabel.text = @"0天";
                        }else{
                            self.dayLabel.text = [NSString stringWithFormat:@"%d天",days];
                        }
                        if (hours<10) {
                            self.hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
                        }else{
                            self.hourLabel.text = [NSString stringWithFormat:@"%d",hours];
                        }
                        if (minute<10) {
                            self.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
                        }else{
                            self.minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
                        }
                        if (second<10) {
                            self.secondLabel.text = [NSString stringWithFormat:@"0%d",second];
                        }else{
                            self.secondLabel.text = [NSString stringWithFormat:@"%d",second];
                        }

                    });
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        }
    }


}

3.在需要出使用:

 [self downSecondHandle:@"1494622800000"];
2016-07-08 11:33:07 yutaotst 阅读数 1155
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18851 人正在学习 去看看 邹积超

#在button的点击触发方法里面调用这个- (void)verifyEvent方法就可以实现倒计时


- (void)verifyEvent

{

    //启动倒计时

    [selfperformSelector:@selector(reflashGetKeyBt:)withObject:[NSNumbernumberWithInt:60] afterDelay:0];

}


//倒数

- (void)reflashGetKeyBt:(NSNumber *)second

{

   if ([second integerValue] == 0)

    {

        _codeButton.selected=YES;

        _codeButton.userInteractionEnabled=YES;

        [_codeButtonsetTitle:@"重新获取"forState:(UIControlStateNormal)];

        [_codeButtonsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

    }

   else

    {

        _codeButton.selected=NO;

        _codeButton.userInteractionEnabled=NO;

       int i = [second intValue];

        [_codeButtonsetTitle:[NSStringstringWithFormat:@"重新获得(%i)",i]forState:(UIControlStateNormal)];

        [selfperformSelector:@selector(reflashGetKeyBt:)withObject:[NSNumbernumberWithInt:i-1] afterDelay:1];

    }

    

}



2018-08-11 15:21:16 yizhiliulangdemiao 阅读数 142
  • 微信小程序开发实战第三季

    本季完成一个辩论赛计时器APP,涉及到动画使用、声音播放、action-sheet、tabBar、表单元素form、switch、slider、radio-group、button等,涉及到面向对象的编程思路

    18851 人正在学习 去看看 邹积超

倒计时按钮在项目中经常用到,为了使用方便我做了简单的封装。
下面直接上代码。

#import <UIKit/UIKit.h>

typedef void(^setBtnAction)();

@interface countdownButton : UIButton

//倒计时开始回调
@property(nonatomic,strong)setBtnAction setBtnAction;

@end
#import "countdownButton.h"

@interface countdownButton()

@property(nonatomic,strong)UIButton * btn;

@end

@implementation countdownButton
{

    NSInteger secondsCountDown;
    NSTimer * countDownTimer;

}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        secondsCountDown = 60;

        [self setBtnUI];
    }

    return self;
}

-(void)setBtnUI
{

    [self setBackgroundColor:[UIColor orangeColor]];

    [self setTitle:@"获取验证码" forState:UIControlStateNormal];

    [self.titleLabel setFont:[UIFont systemFontOfSize:14.0]];

    [self addTarget:self action:@selector(countDownAction:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)countDownAction:(UIButton *)sender
{

    [self setTitle:[NSString stringWithFormat:@"%zd s",secondsCountDown] forState:UIControlStateNormal];

    countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];

    [self setEnabled:NO];

    if(self.setBtnAction)
    {
        self.setBtnAction();
    }

}

-(void)timeFireMethod
{
    secondsCountDown--;

    [self setTitle:[NSString stringWithFormat:@"%zd s",secondsCountDown] forState:UIControlStateNormal];

    if (secondsCountDown<0)
    {
        [countDownTimer invalidate];

        [self setEnabled:YES];

        [self setTitle:@"获取验证码" forState:UIControlStateNormal];

        secondsCountDown = 60;
    }

}
@end

在需要使用倒计时按钮的地方初始化。
这里我在ViewController 中初始化,记得引入头文件。

#import "countdownButton.h"
 countdownButton * btn = [[countdownButton alloc]initWithFrame:CGRectMake(100, 100, 90, 40)];

    [btn setBackgroundColor:[UIColor purpleColor]];

    [btn setSetBtnAction:^(){

       //这里写入倒计时开始时需要执行的事件。
        NSLog(@"倒计时开始");

    }];

    [self.view addSubview:btn];

demo 地址:https://github.com/xiaobai0134/XBCountDownButton

Xamarin.iOS倒计时功能实现

博文 来自: geanwen

iOS 倒计时器

阅读数 367

iOS秒杀倒计时

阅读数 984

iOS中GCD版秒杀倒计时.

博文 来自: SharkToping
没有更多推荐了,返回首页