iOS秒表功能實現
/停止按鈕 和 計次按鈕。點擊開始按鈕,中間的大時鐘開始計時,這時點擊計次按鈕,右上角的小時鐘會把此刻的瞬時時間記錄下來,同時在下面的tableView上也會顯示。點擊停止按鈕會把右上角的小時鐘,中間的大時鐘清零,同時,下面的tableView也會清空。
效果圖如下:
具體代碼實現:
#import "MiaoBiaoViewController.h"
#define kW self.view.frame.size.width
#define kH self.view.frame.size.height
int count=;
@interface MiaoBiaoViewController ()
{
NSTimer * _timer; //定時器
NSInteger _seconds;
}
//開始暫停按鈕
@property (nonatomic,weak) UIButton * ssButton;
//計次按鈕
@property (nonatomic,weak) UIButton * jcButton;
//右上角計次時間
@property (nonatomic,weak) UILabel * conLabel;
//中間秒表
@property (nonatomic,weak) UILabel * ctLabel;
//下面的每次記錄的時間
@property (nonatomic,weak) UITableView * tableView;
//
@property (nonatomic,weak) UITableViewCell * cell;
//存放記錄的數組
@property (nonatomic,strong) NSMutableArray * jcArray;
@end
@implementation MiaoBiaoViewController
#pragma mark - 懶加載
- (NSMutableArray *)jcArray
{
if (_jcArray==nil)
{
_jcArray=[NSMutableArray array];
}
return _jcArray;
}
#pragma mark - 入口
- (void)viewDidLoad {
[super viewDidLoad];
[self _loadViews];
}
- (void) _loadViews
{
self.title=@"秒表";
//小時鐘---一直計時
UILabel * conLabel=[[UILabel alloc]initWithFrame:CGRectMake(267, 65, 110, 50)];
//conLabel.backgroundColor=[UIColor redColor];
conLabel.text=@"00:00.00";
conLabel.font=[UIFont fontWithName:nil size:25];
self.conLabel=conLabel;
[self.view addSubview:conLabel];
//秒表
UILabel * ctLabel=[[UILabel alloc]initWithFrame:CGRectMake(,110,kW,150)];
//ctLabel.backgroundColor=[UIColor redColor];
ctLabel.text=@"00:00.00";
ctLabel.textAlignment=NSTextAlignmentCenter;
ctLabel.font=[UIFont fontWithName:nil size:75];
self.ctLabel=ctLabel;
[self.view addSubview:ctLabel];
//下方視圖
UIView * bView=[[UIView alloc]initWithFrame:CGRectMake(,230,kW,140)];
bView.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
[self.view addSubview:bView];
//NSLog(@"%f",bView.frame.origin.y);
//開始停止按鈕
UIButton * ssButton=[[UIButton alloc]initWithFrame:CGRectMake((kW-200)/3, 20, 100, 100)];
ssButton.backgroundColor=[UIColor whiteColor];
ssButton.layer.cornerRadius=50;
[ssButton setTitle:@"開始" forState:UIControlStateNormal];
[ssButton setTitle:@"停止" forState:UIControlStateSelected];
[ssButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[ssButton setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
ssButton.tag=1;
[ssButton addTarget:self action:@selector(StartStop:) forControlEvents:UIControlEventTouchUpInside];
self.ssButton=ssButton;
[bView addSubview:ssButton];
//計次按鈕
UIButton * jcButton=[[UIButton alloc]initWithFrame:CGRectMake(((kW-200)/3)*2+100, 20, 100, 100)];
jcButton.backgroundColor=[UIColor whiteColor];
jcButton.layer.cornerRadius=50;
[jcButton setTitle:@"計次" forState:UIControlStateNormal];
[jcButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[jcButton addTarget:self action:@selector(CountNum) forControlEvents:UIControlEventTouchUpInside];
self.jcButton=jcButton;
[bView addSubview:jcButton];
//點擊計次按鈕時記錄的每次時間,存放到對應的cell上
UITableView * tableView=[[UITableView alloc]initWithFrame:CGRectMake(, 370, kW, kH-370-60) style:UITableViewStylePlain];
tableView.rowHeight=50;
tableView.delegate=self;
tableView.dataSource=self;
self.tableView=tableView;
[self.view addSubview:tableView];
}
#pragma mark - ssButton按鈕的點擊事件
- (void)StartStop:(UIButton *) button
{
button.selected = !button.selected;
if(_timer==nil)
{
//每隔0.01秒刷新一次頁面
_timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(runAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
NSLog(@"開始計時.....");
}
else
{
[_timer invalidate]; //讓定時器失效
_timer=nil;
_ctLabel.text=@"00:00.00";
_conLabel.text=@"00:00.00";
_seconds=;
self.cell=nil;
self.jcArray=nil;
[self.tableView reloadData];
NSLog(@"計時停止.....");
}
//方法二
/*
// if (button.selected==1)
// {
// NSLog(@"開始計時.....");
//
// }
// else
// {
// NSLog(@"計時停止.....");
//
// }
*/
}
#pragma mark - runAction
- (void) runAction
{
_seconds++;
//動態改變開始時間
NSString * startTime=[NSString stringWithFormat:@"%02li:%02li.%02li",_seconds/100/60%60,_seconds/100%60,_seconds%100];
_ctLabel.text=startTime;
}
#pragma mark - 計次
- (void)CountNum
{
count++;
_conLabel.text=_ctLabel.text;
// NSLog(@"這是記錄的第**** %i ****個時間數據: %@",count,_conLabel.text);
[self.jcArray addObject:_conLabel.text];
// NSLog(@"%@",self.jcArray);
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.jcArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * identy=@"JRTable";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
//增加label
// UILabel * cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, kW, tableView.rowHeight)];
// cellLabel.text=self.jcArray[indexPath.row];
// cellLabel.textAlignment=NSTextAlignmentCenter;
// [cell.contentView addSubview:cellLabel];
}
//NSLog(@"%@",self.jcArray[indexPath.row]);
cell.textLabel.text = self.jcArray[indexPath.row];
cell.textLabel.textAlignment=NSTextAlignmentCenter;
self.cell=cell;
return self.cell;
}
@end