OStack程序员社区-中国程序员成长平台

标题: ios - NSTimer 倒计时无法正常工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:35
标题: ios - NSTimer 倒计时无法正常工作

我正在尝试区分时间并在 ui 标签中向用户显示倒数计时器

NSTimer的声明

@property (strong, nonatomic)NSTimer *timer;

这是我的计时器,确实加载了

_timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selectorselector(updateCountdown userInfo:nil repeats: YES];

这是我的 updateCountdown 方法

-(void) updateCountdownint)secondsLeft {
    int hours, minutes, seconds;

    secondsLeft--;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;
    _countDownlabel.text = [self timeFormatted:secondsLeft];///[NSString stringWithFormat"%02d:%02d:%02d", hours, minutes, seconds];
    NSLog(@"%@",[NSString stringWithFormat"%02d:%02d:%02d", hours, minutes, seconds]);
    if ( secondsLeft == 0 ) {
        [_timer invalidate];
      }
    }

我的日志详细信息是

2017-06-30 09:49:34.070 Barebones[845:56963] requestReply cust liqour category: {
date = "30-6-2017";
"end_time" = "11:41";
"market_crash" = true;
"start_time" = "09:41";
}
2017-06-30 09:49:34.070 Barebones[845:56963] true
2017-06-30 09:49:34.070 Barebones[845:56963] 09:41
2017-06-30 09:49:34.070 Barebones[845:56963] 11:41
2017-06-30 09:49:34.070 Barebones[845:56963] 30-6-2017
2017-06-30 09:49:34.073 Barebones[845:56963] 2016-12-25 08:58:00 +0000
2017-06-30 09:49:34.073 Barebones[845:56963] 2016-12-25 12:15:00 +0000
2017-06-30 09:49:34.073 Barebones[845:56963] 197.000000 is the time  difference
2017-06-30 09:49:34.073 Barebones[845:56963] 00:03:17
2017-06-30 09:49:35.075 Barebones[845:56963] 991:05:35
2017-06-30 09:49:36.075 Barebones[845:56963] 991:05:35
2017-06-30 09:49:37.075 Barebones[845:56963] 991:05:35
2017-06-30 09:49:38.075 Barebones[845:56963] 991:05:35

这个值继续执行

目标:- 倒计时到零并停止计时器实际上一旦倒计时结束我将隐藏标签

更新:-

 int secondsLeft=[self timeFormatted:[date2 timeIntervalSinceDate:date1]/60];

上面的定时器已经初始化了

这是我更新的计时器:-

int secondsLeft=[date2 timeIntervalSinceDate:date1]/60;

NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:secondsLeft], @"cID", nil];
_timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selectorselector(updateCountdown userInfo:userInfo repeats: YES];

这是我更新的计时器方法:-

- (void)updateCountdownNSTimer *)timer{
int hours, minutes, seconds;
NSDictionary *userInfo = [timer userInfo];
int secondsLeft = [[userInfo objectForKey"cID"] intValue];
secondsLeft--;

hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
_countDownlabel.text = [self timeFormatted:secondsLeft];///[NSString stringWithFormat"%02d:%02d:%02d", hours, minutes, seconds];
NSLog(@"%@",[NSString stringWithFormat"%02d:%02d:%02d", hours, minutes, seconds]);
if ( secondsLeft == 0 ) {
    [_timer invalidate];
  }
}



Best Answer-推荐答案


几件事。

  1. 正如 vadian 所说,如果您使用基于选择器的计时器,则计时器处理函数采用单个参数,该参数是对计时器本身的引用。如果你想跟踪倒计时,你可以定义属性来跟踪它:

    @interface ViewController ()
    
    @property (nonatomic, weak) IBOutlet UILabel *label;
    @property (nonatomic, weak) NSTimer *timer;
    @property (nonatomic, strong) NSDateComponentsFormatter *formatter;
    @property (nonatomic, strong) NSDate *stopTime;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.formatter = [[NSDateComponentsFormatter alloc] init];
        self.formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        self.formatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
        self.formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
    
        self.stopTime = [[NSDate date] dateByAddingTimeInterval:5 * 60];   // in 5 minutes, for example
    
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selectorselector(handleTimer userInfo:nil repeats:true];
    
        [self.timer fire];    // don't wait one second before firing the first time; fire now
    }
    
    - (void)handleTimerNSTimer *)timer {
        NSDate *now = [NSDate date];
        if ([now compare:self.stopTime] == NSOrderedDescending) {
            // do whatever you want when timer stops
    
            [timer invalidate];
            return;
        }
    
        self.label.text = [self.formatter stringFromDate:now toDate:self.stopTime];
    }
    
    // Note, when the view disappears, invalidate the timer so the timer doesn't
    // keep strong reference to the view controller. Note that in this selector-based
    // pattern, I can't attempt to do this in `dealloc`, because the scheduled timer
    // will keep a strong reference, preventing `dealloc` from getting called. So do 
    // this in `viewDidDisappear`.
    
    - (void)viewDidDisappearBOOL)animated {
        [self.timer invalidate];
    }
    
    @end
    
  2. 如果只支持 iOS 10 及更高版本,我建议使用完成 block 计时器,因为它可以进一步简化流程:

    @interface ViewController ()
    
    @property (nonatomic, weak) IBOutlet UILabel *label;
    @property (nonatomic, weak) NSTimer *timer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
        formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        formatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
        formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
    
        NSDate *stopTime = [[NSDate date] dateByAddingTimeInterval:5 * 60];  // in 5 minutes, for example
        typeof(self) __weak weakSelf = self; // make sure to not reference `self` in block below, but only reference `weakSelf` to avoid timer from maintaining strong reference
    
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:true block:^(NSTimer * _Nonnull timer) {
            NSDate *now = [NSDate date];
            if ([now compare:stopTime] == NSOrderedDescending) {
                // do whatever you want when timer stops
    
                [timer invalidate];
                return;
            }
    
            weakSelf.label.text = [formatter stringFromDate:now toDate:stopTime];
        }];
    
        [self.timer fire];    // don't wait one second before firing the first time; fire now
    }
    
    // Because I was careful to not reference `self` inside the above block, 
    // this block-based timer will NOT keep a strong reference to the view
    // controller. Nonetheless, when the view controller is dismissed, I want
    // to stop the timer, to avoid wasting CPU cycles on a timer that isn't
    // needed anymore.
    
    - (void)dealloc {
        [self.timer invalidate];
    }
    
    @end
    

    显然,如果您还需要支持 iOS 9 及更早版本,则必须使用上述基于选择器的解决方案。

  3. 我建议不要依赖定时器来调整时间,因为你不能保证定时器会以你想要的频率被调用。在上面的两个例子中,我捕捉到我倒计时的时间,并且只显示“现在”和预定的“停止时间”之间的时间量。

  4. 注意,我还建议您不要自己构建格式字符串。有一个方便的 NSDateComponentsFormatter 可以为你格式化。如果可以,请使用它。

  5. 您已将计时器引用 strong。您可以将其设置为 weak,因为计划的计时器在 invalidated 之前不会被释放。一旦它被invalidated,它可以很方便地自动为你解除分配。

关于ios - NSTimer 倒计时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838584/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4