4 after swift_swift after - CSDN
  • dispatch_afterswift 3 版本

    千次阅读 2017-05-02 09:54:16
    dispatch_afterswift 3 版本,百度找了半天都是旧版本的,上stackover flow才找到新版本..分亨出来. 如果不需要,在主线程执行,就去掉main DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code...
    
    
    dispatch_after 的swift 3 版本,百度找了半天都是旧版本的,上stackover flow才找到新版本..分亨出来.
    如果不需要在主线程执行,就去掉main 
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        // your code here
    }
    展开全文
  • Swift 延迟执行dispatch_after

    千次阅读 2018-03-31 09:36:04
    OC dispatch_after__weak typeof(self) weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ { __strong typeof(self) pThis ...

    OC dispatch_after

    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
    {
        __strong typeof(self) pThis = weakSelf;
        [pThis.backgroundView removeFromSuperview];
        pThis.backgroundView = nil;
    });
    

    Swift DispatchQueue.main.asyncAfte

    DispatchQueue.main.asyncAfter(deadline: .now()+0.5, execute: 
    {
        self.backgroundView()?.removeFromSuperview()
        self.setBackgroundView(backgroundView: nil)
    })
    


    强烈推荐:超简单!!! iOS设置状态栏、导航栏按钮、标题、颜色、透明度,偏移等

    https://github.com/wangrui460/WRNavigationBar
    https://github.com/wangrui460/WRNavigationBar_swift



    作者:韦德460
    链接:https://www.jianshu.com/p/499ec675239e
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    展开全文
  • I've gone through the iBook from Apple, and couldn't find any definition of it: 我从Apple那里浏览了iBook ,

    本文翻译自:dispatch_after - GCD in Swift?

    I've gone through the iBook from Apple, and couldn't find any definition of it: 我从Apple那里浏览了iBook ,找不到任何定义:

    Can someone explain the structure of dispatch_after ? 有人可以解释dispatch_after的结构吗?

    dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>)
    

    #1楼

    参考:https://stackoom.com/question/1cqTw/dispatch-after-Swift中的GCD


    #2楼

    A clearer idea of the structure: 更清楚的结构:

    dispatch_after(when: dispatch_time_t, queue: dispatch_queue_t, block: dispatch_block_t?)
    

    dispatch_time_t is a UInt64 . dispatch_time_t是一个UInt64 The dispatch_queue_t is actually type aliased to an NSObject , but you should just use your familiar GCD methods to get queues. dispatch_queue_t实际上是NSObject别名,但您应该使用熟悉的GCD方法来获取队列。 The block is a Swift closure. 该块是一个Swift闭包。 Specifically, dispatch_block_t is defined as () -> Void , which is equivalent to () -> () . 具体来说, dispatch_block_t定义为() -> Void ,相当于() -> ()

    Example usage: 用法示例:

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

    EDIT: 编辑:

    I recommend using @matt's really nice delay function . 我建议使用@ matt的非常好的delay功能

    EDIT 2: 编辑2:

    In Swift 3, there will be new wrappers for GCD. 在Swift 3中,将有新的GCD包装器。 See here: https://github.com/apple/swift-evolution/blob/master/proposals/0088-libdispatch-for-swift3.md 请参阅: https//github.com/apple/swift-evolution/blob/master/proposals/0088-libdispatch-for-swift3.md

    The original example would be written as follows in Swift 3: 原始示例将在Swift 3中编写如下:

    let deadlineTime = DispatchTime.now() + .seconds(1)
    DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
        print("test")
    }
    

    Note that you can write the deadlineTime declaration as DispatchTime.now() + 1.0 and get the same result because the + operator is overridden as follows (similarly for - ): 请注意,您可以将deadlineTime声明写为DispatchTime.now() + 1.0并获得相同的结果,因为+运算符被覆盖如下(类似于- ):

    • func +(time: DispatchTime, seconds: Double) -> DispatchTime
    • func +(time: DispatchWalltime, interval: DispatchTimeInterval) -> DispatchWalltime

    This means that if you don't use the DispatchTimeInterval enum and just write a number, it is assumed that you are using seconds. 这意味着如果您不使用DispatchTimeInterval enum并只写一个数字,则假定您使用秒。


    #3楼

    Swift 3+ Swift 3+

    This is super-easy and elegant in Swift 3+: 这在Swift 3+中非常简单和优雅:

    DispatchQueue.main.asyncAfter(deadline: .now() + 4.5) {
        // ...
    }
    

    Older Answer: 旧答案:

    To expand on Cezary's answer, which will execute after 1 nanosecond, I had to do the following to execute after 4 and a half seconds. 为了扩展Cezary的答案,它将在1纳秒后执行,我必须在4秒半后执行以下操作。

    let delay = 4.5 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), block)
    

    Edit: I discovered that my original code was slightly wrong. 编辑:我发现我的原始代码有点错误。 Implicit typing causes a compile error if you don't cast NSEC_PER_SEC to a Double. 如果不将NSEC_PER_SEC转换为Double,则隐式类型会导致编译错误。

    If anyone can suggest a more optimal solution I'd be keen to hear it. 如果有人能提出更优化的解决方案,我会热衷于听到它。


    #4楼

    I use dispatch_after so often that I wrote a top-level utility function to make the syntax simpler: 我经常使用dispatch_after因此我编写了一个顶级实用程序函数来使语法更简单:

    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
    

    And now you can talk like this: 现在你可以这样说:

    delay(0.4) {
        // do stuff
    }
    

    Wow, a language where you can improve the language. 哇,一种你可以改善语言的语言。 What could be better? 还有什么比这更好的?


    Update for Swift 3, Xcode 8 Seed 6 更新Swift 3,Xcode 8 Seed 6

    Seems almost not worth bothering with, now that they've improved the calling syntax: 似乎几乎不值得打扰,现在他们已经改进了调用语法:

    func delay(_ delay:Double, closure:@escaping ()->()) {
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
    }
    

    #5楼

    matt's syntax is very nice and if you need to invalidate the block, you may want to use this : matt的语法非常好,如果你需要使块无效,你可能想要使用它:

    typealias dispatch_cancelable_closure = (cancel : Bool) -> Void
    
    func delay(time:NSTimeInterval, closure:()->Void) ->  dispatch_cancelable_closure? {
    
        func dispatch_later(clsr:()->Void) {
            dispatch_after(
                dispatch_time(
                    DISPATCH_TIME_NOW,
                    Int64(time * Double(NSEC_PER_SEC))
                ),
                dispatch_get_main_queue(), clsr)
        }
    
        var closure:dispatch_block_t? = closure
        var cancelableClosure:dispatch_cancelable_closure?
    
        let delayedClosure:dispatch_cancelable_closure = { cancel in
            if closure != nil {
                if (cancel == false) {
                    dispatch_async(dispatch_get_main_queue(), closure!);
                }
            }
            closure = nil
            cancelableClosure = nil
        }
    
        cancelableClosure = delayedClosure
    
        dispatch_later {
            if let delayedClosure = cancelableClosure {
                delayedClosure(cancel: false)
            }
        }
    
        return cancelableClosure;
    }
    
    func cancel_delay(closure:dispatch_cancelable_closure?) {
    
        if closure != nil {
            closure!(cancel: true)
        }
    }
    

    Use as follow 使用方法如下

    let retVal = delay(2.0) {
        println("Later")
    }
    delay(1.0) {
        cancel_delay(retVal)
    }
    

    credits 学分

    Link above seems to be down. 上面的链接似乎有所下降。 Original Objc code from Github 来自Github的原始Objc代码


    #6楼

    Another way is to extend Double like this: 另一种方法是像这样扩展Double:

    extension Double {
       var dispatchTime: dispatch_time_t {
           get {
               return dispatch_time(DISPATCH_TIME_NOW,Int64(self * Double(NSEC_PER_SEC)))
           }
       }
    }
    

    Then you can use it like this: 然后你可以像这样使用它:

    dispatch_after(Double(2.0).dispatchTime, dispatch_get_main_queue(), { () -> Void in
                self.dismissViewControllerAnimated(true, completion: nil)
        })
    

    I like matt's delay function but just out of preference I'd rather limit passing closures around. 我喜欢亚光的延迟功能,但仅仅是出于偏好,我宁愿限制传递闭合。

    展开全文
  • Swift 2.xdispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let image = self.loadImage() // 回到主线程更新UI dispatch_async(dispatch_get_main_queue()) { self....

    Swift 2.x

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        let image = self.loadImage()
        // 回到主线程更新UI
        dispatch_async(dispatch_get_main_queue()) {
            self.imageView.image = image
        }
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
        print("test")
    }

    Swift 4.0

    DispatchQueue.global(qos: .userInitiated).async {
        let image = self.loadImage()
        // back to the main thread 
        DispatchQueue.main.async {
            self.imageView.image = image
        }
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) { 
        print("Are we there yet?")
    }



    展开全文
  • iOS Swift下dispatch_after的写法

    千次阅读 2016-09-27 11:29:16
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in //延迟3秒,执行的操作 }
  • swift实现dispatch_after

    2019-10-03 23:08:46
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(30 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in print("延迟30...
  • GCD延时操作Dispatch_after swift写法

    千次阅读 2016-11-08 14:09:57
    swift的延时操作方法 主要是int64这边 和 oc 有点区别  let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC)))  dispatch_after(delayTime, dispatch_get_main_queue...
  • Swift_3.0 相对于 Swift_2.2 版本发生了哪些的变化。本文件将以最直观的代码对比以及文字分析阐述。
  • 4Swift中UIButton的使用

    千次阅读 2015-11-30 00:36:58
    1、创建一个按钮对象,如下代码所示: let btn=UIButton(type:UIButtonType.System); 其中UIButtonType还有以下几种类型,如下说明以及运行效果图: //Custom //表示没有按钮类型,如果使用此类型,字体颜色为...
  • 许多优秀的 Swift 第三方框架还没有来得及迎接 Swift 4.0 的到来,它们还停留在 Swift3.x 的状态。 这个时候新建一个项目,使用cocoapods 引入所需的第三方,即使 pod install 成功后,一编译工程就是满屏红, 且...
  • Swift4学习笔记3——String和Character

    千次阅读 2017-06-13 20:52:03
    swift string characters
  • swift4--使用URLSession进行网络请求

    千次阅读 2018-11-22 20:04:58
    不知道怎么解析的小伙伴可以看我另一篇贴子 《 swift4--解析json》   GET请求: // // ViewController.swift // URLSesstionTest // import UIKit class ViewController: UIViewController { // 要传递的...
  • Swift 4.0 新特性

    千次阅读 2017-08-15 19:37:46
    WWDC 2017 带来了很多惊喜,在这次大会上,Swift 4 也伴随着 Xcode 9 测试版来到了我们的面前,虽然正式版要8月底9月初才会公布,但很多强大的新特性正吸引我们去学习它。根据大会上已经开放的新特性,先一睹为快。...
  • Swift接口和扩展

    2016-04-12 19:22:52
    这里主要说一下swift中的接口,其中类、结构体和枚举都可以实现接口。下面通过代码来分别实现一下// // ViewController.swift // swiftDemo // // Created by Jack on 16/4/6. // Copyright © 2016年 Jack. All ...
  • swift4 交换方法

    2019-06-16 22:34:10
    2019独角兽企业重金招聘Python工程师标准>>> ...
  • 开始把自己做过的项目尝试用swift语言写了一遍,学习全新的语法和设计模式,由于学习Swift的时间比较短,今天主要简单说一下如何在Swift 工程中使用时下比较流行的AFNetworking请求框架。 由于目前AF
  • ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/29/14. // Copyright (c) 2014 jikexueyuan. All rights reserved. // import UIKit class ...
  • 前面说过,React Native作为一个全新的跨平台开发框架,好多东西还不是很成熟,很多原生的控件还不是很完善,于是好多爱好者便自己封装相关的组件,可以使用oc来封装,也可以使用Swift来封装。关于封装的原理,大家...
  • Swift编程的15个技巧

    2019-07-05 10:06:53
    自2014年9月1.0版发布后,Savvy的应用开发团队就开始在iOS项目中实验并使用Swift。不过由于Swift相对还很新,当时大多项目仍在使用Objective-C;自从2015年9月Swift 2.0版本发布以来,我们已经开始改用Swift来编译新...
  • 升级Swift4 0遇到的坑

    2019-02-27 00:23:35
    升级Swift4.0 并不是所有库都能做到及时支持Swift4.0,更何况是在现在连Xcode9也还是beta的状态 所以我们仅能做到将自己的业务代码(主工程代码)部分升级到Swift4.0,然后同时保留各种pod库在Swift3.2版本。 没...
1 2 3 4 5 ... 20
收藏数 10,342
精华内容 4,136
关键字:

4 after swift