UI NSTimer

来源:互联网 发布:c语言输出文本文件 编辑:程序博客网 时间:2024/05/17 23:12
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        //    scheduledTimerWithTimeInterval:(NSTimeInterval)seconds//    预订一个Timer,设置一个时间间隔。//    表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1//    target:(id)aTarget//    表示发送的对象,如self//    selector:(SEL)aSelector//    方法选择器,在时间间隔内,选择调用一个实例方法//    userInfo:(id)userInfo//    此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。//    repeats:(BOOL)yesOrNo//    当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。//    invocation:(NSInvocation *)invocation        //    //关于定时器的以下信息//    Information About a Timer//    – isValid  //是否在运行//    – fireDate //Returns the date at which the receiver will fire.//    – setFireDate: //重新设置定时器开始运行的时间//    – timeInterval  //定时器延时时间//    – userInfo //其他信息    //    UIButton *startButton=[UIButton buttonWithType:UIButtonTypeSystem];//    startButton.frame=CGRectMake(50, 70, 100, 50);//    [startButton setTitle:@"开始" forState:UIControlStateNormal];//    [startButton addTarget:self action:@selector(startButtonAction:) forControlEvents:UIControlEventTouchUpInside];//    [self.view addSubview:startButton];//    //    UIButton *endButton=[UIButton buttonWithType:UIButtonTypeSystem];//    endButton.frame=CGRectMake(50, 150, 100, 50);//    [endButton setTitle:@"结束" forState:UIControlStateNormal];//    [endButton addTarget:self action:@selector(endButtonAction:) forControlEvents:UIControlEventTouchUpInside];//    [self.view addSubview:endButton];        }- (void)timerAction:(NSTimer*)timer{    NSLog(@"timerAction");}- (void)startButtonAction:(UIButton*)button{    //创建NSTimer对象    NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];    self.timer=timer;        }- (void)endButtonAction:(UIButton*)button{    if (_timer) {        [_timer invalidate];        _timer=nil;    }}-(void)dealloc{    [_timer release];    [_timeDisply release];    [super dealloc];}- (IBAction)startTime:(UIButton *)sender {        if (!_timer) {        _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimeDisplay) userInfo:nil repeats:YES];    }    }- (void)changeTimeAtTimeDisplay{    int second=[self.timeDisply.text intValue];    self.timeDisply.text=[NSString stringWithFormat:@"%d",second-1];}- (IBAction)stopTime:(UIButton *)sender {        if (_timer) {        NSLog(@"调用 self.time为真!!");                //如果定时器在运行        if ([self.timer isValid]) {                        NSLog(@"单击停止按钮,取消定时器!!");                        [self.timer invalidate];            //这行代码很关键            _timer=nil;        }            }}

0 0
原创粉丝点击