首页 文章

如何将方法调用延迟1秒?

提问于
浏览
159

是否有一种简单的方法可以延迟方法调用1秒钟?

我有 UIImageView 对触摸事件做出反应 . 检测到触摸时,应用程序中会出现一些动画 . 一秒钟后,我想调用另一种方法 . 在这种情况下,我无法使用 animationDidStop 选择器 .

11 回答

  • 125
    performSelector:withObject:afterDelay:
    

    Document Reference

  • 22

    你也可以使用一个块

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [object method]; 
    });
    

    大多数情况下,您会想要使用dispatch_get_main_queue,但如果方法中没有UI,您可以使用global queue .

    Edit:

    Swift 3版本:

    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        object.method()
    }
    

    同样, DispatchQueue.global().asyncAfter(... 也可能是一个不错的选择 .

  • 17

    最好的方法是:

    [self performSelector:@selector(YourFunctionName) 
               withObject:(can be Self or Object from other Classes) 
               afterDelay:(Time Of Delay)];
    

    你也可以传递nil作为withObject参数 .

    例子:

    [self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];
    
  • -35

    已经有很多答案,它们都是正确的 . 如果你想使用 dispatch_after ,你应该寻找包含在右下方 Code Snippet Library 内的片段(你可以选择 UI 元素) .

    enter image description here

    因此,您只需要在代码中编写 dispatch 来调用此代码段:

    enter image description here

  • 251

    您可以使用执行选择器为0.1秒延迟方法调用以下代码后执行此操作 .

    [self performSelector:@selector(InsertView)  withObject:nil afterDelay:0.1];
    
  • 8

    你也可以:

    [UIView animateWithDuration:1.0
                     animations:^{ self.view.alpha = 1.1; /* Some fake chages */ }
                     completion:^(BOOL finished)
    {
        NSLog(@"A second lapsed.");
    }];
    

    在这种情况下,您必须对某些视图进行一些更改才能使动画生效 . 这确实是hacky,但我喜欢基于块的东西 . 或者在下面结束@mcfedr的答案 .


    waitFor(1.0, ^
    {
        NSLog(@"A second lapsed");
    });
    

    typedef void (^WaitCompletionBlock)();
    void waitFor(NSTimeInterval duration, WaitCompletionBlock completion)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC),
                       dispatch_get_main_queue(), ^
        { completion(); });
    }
    
  • 0

    你可以这样做

    [self performSelector:@selector(MethodToExecute) withObject:nil afterDelay:1.0 ];
    
  • 1

    Swift 2.x

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
     dispatch_after(delayTime, dispatch_get_main_queue()) {
     print("do some work")
    }
    

    Swift 3.x --&-- Swift 4

    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        print("do some work")
    }
    
  • 7

    已检查的解决方案中有一个Swift 3解决方案:

    self.perform(#selector(self.targetMethod), with: self, afterDelay: 1.0)
    

    并且有方法

    @objc fileprivate func targetMethod(){
    
    }
    
  • 0

    在Swift 3中使用

    perform(<Selector>, with: <object>, afterDelay: <Time in Seconds>)
    
  • 185

    NOTE: this will pause your whole thread, not just the one method.
    在调用方法之前调用sleep / wait / halt 1000毫秒?

    Sleep(1000); // does nothing the next 1000 mSek
    
    Methodcall(params); // now do the real thing
    

    编辑:上述答案适用于一般问题“如何延迟方法调用1秒?”,这是答案时提出的问题(实际上答案是在原始问题的7分钟内给出的: - )) . 当时没有给出关于语言的信息,所以请停止讨论使用睡眠的正确方法我XCode缺少类...

相关问题