Objective-C delay action with blocks

后端 未结 4 885
眼角桃花
眼角桃花 2020-12-08 00:12

I know that there are several ways of delaying an action in Objective-C like:

performSelector:withObject:afterDelay:

or using NSTimer

4条回答
  •  时光取名叫无心
    2020-12-08 00:47

    Here is how you can trigger a block after a delay in Swift:

    runThisAfterDelay(seconds: 4) { () -> () in
        print("Prints this 4 seconds later in main queue")
        // Or just call animatedMyObject() right here
    }
    
    /// EZSwiftExtensions
    func runThisAfterDelay(seconds seconds: Double, after: () -> ()) {
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
        dispatch_after(time, dispatch_get_main_queue(), after)
    }
    

    Its included as a standard function in my repo: https://github.com/goktugyil/EZSwiftExtensions

提交回复
热议问题