一、倒计时

iOS UIAlertController中加入倒计时,输入框,Swift讲解

@interface ViewController ()
{
    UIAlertController *alertview;
    NSString * message;
    NSTimer * waiTimer;
    NSInteger waitTctime;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    waitTctime = 15;
    message = @"用户向您发起了视频聊天 15";
    alertview =[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * refused = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDestructive handler:nil];
    UIAlertAction * accept = [UIAlertAction actionWithTitle:@"接受" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertview addAction:refused];
    [alertview addAction:accept];

       }

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
    waitTctime = 15;
    alertview.message = [NSString stringWithFormat:@"用户向您发起了视频聊天 15"];;
      [self presentViewController:alertview animated:YES completion:nil];
    if (!waiTimer) {
        waiTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tcWait:) userInfo:@{@"title":@"提示",@"message":message,@"stringtime":@"15"} repeats:YES];
    }

}

-(void)tcWait:(NSTimer *)timer{
    //参数字典
    NSDictionary *dic = timer.userInfo;
    if (waitTctime==0) {
        
        //退出当前警示
        [alertview dismissViewControllerAnimated:YES completion:nil];
        
        //销毁定时器
        if (waiTimer) {
            [waiTimer invalidate];
            waiTimer = nil;
        }
        
        
    }
    
    waitTctime --;
    if (waitTctime>=0) {
        
        
        alertview.message = [NSString stringWithFormat:@"用户向您发起了视频聊天 %02ld",waitTctime];;
        
        
    }
    
    
}




@end
倒计时alert

相关文章: