• NSTimer 滑动导致失效


    关键地方:

    1.封装一个NSTimer  作用:防止循环引用

    2.NStimer 在滑动时停止运行,

    解决方法:1.通过修改timer默认mode, NSRunLoopCommonModes(滑动时主线程会从NSDefaultRunLoopMode切换为UITrackingRunLoopMode,导致timer停止运行)

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    2.通过创建子线程。

    1. [NSThread detachNewThreadWithBlock:^{
    2. timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval
    3. target:timerTarget
    4. selector:@selector(timeAction:)
    5. userInfo:userInfo
    6. repeats:repeats];
    7. [[NSRunLoop currentRunLoop] addTimer:timerTarget.timer forMode:NSDefaultRunLoopMode];
    8. [[NSRunLoop currentRunLoop] run];
    9. }];

    封装NStimer:

    1. //
    2. // HSTimer.m
    3. // sad
    4. //
    5. // Created by xc on 2022/8/9.
    6. //
    7. #import "HSTimer.h"
    8. @interface HSTimerTarget : NSObject
    9. @property (nonatomic, weak) id target;
    10. @property (nonatomic, assign) SEL selector;
    11. @property (nonatomic, weak) NSTimer* timer;
    12. @end
    13. @implementation HSTimerTarget
    14. - (void)timeAction:(NSTimer *)timer {
    15. if(self.target) {
    16. [self.target performSelector:self.selector withObject:timer.userInfo afterDelay:0.0f];
    17. } else {
    18. [self.timer invalidate];
    19. }
    20. }
    21. @end
    22. @implementation HSTimer
    23. + (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval
    24. target:(id)aTarget
    25. selector:(SEL)aSelector
    26. userInfo:(id)userInfo
    27. repeats:(BOOL)repeats {
    28. HSTimerTarget* timerTarget = [[HSTimerTarget alloc] init];
    29. timerTarget.target = aTarget;
    30. timerTarget.selector = aSelector;
    31. [NSThread detachNewThreadWithBlock:^{
    32. timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval
    33. target:timerTarget
    34. selector:@selector(timeAction:)
    35. userInfo:userInfo
    36. repeats:repeats];
    37. [[NSRunLoop currentRunLoop] addTimer:timerTarget.timer forMode:NSDefaultRunLoopMode];
    38. [[NSRunLoop currentRunLoop] run];
    39. }];
    40. return timerTarget.timer;
    41. }
    42. @end
    1. //
    2. // HSTimer.h
    3. // sad
    4. //
    5. // Created by xc on 2022/8/9.
    6. //
    7. #import
    8. NS_ASSUME_NONNULL_BEGIN
    9. @interface HSTimer : NSObject
    10. + (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval
    11. target:(id)aTarget
    12. selector:(SEL)aSelector
    13. userInfo:(id)userInfo
    14. repeats:(BOOL)repeats;
    15. @end
    16. NS_ASSUME_NONNULL_END

    使用:

    注意点:在主线程刷新ui

    1. - (NSTimer *)timer {
    2. if (!_timer) {
    3. _timer =
    4. _timer = [HSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeEvent) userInfo:@{@"":@""} repeats:YES];
    5. }
    6. return _timer;
    7. }
    8. - (void)timeEvent {
    9. SK_WeakSelf(self)
    10. dispatch_async(dispatch_get_main_queue(), ^{
    11. weakself.count--;//时间减少
    12. weakself.lab.text = [NSString stringWithFormat:@"%ld:%02ld",weakself.count/60,weakself.count%60];
    13. if (weakself.count == 0) {
    14. //到达时间以后取消定时器
    15. weakself.lab.text = @"去领取";
    16. [weakself stopTimer];
    17. if (weakself.progressOver) {
    18. weakself.progressOver();
    19. }
    20. }
    21. if (weakself.count <= timeNum) {
    22. CGFloat prose = (CGFloat)weakself.count/timeNum;
    23. [weakself setProgress:prose];
    24. }
    25. [weakself setNeedsDisplay];
    26. });
    27. }

  • 相关阅读:
    51单片机mq3酒精浓度检测mq3酒驾醉驾检测酒精报警ADC0832采集
    springcloudalibaba-3
    Spring Boot应用程序安装为Unix、Linux、Windows操作系统服务
    Kotlin--1.基础语法
    内核中内存数据迁移速度对比
    .Net6 微服务之Ocelot+IdentityServer4入门看这篇就够了
    java计算机毕业设计网上汽车售票系统源代码+数据库+系统+lw文档
    Oracle之常用SQL语句
    C. Crossword Validation(字典树)
    代码随想录算法训练营第一天 |142.环形链表II
  • 原文地址:https://blog.csdn.net/Mayxc/article/details/127101609