加入
//
// ViewController.m
// 进度显示
//
// Created by beevle on 16/1/12.
// Copyright © 2016年 beevle. All rights reserved.
//
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#define WW_WIDTH [UIScreen mainScreen].bounds.size.width
#define WW_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
{
NSTimer * _timer1;//定时器
UITextField * _TField;//输入时间
int _times;//判断是否正在倒计时
}
//判断是否正在倒计时
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
if (_timers!=0) {
NSLog(@"执行中--%d",_timers);
}
}
- (void)viewDidLoad {
[superviewDidLoad];
//创建进度条
[selfcreateView];
[selfcreateTF];
_timers =0;
[ [ UIApplicationsharedApplication] setIdleTimerDisabled:YES ] ;
}
//震动回掉
void soundCompleteCallback(SystemSoundID sound,void * clientData) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //震动
}
//进度显示
-(void)createView {
for (int i=0; i<20; i++) {
UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake((WW_WIDTH-300)/2+i*15,100, 10,10)];
label.backgroundColor = [UIColorgrayColor];
label.layer.cornerRadius =5;
label.layer.masksToBounds =YES;
label.tag =100 + i ;
[self.viewaddSubview:label];
}
}
//输入时间,点击开始倒计时
-(void)createTF{
_TField = [[UITextFieldalloc]initWithFrame:CGRectMake(50,200, 150,40)];
_TField.borderStyle =UITextBorderStyleRoundedRect;
_TField.backgroundColor = [UIColorcolorWithRed:0.9green:0.9blue:0.9alpha:1];
_TField.placeholder =@"输入时间(秒为单位)";
_TField.font = [UIFontsystemFontOfSize:13];
_TField.clearButtonMode =UITextFieldViewModeWhileEditing;
_TField.keyboardType =UIKeyboardTypeNumberPad;//数字键盘
[self.viewaddSubview:_TField];
UIButton * btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.backgroundColor= [UIColorgrayColor];
btn.frame =CGRectMake(WW_WIDTH*0.75,200, 50,40);
[btn setTitle:@"开始"forState:(UIControlStateNormal)];
[btn addTarget:selfaction:@selector(btnClick)forControlEvents:(UIControlEventTouchUpInside)];
[self.viewaddSubview:btn];
UIButton * btn2 = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn2.backgroundColor= [UIColorgrayColor];
btn2.frame =CGRectMake(WW_WIDTH*0.75,250, 50,40);
[btn2 setTitle:@"重置"forState:(UIControlStateNormal)];
[btn2 addTarget:selfaction:@selector(replaceall)forControlEvents:(UIControlEventTouchUpInside)];
[self.viewaddSubview:btn2];
}
//重置
-(void)replaceall{
if (_timers!=0) {
//重置所有颜色
for (int i =0 ; i<20; i++) {
UILabel * label= (UILabel * )[self.viewviewWithTag:i+100];
label.backgroundColor = [UIColorgrayColor];
}
// 销毁定时器
[_timer1invalidate];
_timer1=nil;
// 重置计时
_timers=0;
}
//移除震动
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
//清空输入
_TField.text =@"";
}
//点击开始
-(void)btnClick {
//判断输入是否为空
if ([_TField.text isEqualToString:@""]) {
}else{
int total =_TField.text.intValue;
_timer1 = [NSTimerscheduledTimerWithTimeInterval:total/20.0target:selfselector:@selector(checkChange)userInfo:nilrepeats:YES];
//调用震动
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,NULL, NULL,soundCompleteCallback,NULL);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//添加第二个震动
NSTimer * timerS = [NSTimerscheduledTimerWithTimeInterval:0.1target:selfselector:@selector(addsecond)userInfo:nilrepeats:NO];
}
}
//第二个震动
-(void)addsecond{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,NULL, NULL,soundCompleteCallback,NULL);
}
//改变
-(void)checkChange{
UILabel * label= (UILabel * )[self.viewviewWithTag:100+_timers];
label.backgroundColor = [UIColorgreenColor];
NSLog(@"%d",_timers);
_timers++;
if (_timers>20) {
//添加弹窗
// UIAlertController*alertController= [UIAlertController alertControllerWithTitle:@"标题" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"时间到" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
//
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
// //UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
// [alert addAction:cancelAction];
//
// [self presentViewController:alert animated:YES completion:nil];
//添加弹窗
UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:@"时间到"message:nildelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alert show];
[_timer1invalidate];
_timer1=nil;
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
}
}
//弹窗点击回调重置
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
[selfreplaceall];
}
}
//收键盘
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_TFieldresignFirstResponder ];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end