使用UIButton添加和减少计时器的时间

时间:2014-10-08 15:48:42

标签: ios objective-c uibutton nstimer

大家好,我有一个问题,如何添加和减少UIButton计时器的时间,例如,如果我有一个带减号和加号的按钮以及定时器的时间。

谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用以下方法为NSTimer添加时间:

[yourTimer setFireDate:[[yourTimer fireDate] dateByAddingTimeInterval:secondsToAdd]];

您可以使用以下方式缩短NSTimer的时间:

[yourTimer setFireDate:[[yourTimer fireDate] dateByAddingTimeInterval:-secondsToDecrease]];

希望这有帮助!

答案 1 :(得分:2)

-(void)viewDidLoad{
  UIButton *buttonPlus = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  UIButton *buttonMinus = [UIButton buttonWithType:UIButtonTypeRoundedRect];

 [buttonPlus addTarget:self action:@selector(addTime) forControlEvents:UIControlEventTouchUpInside];
 [buttonMinus addTarget:self action:@selector(removeTime) forControlEvents:UIControlEventTouchUpInside];

 [self.view addSubview:buttonMinus];
 [self.view addSubview:buttonPlus];

}

-(void) addTime {
    [yourTimer setFireDate:[[yourTimer fireDate] dateByAddingTimeInterval:secondsToAdd]];
}

-(void) removeTime {
    [yourTimer setFireDate:[[yourTimer fireDate] dateByAddingTimeInterval:-secondsToAdd]];
}