在延迟不使用NSThread或sleep()之后执行方法 - 还有其他选择吗?

Sle*_*lee 17 iphone objective-c ipad ios

有没有办法在iOS4中使用除NSThread以外的其他东西延迟调用方法或使用sleep()阻止UI?

Mar*_*ves 29

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // code to be executed on main thread.If you want to run in another thread, create other queue
    });
Run Code Online (Sandbox Code Playgroud)


Mar*_*ell 9

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
Run Code Online (Sandbox Code Playgroud)

NSObject会为你做到这一点.

此外,您可以创建一个NSTimer并让它在给定的时间后对某个目标和选择器执行回调.