iOS 定时器

1NSTimer使用注意

1RunLoop的关系 

2)释放不掉的问题

http://blog.csdn.net/Christ_Beings/article/details/53425665


https://www.mgenware.com/blog/?p=459

 ***http://www.cocoachina.com/ios/20150710/12444.html


.h

//

//  HLWeakTimerTarget.h

//  ThreadTest

//

//  Created by Holly on 2017/12/4.

//  Copyright © 2017 Holly. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface HLWeakTimerTarget : NSObject


@property (nonatomic, weak) id target;

@property (nonatomic, assign) SEL selector;

@property (nonatomic, weak) NSTimer* timer;


+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval

                                      target:(id)aTarget

                                    selector:(SEL)aSelector

                                    userInfo:(id)userInfo

                                     repeats:(BOOL)repeats;

@end



.m

//

//  HLWeakTimerTarget.m

//  ThreadTest

//

//  Created by Holly on 2017/12/4.

//  Copyright © 2017 Holly. All rights reserved.

//


#import "HLWeakTimerTarget.h"


@implementation HLWeakTimerTarget


+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval

                                      target:(id)aTarget

                                    selector:(SEL)aSelector

                                    userInfo:(id)userInfo

                                     repeats:(BOOL)repeats {

    HLWeakTimerTarget* timerTarget = [[HLWeakTimerTarget alloc] init];

    timerTarget.target = aTarget;

    timerTarget.selector = aSelector;

//    通过这个方法把timer强引用的对象换为timerTarget

    timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval

                                                         target:timerTarget

                                                       selector:@selector(fire:)

                                                       userInfo:userInfo

                                                        repeats:repeats];

    return timerTarget.timer;

}


- (void) fire:(NSTimer *)timer {

    if(self.target) {

//        还是原来的target执行定时器的方法

        [self.target performSelector:self.selector withObject:nil];

    } else {

        [self.timer invalidate];

    }

}


@end


使用:

 NSTimer *timer = [HLWeakTimerTarget scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];


-(void)timerFire:(id)userinfo {

    NSLog(@"Fire--------%@",userinfo);

}

1NSTimer 误差

http://blog.csdn.net/y_csdnblog_xx/article/details/51538247


加入到RunLoopmode方式,我们刚才的NSTimer默认添加在NSDefaultRunLoopMode上,而UIScrollView在滑动的时候,RunLoop会自动切换到 UITrackingRunLoopModeNSTimer并没有添加到这个RunLoop模式上,自然也是不会启动的。所以,如果我们想要NSTimerUIScrollView滑动的时候也会启动的话,只要将NSTimer添加NSRunLoopCommonModes上即可。NSRunLoopCommonModesRunLoop模式的集合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值