objective-c 取消执行的延迟函数

//延迟执行
[self performSelector:@selector(hidenSelf) withObject:nil afterDelay:2];

//取消延迟执行
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(hidenSelf) object:nil];

#import <Foundation/NSObject.h>
#import <Foundation/NSDate.h>
#import <CoreFoundation/CFRunLoop.h>

@class NSTimer, NSPort, NSArray;

FOUNDATION_EXPORT NSString * const NSDefaultRunLoopMode;
FOUNDATION_EXPORT NSString * const NSRunLoopCommonModes NS_AVAILABLE(10_5, 2_0);

@interface NSRunLoop : NSObject {
@private
    id          _rl;
    id          _dperf;
    id          _perft;
    id          _info;
    id        _ports;
    void    *_reserved[6];
}

+ (NSRunLoop *)currentRunLoop;
+ (NSRunLoop *)mainRunLoop NS_AVAILABLE(10_5, 2_0);

- (NSString *)currentMode;
- (CFRunLoopRef)getCFRunLoop;

- (void)addTimer:(NSTimer *)timer forMode:(NSString *)mode;

- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode;
- (void)removePort:(NSPort *)aPort forMode:(NSString *)mode;

- (NSDate *)limitDateForMode:(NSString *)mode;
- (void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)limitDate;

@end

@interface NSRunLoop (NSRunLoopConveniences)

- (void)run;
- (void)runUntilDate:(NSDate *)limitDate;
- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate;

#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
- (void)configureAsServer NS_DEPRECATED(10_0, 10_5, 2_0, 2_0);
#endif

@end

@interface NSObject (NSDelayedPerforming)

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument;
#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
#endif

@end

@interface NSRunLoop (NSOrderedPerform)

- (void)performSelector:(SEL)aSelector target:(id)target argument:(id)arg order:(NSUInteger)order modes:(NSArray *)modes;
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)arg;
#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
- (void)cancelPerformSelectorsWithTarget:(id)target;
#endif
@end

 
2011年11月24日
最近在做一个图片自动播放的功能,这些图片是通过url读取的;
最容易想到的就是当图片读取到后然后使用
[self performSelector:@selector(function) withObject:nil afterDelay:IntervalTime];
使用这种方法可以很顺利的去操作,但是当我用
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(function) object:nil];
取消自动播放的时候有一个弊病,图片还是会自动加载一张,然后才能停止。
所以上面这种方法取消自动播放的时候,performSelector依然会执行最后一次发出去的请求,导致会多加载一张图片;
还有一种方法就是就是使用NSTimer处理,但是不使用repeats功能,如果使用repeats=yes;那么图片会在时间内自动跳过正在加载的图片,这样也许还没有从网络上获取图片;
首先咱们定义一个成员变量NSTimer * m_autoPlayTimer;
执行的时候->

- (void)execute

{

if (m_autoPlayTimer) {

[m_autoPlayTimer invalidate];

m_autoPlayTimer = nil;

}

m_autoPlayTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:selfselector:@selector(autoNext) userInfo:nil repeats:NO];

[m_autoPlayTimer retain];

}

咱们的- (void)execute函数会被自动运行很多次,所以每次近来的时候需要将原来的对象给清空,NSTimer是一个自动释放的对象,一般都是在初始化的时候就[m_autoPlayTimer retain]下,存储在内存方便以后操作,invalidate之后就自动被释放了,再补一个m_autoPlayTimer = nil;所以这样内存中始终会存储一个m_autoPlayTimer的指针,

然后暂停自动播放的时候->

 

- (void)cancelExecute

{

if (m_autoPlayTimer != nil) {

[m_autoPlayTimer invalidate];

m_autoPlayTimer = nil;

}

}

判断当前是否存在m_autoPlayTimer,如果存在则销毁,这样m_autoPlayTimer就不会执行了,也不会出现把最后一次请求给送走的情况。

http://www.cocoachina.com/bbs/read.php?tid-21341-keyword-NStimer.html

 

posted @ 2016-03-27 15:36  YH_Coding  阅读(648)  评论(0编辑  收藏  举报