倒计时持续震动,结束时停止震动 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


@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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值