gpt4 book ai didi

ios - NSTimer 不工作 swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:45:55 25 4
gpt4 key购买 nike

我以前见过几次,但我从来没有想过会出什么问题。

首先,我想创造出像电影中那些黑客场景中那样的数字加扰效果。因此,我制作了一个 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)

如果您的选择器中的函数是嵌套函数,则该代码会崩溃,因为它对计时器不可见。

最后,看起来您需要将变量 diceRolldiceRollSecond 移出您的 hackDevice 函数,并使它们成为您的实例变量类。

关于ios - NSTimer 不工作 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com