更新答案
您需要使计时器无效以停止计数:
-(void)stopTimer
{
[self.timer invalidate];
self.timer = nil;
[UIView animateWithDuration:1.0 animations:^{
self.lblTimer.alpha = 0;
}];
}
这是创建 UITapGesture 的新 viewDidLoad() 方法。我不知道您的应用程序是如何工作的,但我会说您应该有一个停止记录按钮并将其连接到上述方法。我的新 viewDidLoad() 方法如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopTimer)];
tapGesture.numberOfTapsRequired = 2;
self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentCenter;
self.lblTimer.font = [UIFont systemFontOfSize:32];
self.lblTimer.userInteractionEnabled = YES;
[self.lblTimer addGestureRecognizer:tapGesture];
// set the starting total seconds
self.totalSeconds = 0;
[self setTimerTextWithTotalSeconds:self.totalSeconds];
[self.view addSubview:self.lblTimer];
// adding constraints
id views = @{@"lblTimer": self.lblTimer};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lblTimer]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lblTimer]|" options:0 metrics:nil views:views]];
// start the timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}
你有没有尝试过这样的事情:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UILabel *lblTimer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int totalSeconds;
@end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentCenter;
self.lblTimer.font = [UIFont systemFontOfSize:32];
// set the starting total seconds
self.totalSeconds = 0;
[self setTimerTextWithTotalSeconds:self.totalSeconds];
[self.view addSubview:self.lblTimer];
// adding constraints
id views = @{@"lblTimer": self.lblTimer};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lblTimer]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lblTimer]|" options:0 metrics:nil views:views]];
// start the timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}
-(void)setTimerTextWithTotalSeconds:(int)totalSeconds
{
int remainder = 0;
int hours = totalSeconds / 3600;
remainder = totalSeconds % 3600;
int minutes = remainder / 60;
remainder = remainder % 60;
int seconds = remainder;
NSString *strTotalTime = [[NSString alloc] initWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
self.lblTimer.text = strTotalTime;
}
// ----------------------------------------
// Adds 1 second to total seconds
// ----------------------------------------
-(void)updateTimer:(NSTimer *)timer
{
self.totalSeconds += 1;
[self setTimerTextWithTotalSeconds:self.totalSeconds];
}
您应该会看到如下内容: