問題的實作描述:首先設定倒計時的時間,通過UIDatePicker來制定倒計時的時長,點選“确定”按鈕,跳轉到倒計時頁面,同時将UIDatePicker定義的時間傳到新push的頁面的上方label位置,點選“開始”按鈕,開始倒計時,這期間可以點選“暫停/繼續”按鈕 來控制倒計時時間的停止和繼續,同時,倒計時開始後,也可重置倒計時時間pop到UIDatePicker頁面,重新設定倒計時的時間。倒計時結束時,彈出提示框。
效果圖:

代碼如下:
JSQViewController.m檔案
#import "JSQViewController.h"
#import "PushViewController.h"
#define kW self.view.frame.size.width
#define kH self.view.frame.size.height
@interface JSQViewController ()
@property (nonatomic,weak) UIDatePicker * picker;
@end
@implementation JSQViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"計時器";
//加載視圖
[self _loadViews];
}
#pragma mark - 加載視圖
- (void) _loadViews
{
//添加picker 高度:216
UIDatePicker * picker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 64, 0, 0)];
//NSLog(@"%f_ _ _ _ _ _ _ _ _%f",picker.frame.origin.y,picker.frame.size.height);
picker.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
picker.datePickerMode=UIDatePickerModeCountDownTimer;
_picker=picker;
[self.view addSubview:picker]; //添加到子視圖
UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake((367-100)/2, 64+216+50, 100, 100)];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.layer.cornerRadius=50;
button.showsTouchWhenHighlighted=YES;
button.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2];
[button addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
#pragma mark - pushVC
- (void) pushVC
{
//NSLog(@"%f",_picker.countDownDuration);
PushViewController * pushVC=[[PushViewController alloc]init];
pushVC.timeCount=_picker.countDownDuration;
[self.navigationController pushViewController:pushVC animated:YES];
}
- (void)setDate:(NSDate *)date animated:(BOOL)animated;
{
NSDate * temDate=[[NSDate alloc]initWithTimeIntervalSinceNow:2];
date=temDate;
NSLog(@"%@",_picker.minimumDate);
}
@end
PushViewController.m檔案
#import "PushViewController.h"
#define kW self.view.frame.size.width
#define kH self.view.frame.size.height
@interface PushViewController ()
{
NSTimer * _timer; //定時器
}
@property (nonatomic,weak) UILabel * label;
@property (nonatomic,weak) UIButton * button;
@end
@implementation PushViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%.0f",self.timeCount);
[self pushView];
}
- (void) pushView
{
//上方子視圖
UIView * view=[[UIView alloc]initWithFrame:CGRectMake(0, 65, kW, 216)];
view.backgroundColor=[UIColor whiteColor];
//倒計時Label
UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, kW, 216)];
//label.backgroundColor=[UIColor redColor];
label.textAlignment=NSTextAlignmentCenter;
NSString * str=[NSString stringWithFormat:@"%02d:%02d:%02d",(int)(self.timeCount)/3600%24,(int)(self.timeCount)/60%60,(int)(self.timeCount)%60];
label.text=str;
[label setFont:[UIFont fontWithName:nil size:80]];
self.label=label;
[view addSubview:label];
[self.view addSubview:view]; //添加到主視圖
//NSLog(@"%@",picker.minimumDate);
//添加下方視圖子產品
UIView * btmView=[[UIView alloc]initWithFrame:CGRectMake(0, 65+216, kW, kH-49-45-216)];
btmView.backgroundColor=[UIColor whiteColor];
UIView * xView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kW, kH-49-45-216)];
xView.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
//添加提示Label
UILabel * tsLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, kW, 48)];
tsLabel.backgroundColor=[UIColor whiteColor];
[tsLabel setFont:[UIFont fontWithName:nil size:20]];
tsLabel.textAlignment=NSTextAlignmentCenter;
tsLabel.text=@"計時結束時,啟用234 Do Do Do... >";
[xView addSubview:tsLabel];
//開始/重置 按鈕
UIButton * kqButton=[[UIButton alloc]initWithFrame:CGRectMake((kW-200)/3, 50+50, 100, 100)];
kqButton.backgroundColor=[UIColor whiteColor];
kqButton.layer.cornerRadius=50;
[kqButton setTitle:@"開始" forState:UIControlStateNormal];
[kqButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[kqButton setTitle:@"重置" forState:UIControlStateSelected];
[kqButton setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
[kqButton addTarget:self action:@selector(startRenew:) forControlEvents:UIControlEventTouchUpInside];
self.button=kqButton;
[xView addSubview:kqButton];
//暫停/繼續 按鈕
UIButton *zkButton=[[UIButton alloc]initWithFrame:CGRectMake((kW-200)/3*2+100, 100, 100, 100)];
zkButton.backgroundColor=[UIColor whiteColor];
zkButton.layer.cornerRadius=50;
[zkButton setTitle:@"暫停" forState:UIControlStateNormal];
[zkButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[zkButton setTitle:@"繼續" forState:UIControlStateSelected];
[zkButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[zkButton addTarget:self action:@selector(stopContinue:) forControlEvents:UIControlEventTouchUpInside];
[xView addSubview:zkButton];
[btmView addSubview:xView];
[self.view addSubview: btmView]; //添加到主視圖
}
#pragma mark - 開始重置按鈕點選事件
- (void) startRenew: (UIButton *) button
{
button.selected=!button.selected;
if(_timer==nil)
{
//每隔0.01秒重新整理一次頁面
_timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(runAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
NSLog(@"開始倒計時.....");
}
else
{
[_timer invalidate]; //定時器失效
[self _popView];
}
}
- (void) _popView
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void) runAction
{
_timeCount--;
if (_timeCount==0)
{
[_timer invalidate];//讓定時器失效
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"滴滴滴滴滴...時間到!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
NSString * str=[NSString stringWithFormat:@"%02d:%02d:%02d",(int)(self.timeCount)/3600%24,(int)(self.timeCount)/60%60,(int)(self.timeCount)%60];
_label.text=str;
}
#pragma mark - 暫停繼續按鈕點選事件
- (void) stopContinue:(UIButton *) button
{
if([self.label.text isEqualToString:@"00:00:00"] || !self.button.selected)
{
return;
}
button.selected=!button.selected;
if (!button.selected)
{
//每隔0.01秒重新整理一次頁面
_timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(runAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
NSLog(@"繼續.....");
}
else
{
[_timer invalidate];//讓定時器失效
NSLog(@"暫停中.....");
}
}
@end
PushViewController.h檔案
#import <UIKit/UIKit.h>
@interface PushViewController : UIViewController
@property (nonatomic,assign) float timeCount;
@end 在該檔案中提供了一個接口,當push頁面時,可以将picker的值傳過來
需要注意的是:picker的大小是固定的(高度216),是以自定義picker時,隻需考慮它的起始位置即可.還有一個就是秒表和計時器有啥差別,秒表是正着走,從零開始,你要它停它停,你要它走它走;而計時器是你事先給它定好一段時間,它開始倒計時,等到00:00:00時就停下。。。(其實我以前傻傻分不清楚哪個是哪個來着)
PS:最近在學多線程,不要問我多線程是啥,我已經繞裡面出不來了,什麼同步異步,串行并行,還有主線程和子線程!完全搞暈中~ 快被自己蠢哭了!我隻知道,重新整理UI要在主線程,不要太笨啊!