草庐IT

ios - Swift NSTimer 使应用程序崩溃

调用时,应用程序崩溃。funchandlers(){vartimer=NSTimer.scheduledTimerWithTimeInterval(0.5,target:self,selector:"timeListener",userInfo:data,repeats:true)}functimeListener(timer:NSTimer){vardata:AnyObject?=timer.userInfoprintln(data);}尝试了一些东西,尝试了另一个答案(PassingparameterstoamethodcalledbyNSTimerinSwift),但每个代码都使

swift - 使 NSTimer 无效无效

我有一个设置为150秒的NSTimer,当它下降到0时,它应该停止计数并结束我正在创建的游戏。但是,当我调用invalidate()时,它会继续运行。我也有它,所以当计时器无效但没有运气时它应该打印“计时器停止”。还有另一种方法吗?这是我的代码:importSpriteKitvarcountDown:NSTimer()classGameScene:SKScene{overridefuncdidMoveToView(view:SKView)countDown=NSTimer.scheduledTimerWithTimeInterval(1.0,target:self,selector:"

swift - NSTimer 在此函数中无效时不会停止

我正在尝试使用NTTimer,但它不起作用。从这里开始:timer=NSTimer.scheduledTimerWithTimeInterval(0.003,target:self,selector:"openFrameAnimation",userInfo:nil,repeats:true)它触发这个函数:funcopenFrameAnimation(){Swift.print("AnimationGoes...",NSDate().timeIntervalSince1970)self.frame=NSRect(x:self.frame.origin.x,y:self.frame.o

ios - 无法将 (Timer!) -> Void 转换为 ((CFRunLoopTimer?) ->Void)! - 将 NSTimer 扩展转换为 Swift 3

我正在尝试将我在我的项目中使用的Pod转换为Swift3。我没有写它,但原作者没有更新它所以我fork了我自己尝试做的任何东西。但是……我在尝试将NSTimer的扩展转换为Swift3时遇到此错误:无法将类型“(Timer!)->Void”的值转换为预期的参数类型“((CFRunLoopTimer?)->Void)!似乎Swift3处理程序类型(Timer!)->Void与老派的CFRunLoop样式处理程序不兼容,但我不确定如何在保持与iOS9的兼容性的同时进行转换。我在下面粘贴由Xcode转换后的代码。您可以在https://github.com/entotsu/TKSubmitT

ios - 在 Swift 中,当应用程序进入后台时,如何在 AppDelegate 中验证 NSTimer?

我需要将我的iOS应用程序从obj-c转换为swift。我在ViewController中有一个NStimer,它每30秒从shoutcast加载一次元数据,但是当应用程序退出事件时它会停止,当进入前台时它会再次运行。编辑:好的。问题解决了!我在viewDidLoad中添加了两个名为UIApplicationWillResignActiveNotification和UIApplicationWillEnterForegroundNotification的观察者,如下所示:overridefuncviewDidLoad(){NSLog("SystemVersionis\(UIDevice

swift - macOS 上的 OpenGL VSync/NSTimer 问题

我正在尝试在macOS上设置一个简单的OpenGL游戏,使用NSTimer设置运行循环,如here所述.这个想法是创建一个具有非常小(~1ms)时间间隔的重复计时器,并依靠vsync来调节帧速率。我将我的NSOpenGLContext交换间隔设置为值1,这应该启用垂直同步。我的印象是这会导致NSOpenGLContext.flushbuffer阻塞,但事实并非如此。我的渲染代码每秒触发60次以上。我链接的文档已被标记为已停用,但我读过的所有官方文档都表明可以以某种方式将NSTimer循环限制为显示的刷新率。不过,我还没有成功,我想知道这种方法是否不再可行。我错过了什么吗?在现代项目中,

ios - 一个 NSTimer 可以接受多个选择器吗?

我正在尝试在我的应用程序中使用NSTimer,并且想知道是否可以在计时器触发时调用两个方法。代码如下:gameTimer=NSTimer.scheduledTimerWithTimeInterval(0.01,target:self,selector:Selector("gameMovement"&&"fireBullet"),userInfo:nil,repeats:true)我收到一条错误消息,指出选择器中有两个参数。 最佳答案 没有。您只需调用一个方法即可委托(delegate)给所有您想要的东西。funcsomeFunc()

ios - 在 Swift 中使用 NSTimer 的几个小数槽的倒计时

例如,我想制作一个计时器从10.0000000开始的应用程序,并且我希望它完美倒计时到目前为止,这是我的代码:importUIKitclassViewController:UIViewController{@IBOutletweakvarlabelTime:UILabel!varcounter=10.0000000varlabelValue:Double{get{returnNSNumberFormatter().numberFromString(labelTime.text!)!.doubleValue}set{labelTime.text="\(newValue)"}}overri

ios - spritekit 中的多个 NSTimer 未按预期工作

目标:我有粒子从屏幕顶部掉落,我希望它们在分数达到一定值时提高速度。上一次失败的尝试:我首先让那些下落的粒子受到重力的影响,并尝试在分数达到一定值时增加重力值。但是它似乎没有用,所以我假设在游戏过程中不能改变重力。现状:经过一些研究,我想到了使用两个全局NSTimer,slowTimer和fastTimer。slowTimer在didMoveToView中调用。在touchesBegan中,每次点击时,我都会调用一个函数changeSpeed,它是这样的:funcchangeSpeed(){ifscore>=2{slowTimer.invalidate()makeWallsDivide

swift - NSTimer scheduledTimerWithTimeInterval 和目标是 "class level function"

如何在Swift中设置target(调用类级函数),就像在obj-c中完成的那样:callaclasslevelmethodinscheduledTimerWithTimeInterval 最佳答案 classMyClass:NSObject{classfuncstartTimer(){NSTimer.scheduledTimerWithTimeInterval(2.0,target:MyClass.self,selector:"callByTimer:",userInfo:nil,repeats:true)}classfuncca