内容简介:业务中有些场景需要做延时处理,一般采用的是
业务中有些场景需要做延时处理,一般采用的是 dispatch_after
这个 GCD 方法来实现,但是缺点是这种延时没办法取消,于是乎有了:
[self performSelector:@selector(myDelayedMethod) withObject: self afterDelay: desiredDelay]; [NSObject cancelPreviousPerformRequestsWithTarget: self selector:@selector(myDelayedMethod) object: self];
用了上面的方法,可以随时取消还没有执行的延时,但这个 API 只支持一个参数,有时候并不能满足我们的要求,网上搜索都提到了用 NSInvocation 或者 dispatch_after代替,但是这些方法都存在不能延时或者不能取消的问题,
于是试给 NSObject 扩展一下,让其支持多个参数,并支持cancelPreviousPerformRequestsWithTarget
取消,原理其实很简单,直接贴代码:
NSObject+Additions.h
@interface NSObject (Additions) - (void)performSelector:(SEL)aSelector withObject:(id)argument0 withObject:(id)argument1 afterDelay:(NSTimeInterval)delay; - (void)performSelector:(SEL)aSelector withObject:(id)argument0 withObject:(id)argument1 afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes; + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)argument0 object:(id)argument1; @end
NSObject+Additions.m
@implementation NSObject (Additions) - (void)performSelector:(SEL)aSelector withObject:(id)argument0 withObject:(id)argument1 afterDelay:(NSTimeInterval)delay { [self performSelector:aSelector withObject:argument0 withObject:argument1 afterDelay:delay inModes:@[NSDefaultRunLoopMode]]; } - (void)performSelector:(SEL)aSelector withObject:(id)argument0 withObject:(id)argument1 afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes { NSArray *arguments = [NSArray arrayWithObjects:NSStringFromSelector(aSelector), argument0, argument1, nil]; [self performSelector:@selector(_performWithArguments:) withObject:arguments afterDelay:delay inModes:modes]; } + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)argument0 object:(id)argument1 { NSArray *arguments = [NSArray arrayWithObjects:NSStringFromSelector(aSelector), argument0, argument1, nil]; [self cancelPreviousPerformRequestsWithTarget:aTarget selector:@selector(_performWithArguments:) object:arguments]; } - (void)_performWithArguments:(NSArray *)arguments { SEL aSelector = NSSelectorFromString([arguments firstObject]); NSMethodSignature *signature = [self methodSignatureForSelector:aSelector]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setTarget:self]; [invocation setSelector:aSelector]; for (NSUInteger i = 1; i < arguments.count; i++) { id obj = arguments[i]; [invocation setArgument:&obj atIndex:i + 1]; } [invocation invoke]; } @end
以上所述就是小编给大家介绍的《让 performSelector:withObject:afterDelay: 支持多个参数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何一条Mediainfo --Inform语句同时获取视频参数和音频参数多个Parameters
- 关于Vuex的action传入多个参数的问题
- Rust 1.36.0 发布,dbg! 宏现在支持多个参数
- 如何在JPQL查询中执行具有多个参数的SQL函数?
- node.js将回调函数嵌套,用promise改造成async/await顺序执行:异常处理和返回多个参数
- asp.net-mvc – ASP.NET MVC – 将多个查询字符串参数映射到一个漂亮的URL
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。