Xcode NSTimer 计数++

标签 xcode swift nstimer

我有一个 NSTimer 在名为 moving 的函数中每 0.05 秒运行一次。 在 moving 里面我想要一个数字来计算并将它显示到标签中 同时让物体移动。

现在的问题是它只能同时做其中一件事情。 这是代码:

timer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "moving", userInfo: nil, repeats: true)    

func moving() {

   countingUp = countingUp + 1

    label.text = "\(countingUp)"

    objectOne.center = CGPointMake(objectOne.center.x, objectOne.center.y + 1) 
}

现在它只在对象不动时在标签中计数。 但是,如果我删除标签的全部代码,对象就会移动。 有人可以帮我解决这个问题吗?

最佳答案

试试这个:

let runloop = NSRunLoop.currentRunLoop()
let timer = NSTimer(timeInterval: 0.05, target: self, selector: "moving", userInfo: nil, repeats: true)
runloop.addTimer(timer, forMode:NSRunLoopCommonModes)
runloop.addTimer(timer, forMode:UITrackingRunLoopMode)

关于Xcode NSTimer 计数++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787524/

相关文章:

ios - 为什么在设置imageView之前将 "translatesAutoresizingMaskIntoConstraints"设置为false,会显示无图像?

c++ - 奇怪的 Xcode 链接器警告构建 Poco C++ 应用程序

ios - 追踪 xcode 堆栈跟踪时遇到问题

ios - 如何将 NSURL session 进度添加到 TableView 单元格

ios - 如何使用 swift 下载和共享 pdf 文件

ios - Xamarin,更新 Xcode 后未找到有效的配置文件

ios - 在 .sks 文件中使用自定义 SKSpriteNode 类?

ios - 使用尚未触发的 block 使 NSTimer 无效 : Objective-C

swift - 生成敌人

ios - NSTimer 启动后如何更改显示的时间格式?