前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NSTimer 滑动导致失效

NSTimer 滑动导致失效

作者头像
菜菜不吃蔡
发布2022-09-30 08:06:12
2750
发布2022-09-30 08:06:12
举报
文章被收录于专栏:编程语言

关键地方:

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

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

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

代码语言:javascript
复制
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

2.通过创建子线程。

代码语言:javascript
复制
[NSThread detachNewThreadWithBlock:^{
        timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                             target:timerTarget
                                                           selector:@selector(timeAction:)
                                                           userInfo:userInfo
                                                            repeats:repeats];
        [[NSRunLoop currentRunLoop] addTimer:timerTarget.timer forMode:NSDefaultRunLoopMode];
        
        [[NSRunLoop currentRunLoop] run];
    }];

封装NStimer:

代码语言:javascript
复制
//
//  HSTimer.m
//  sad
//
//  Created by xc on 2022/8/9.
//

#import "HSTimer.h"

@interface HSTimerTarget : NSObject
@property (nonatomic, weak) id target;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, weak) NSTimer* timer;
@end

@implementation HSTimerTarget
- (void)timeAction:(NSTimer *)timer {
    if(self.target) {
        [self.target performSelector:self.selector withObject:timer.userInfo afterDelay:0.0f];
    } else {
        [self.timer invalidate];
    }
}
@end

@implementation HSTimer
+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                      target:(id)aTarget
                                    selector:(SEL)aSelector
                                    userInfo:(id)userInfo
                                     repeats:(BOOL)repeats {
    HSTimerTarget* timerTarget = [[HSTimerTarget alloc] init];
    timerTarget.target = aTarget;
    timerTarget.selector = aSelector;
   
    [NSThread detachNewThreadWithBlock:^{
        timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                             target:timerTarget
                                                           selector:@selector(timeAction:)
                                                           userInfo:userInfo
                                                            repeats:repeats];
        [[NSRunLoop currentRunLoop] addTimer:timerTarget.timer forMode:NSDefaultRunLoopMode];
        
        [[NSRunLoop currentRunLoop] run];
    }];
    return timerTarget.timer;
}

@end
代码语言:javascript
复制
//
//  HSTimer.h
//  sad
//
//  Created by xc on 2022/8/9.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface HSTimer : NSObject

+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                      target:(id)aTarget
                                    selector:(SEL)aSelector
                                    userInfo:(id)userInfo
                                     repeats:(BOOL)repeats;
@end

NS_ASSUME_NONNULL_END

使用:

注意点:在主线程刷新ui

代码语言:javascript
复制
- (NSTimer *)timer {
    if (!_timer) {
        _timer =
        _timer = [HSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeEvent) userInfo:@{@"":@""} repeats:YES];
    }
    return _timer;
}


- (void)timeEvent {
    SK_WeakSelf(self)
    dispatch_async(dispatch_get_main_queue(), ^{
        weakself.count--;//时间减少
        weakself.lab.text = [NSString stringWithFormat:@"%ld:%02ld",weakself.count/60,weakself.count%60];
        if (weakself.count == 0) {
            //到达时间以后取消定时器
            weakself.lab.text = @"去领取";
            [weakself stopTimer];
            if (weakself.progressOver) {
                weakself.progressOver();
            }
        }
        if (weakself.count <= timeNum) {
            CGFloat prose = (CGFloat)weakself.count/timeNum;
            
            [weakself setProgress:prose];
            
        }
        
        [weakself setNeedsDisplay];
    });
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档