1. 程式人生 > >倒計時持續震動,結束時停止震動 iOS

倒計時持續震動,結束時停止震動 iOS

加入


//

//  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

@interfaceViewController ()<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.textisEqualToString:@""]) {

    }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