nstimer-如何在swift中延迟(代码片段)

author author     2023-05-13     593

关键词:

我在延迟计算机在游戏中的移动方面遇到了问题。

我找到了一些解决方案,但它们在我的情况下不起作用,例如:

var delay = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: nil, userInfo: nil, repeats: false)

我试图用功能fire但也没有效果。

还有哪些其他可能性?

答案

斯威夫特3

使用GCD:

let delayInSeconds = 4.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) 

    // here code perfomed with delay


或者使用计时器:

func myPerformeCode() 

   // here code to perform

let myTimer : Timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.myPerformeCode), userInfo: nil, repeats: false)

斯威夫特2

使用GCD:

let seconds = 4.0
let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

dispatch_after(dispatchTime, dispatch_get_main_queue(), 

   // here code perfomed with delay

)

或者使用计时器:

func myPerformeCode(timer : NSTimer) 

   // here code to perform

let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("myPerformeCode:"), userInfo: nil, repeats: false)
另一答案

使用Swift 4.2

使用Timer您可以避免使用选择器,而是使用闭包:

    Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false)  (nil) in
        // Your code here
    

请记住,Timer是使用CFRunLoopTimer免费桥接的,并且运行循环和GCD是两种完全不同的方法....

另一答案

在swift中,我们可以通过使用Dispatch_after.来延迟

SWIFT 3.0: -

DispatchQueue.main.asyncAfter(deadline: .now()+4.0) 

        alert.dismiss(animated: true, completion: nil)
    
另一答案

使用Grand Central Dispatch怎么样?

https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html

Valfer告诉你如何

在 Swift Playground 中使用 NSTimer [重复]

】在SwiftPlayground中使用NSTimer[重复]【英文标题】:UsingNSTimerinswiftplayground[duplicate]【发布时间】:2015-03-2412:10:52【问题描述】:我想知道如何在SwiftPlayground中使用NSTimer。Thisquestionhasbeenaskedbefore,但没有一个答案真正回答了这个问... 查看详情

swift的nstimer(代码片段)

查看详情

swift带有swift闭包的预定nstimer(代码片段)

查看详情

将参数传递给 NSTimer 在 Swift 中调用的方法

】将参数传递给NSTimer在Swift中调用的方法【英文标题】:PassingparameterstoamethodcalledbyNSTimerinSwift【发布时间】:2014-09-1309:15:31【问题描述】:我正在尝试将参数传递给NSTimer在我的代码中调用的方法。它正在引发异常。我就是这样... 查看详情

在 Swift 中使用 NSTimer

】在Swift中使用NSTimer【英文标题】:UsinganNSTimerinSwift【发布时间】:2014-08-1317:02:58【问题描述】:在这种情况下,永远不会调用timerFunc()。我错过了什么?classAppDelegate:NSObject,NSApplicationDelegatevarmyTimer:NSTimer?=nilfunctimerFunc()println("tim... 查看详情

如何在 Swift 中声明类级函数?

...想知道它是否存在于原生Swift中。例如,我可以像这样在NSTimer上调用类级函数:NSTimer.scheduledTimerWithTimeInterval(0.2,target:self,selector:"som 查看详情

在 Swift 中打开 NSMenu 时,NSTimer 不会触发

】在Swift中打开NSMenu时,NSTimer不会触发【英文标题】:NSTimernotfiringwhenNSMenuisopeninSwift【发布时间】:2015-03-1520:33:01【问题描述】:我有一个计时器,它会在闹钟响起后重新启动。alarm=NSTimer.scheduledTimerWithTimeInterval(60*minutesConstant+se... 查看详情

如何延迟 For 循环直到声音在 Swift 中播放完毕

】如何延迟For循环直到声音在Swift中播放完毕【英文标题】:HowtoDelayaForLoopUntilSoundhasFinishedPlayinginSwift【发布时间】:2016-02-0703:15:48【问题描述】:所以,我有一个for循环,它将数组中的值运行到if语句中。if语句根据数组中的值... 查看详情

为啥 NSTimer 在 swift 中只工作一次

】为啥NSTimer在swift中只工作一次【英文标题】:whyNSTimerworksjustonceinswift为什么NSTimer在swift中只工作一次【发布时间】:2016-01-1712:40:25【问题描述】:我有一个http请求,如果我正确接收到响应,那么我想启动一个每秒触发一个函... 查看详情

在 Swift 中使用 NSTimer 时发送到类的无法识别的选择器; NSInvalidArgumentException

】在Swift中使用NSTimer时发送到类的无法识别的选择器;NSInvalidArgumentException【英文标题】:UnrecognizedselectorsenttoclasswhenusingNSTimerinSwift;NSInvalidArgumentException【发布时间】:2016-01-2623:52:52【问题描述】:我正在尝试使用NSTimer以特定间... 查看详情

swift如何在swift中制作一个二维数组(代码片段)

查看详情

swift如何在swift中使用nsurlsessiondownloadtask顺序下载多个文件(代码片段)

查看详情

如何在 Swift 中创建延迟?

】如何在Swift中创建延迟?【英文标题】:HowtocreateadelayinSwift?【发布时间】:2015-02-1512:45:31【问题描述】:我想在某个时间点暂停我的应用程序。换句话说,我希望我的应用程序执行代码,然后在某个时间点暂停4秒,然后继续... 查看详情

swift中的异步延迟

...ousDelayinswift【发布时间】:2016-09-2611:36:50【问题描述】:如何在swift中获得异步延迟?我有3个函数,假设函数first()、second()和third()被异步调用。但是在second()函数中延迟10秒后,第三个函数在10秒后被调用,而不是我只想在10秒后... 查看详情

如何在 Cocoa 中运行 NSTimer 直到标志变量 ON

】如何在Cocoa中运行NSTimer直到标志变量ON【英文标题】:HowtorunNSTimeruntilflagvariableONinCocoa【发布时间】:2013-09-0403:48:02【问题描述】:我有一个问题:我想让NSTimer等到FLAG变量为YES,如果FLAG=YES,myTimer将停止。我怎样才能做到这一... 查看详情

Swift:在 nstimer 倒计时结束时转换到新的视图控制器 [重复]

】Swift:在nstimer倒计时结束时转换到新的视图控制器[重复]【英文标题】:Swift:transitiontonewviewcontrolleratendofnstimercountdown[duplicate]【发布时间】:2015-07-0517:53:07【问题描述】:好的,所以我在Swift中工作,我有我的ViewController和我的... 查看详情

如何用另一个 NSTimer 停止 NSTimer?

】如何用另一个NSTimer停止NSTimer?【英文标题】:HowtostopNSTimerwithanotherNSTimer?【发布时间】:2015-12-3116:40:51【问题描述】:注意:我在这个项目中使用swift。我目前正在处理我的第一个项目,如果在一定时间后未按下按钮,我正在... 查看详情

定时器延迟加载(代码片段)

...种方法呢?经过查阅资料,大概有三种方法:NSTimer、CADisplayLink、GCD。接下来我就一一介绍 查看详情