NStimer 倒计时实现
来源:互联网 发布:河南郑州网络诈骗6.29 编辑:程序博客网 时间:2024/06/17 12:21
1、思路:当点击UIswich控件的开启功能时,再开始执行一个倒计时功能的实现,关闭了UIswich控件,则不再执行倒计时功能。
2、代码实现:
//// ViewController.m// NStimer//// Created by rimi on 15/11/11.// Copyright © 2015年 Liu. All rights reserved.//#import "ViewController.h"@interface ViewController (){ int secondsCountDown;//倒计时总时长 NSTimer *countDownTimer;//计时器 UILabel *labelText;//显示秒数}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; labelText = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 30)]; [self.view addSubview:labelText]; UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 300, 120, 30)]; [theSwitch addTarget:self action:@selector(switchToChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:theSwitch]; secondsCountDown = 10;//设置秒数}- (void)timeFireMethod{ //倒计时减1 secondsCountDown--; //修改倒计时便签显示内容 labelText.text = [NSString stringWithFormat:@"%d",secondsCountDown]; //当倒计时到0时,打印倒倒计时结束 if (secondsCountDown == 0) { [countDownTimer invalidate];/**< 时间无效 */ [labelText removeFromSuperview];/**< 移除显示 */ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您应该记账了!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; // NSLog(@"倒计时结束"); }}//监听UISwitch事件- (void)switchToChange:(UISwitch*)sender{ if (sender.isOn) { countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; labelText.text = [NSString stringWithFormat:@"%d",secondsCountDown]; secondsCountDown = 10; [self timeFireMethod]; [self.view addSubview:labelText]; } else { [countDownTimer invalidate]; [labelText removeFromSuperview]; NSLog(@"已关闭计时"); }}@end
0 0
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- 使用NSTimer实现倒计时
- iOS NSTimer 实现倒计时
- 使用NSTimer实现倒计时
- NStimer 倒计时实现
- NSTimer+倒计时功能实现
- NSTimer+倒计时功能实现
- iphone--使用NSTimer实现倒计时
- NSTimer 倒计时
- NSTimer倒计时
- NSTimer实现读秒、倒计时等周期性操作
- NSTimer实现倒计时 及 定时器用法
- 使用NSTimer和gcb实现倒计时
- 用NSTimer实现倒计时--ios开发
- NSTimer知识点和倒计时的实现
- LeetCode:Rotate Array
- 数据库分片(Sharding)与分区(Partition)的区别
- C#开发学习(Visual Studio的安装与使用)
- matlab学习笔记(十五)---综合实例
- linux上很方便的上传下载文件工具rz和sz
- NStimer 倒计时实现
- 【分享】一个语音Java SDK使用的常见问题解答
- 第十五节 BLE蓝牙4.0协议栈启动分析
- 用jconsole可视化JVM的运行状态
- 优化算法-梯度下降法
- C++:使用vector的元素
- Swift基础笔记11/27
- bootstrap 左侧横向选项卡
- iOS 百度地图-绘制行车路径