大体思路:
1、定义一个定时器,负责时间的变化,两个按钮,一个响应按钮,和一个显示按钮,一个计时变量。
2、创建响应按钮,显示在视图上,当点击它时,创建显示按钮,显示在响应按钮的位置覆盖它,并开启定时器。
3、定时器负责每秒计时变量减1,然后将新的计时变量值显示在显示按钮上,终止条件是当计时变量小于0时,在视图上移除显示按钮,并关闭定时器。
前面的变量定义和调用方法,我就不放上去了(^__^)
#pragma mark - 创建"发送短信"的按钮
- (void) createButton {
//1.初始化
_button = [UIButton buttonWithType:UIButtonTypeCustom];
//2.设置frame
_button.frame = CGRectMake(250, 50, 80, 30);
//3.设置背景颜色
_button.backgroundColor = [UIColor orangeColor];
//4.设置文字颜色
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//5.设置点击圆角效果
_button.layer.cornerRadius = 5;
//6.设置文字内容
[_button setTitle:@"发送短信" forState:UIControlStateNormal];
//7.设置点击事件
[_button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
//8.添加到视图
[self.view addSubview:_button];
}
#pragma mark - 按钮点击事件
- (void) clickAction:(UIButton *) button {
//1.初始化计时变量
_count = 10;
//------创建显示按钮
//1.初始化
showButton = [UIButton buttonWithType:UIButtonTypeCustom];
//2.设置frame
showButton.frame = CGRectMake(250, 50, 130, 30);
//3.设置背景颜色
showButton.backgroundColor = [UIColor orangeColor];
//4.设置文字颜色
[showButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//5.设置点击圆角效果
showButton.layer.cornerRadius = 5;
//6.设置文字内容
[showButton setTitle:[NSString stringWithFormat:@"%ld正在发送....",_count] forState:UIControlStateNormal];
//7.添加到视图
[self.view addSubview:showButton];
//3.初始化定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
#pragma mark - 定时器方法
- (void) timerAction:(NSTimer *) time {
//1.计时变量减1
_count--;
//2.改变按钮内容
[showButton setTitle:[NSString stringWithFormat:@"%ld正在发送...",_count] forState:UIControlStateNormal];
//3.判断
if (_count < 0) {
//移除显示按钮
[showButton removeFromSuperview];
//关闭定时器
[_timer invalidate];
_timer = nil;
}
}
我在storyboard里面手动添加了一个输入框,点击输入后无法收起键盘,在网上找了一个方法,可以实现收起键盘
#pragma mark - 点击视图,收起键盘
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}