- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我以前见过几次,但我从来没有想过会出什么问题。
首先,我想创造出像电影中那些黑客场景中那样的数字加扰效果。因此,我制作了一个 NSTimer
来进行延迟,这样数字每 0.2 秒就会发生变化。然后,我制作了另一个计时器来告诉我的第一个计时器
invalidate()
两秒后。我的代码如下:
import UIKit
class MainPage: UIViewController {
@IBOutlet var genericDeviceName: UITextField!
@IBOutlet var hackButton: UIButton!
@IBOutlet var rightNumber: UILabel!
@IBOutlet var leftNumber: UILabel!
@IBOutlet var detectionText: UILabel!
@IBAction func deviceNameEnter(sender: AnyObject) {
detectionText.text = "Device detected: " + genericDeviceName.text!
if genericDeviceName.text == "" {
detectionText.text = "Error"
}
hackButton.alpha = 1
}
@IBAction func hackDevice(sender: AnyObject) {
var tries = 0
var timer = NSTimer()
var timerStop = NSTimer()
timer = NSTimer (timeInterval: 0.2, target: self, selector: "update", userInfo: nil, repeats: true)
timerStop = NSTimer (timeInterval: 2, target: self, selector: "endTimer", userInfo: nil, repeats: true)
let diceRoll = Int(arc4random_uniform(9) + 1)
let diceRollSecond = Int(arc4random_uniform(9) + 1)
UIView.animateWithDuration(0.25, animations:{
self.hackButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))})
func update() {leftNumber.text = String(diceRoll)
rightNumber.text = String(diceRoll)
print("it worked!")}
func endTimer() {
timer.invalidate()
detectionText.text = "Access Granted!"
timerStop.invalidate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
}
那么...出了什么问题?最近几次我尝试使用 NSTimers
,它们也没有用。我对 NSTimer
的概念有误吗?还是我的代码有错误?没有触发错误消息,只是计时器没有触发,数字也没有改变。甚至没有“它起作用了!”被打印到日志中。请通过建议一些代码来帮助。提前致谢!
更新
我已经更新了我的代码。在这里:
import UIKit
class MainPage: UIViewController {
@IBOutlet var genericDeviceName: UITextField!
@IBOutlet var hackButton: UIButton!
@IBOutlet var rightNumber: UILabel!
@IBOutlet var leftNumber: UILabel!
@IBOutlet var detectionText: UILabel!
@IBAction func deviceNameEnter(sender: AnyObject) {
detectionText.text = "Device detected: " + genericDeviceName.text!
if genericDeviceName.text == "" {detectionText.text = "Error"}
hackButton.alpha = 1
}
let diceRoll = Int(arc4random_uniform(9) + 1)
let diceRollSecond = Int(arc4random_uniform(9) + 1)
func update(timer: NSTimer) {leftNumber.text = String(diceRoll)
rightNumber.text = String(diceRoll)
print("it worked!")}
func endTimer(timerStop: NSTimer) {
timer.invalidate()
detectionText.text = "Access Granted!"
timerStop.invalidate()}
@IBAction func hackDevice(sender: AnyObject) {
var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "update:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
var timerStop = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "endTimer:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timerStop, forMode: NSRunLoopCommonModes)
UIView.animateWithDuration(0.25, animations:{
self.hackButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))})
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
目前,似乎函数“endTimer”不起作用,因为变量“timer”未被识别。请帮忙。非常感谢大家的宝贵时间!
最佳答案
一些事情:NSTimer 的选择器应该以冒号结尾(例如“更新:”或“endTimer:”并且该函数应该采用单个参数:NSTimer。
其次,定时器调用的函数必须是目标的顶层函数。您的更新方法是您的 hackDevice
函数的本地函数,该函数不起作用。
第三,您需要使用 scheduledTimerWithTimeInterval,如 ShahiM 的回答:
var timer = NSTimer.scheduledTimerWithTimeInterval(
0.4,
target: self,
selector: "update:",
userInfo: nil,
repeats: true)
如果您的选择器中的函数是嵌套函数,则该代码会崩溃,因为它对计时器不可见。
最后,看起来您需要将变量 diceRoll
和 diceRollSecond
移出您的 hackDevice
函数,并使它们成为您的实例变量类。
关于ios - NSTimer 不工作 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134702/
注意:我在这个项目中使用 swift。 我目前正在开发我的第一个项目,如果在一段时间后没有按下按钮,我会尝试停止(无效)NSTimer。我找到了其他停止 NSTimer 的方法,但没有说明如何在一定时
我正在 Xcode4 中工作,在一个项目中,出于组织目的,我一直使用三个不同的 NSTimer,但我意识到,除了名称之外,计时器是相同的,因此很容易将所有三个计时器合并为一。对于我的项目来说,哪一个更
根据 Apple 文档,将同时使用 WKInterfaceTimer(Watch 本地,倒计时但在结束时不触发任何事件)和 NSTimer(在计时器结束时触发方法)。所以,我的 App Interfa
我目前正在尝试制作一个 queueHandler,它将一个对象数组作为输入,用于在一个简单的 Double 机器人上执行驱动命令。我目前正在尝试使用 GCD 以串行执行我的函数,但是当我在我的队列中使
有个问题...我有定时器 [NSTimer scheduledTimerWithTimeInterval:120
我创建了一个计时器, self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self s
我尝试使用以下代码停止 NSTimer: - (void)viewDidLoad { [super viewDidLoad]; timer3 = [NSTimer timerWithT
我正在制作一个带有计时器的应用程序。我从给定时间开始计算分钟和秒,直到 0。发生这种情况时,我启动一个警报 View 。 我的结构是这样的: 主线程方法分配一个新线程并初始化它。线程的入口点(方法)有
好吧,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 5 秒钟的时间回答下一个数学问题。如果他们这样做了,函数“doCalculation”将再次运行,他
我正在创建益智游戏应用程序,并使用 NSTimer 显示时间(即 01:20)。当应用程序进入后台时,NSTimer 会暂停,但即使应用程序处于后台状态,我也想继续它。 例如,当应用程序进入后台时,计
我有一个带有 NSTimer *myTimer; 变量的类。在某些时候我会这样做: myTimer = [NSTimer scheduledTimerWithTimeInterval:20 targe
Edit2:为什么在“doSomething”方法中只更新了进度而不是point0? 编辑:使用我拥有的代码。我知道我一定忽略了一些东西,但我就是找不到它。 我正在编写一个 iPhone 应用程序,它
我的应用是一款体育应用。即使在后台游戏也应该继续,我为此使用了 nstimer。但我无法在后台运行计时器。每当应用程序处于后台时,计时器就会停止,返回时它会从离开时的同一时间继续。 最佳答案 您无法在
它将不断倒计时并更新当前时间。任何人都知道如何解决这个问题?我不明白 NStimer 如何识别这样的日期格式:20110803 23:59:59 最佳答案 查找NSDateFormatter 。您可以
是否可以在主线程上运行由 nstimer 调用的选择器? NSTimer 在它自己的线程中生成。 我的结构是一个线程调用一个带有 nstimer 的方法,nstimer 调用一个执行一些更新的方法,但
我在我的应用程序中使用了很多计时器。用于记录时间、移动物体、淡入淡出等。我在不同时间、同一 View 中使用同一个计时器来实现多个目的。我应该如何正确声明和无效或释放我的计时器? Atm 我这样声明计
我正在尝试使用 iPhone SDK 3.0 在线程上运行 NSTimer。我认为我做的一切都是正确的(新的运行循环等)。如果我在 viewDidDissappear 上调用 [timer inval
这是让我困惑了一段时间的事情。 我有一个 NSTimer,添加到 currentRunLoop 中,如果我不保留它,它就会崩溃。 NSTimer *timer = [[NSTimer timerWit
当我们使用 NSTimer 时,一旦在上述时间间隔后调用回调,UI 是否会被阻塞? 最佳答案 这要看情况。大多数时候,这不会成为问题。 如果,但是,满足以下两个条件,NSTimer 将阻塞 UI 线程
我使用大约 20 个 UIImageView 的小型飞机图像(50x50 像素)在 iPhone 屏幕上制作简单的动画。动画是通过按计时器间隔移动 UIImageView center 属性来完成的。
我是一名优秀的程序员,十分优秀!