当前位置:  开发笔记 > 编程语言 > 正文

NSTimer不工作Swift

如何解决《NSTimer不工作Swift》经验,为你挑选了1个好方法。

我以前见过这几次,但从来没有发生过什么可能是错的.

首先,我想创造像在电影中的黑客场景中那样加扰数字的效果.所以,我做了一个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错的吗?或者我的代码中是否有错误?没有触发错误消息,只是计时器没有触发,数字没有改变.甚至没有"它奏效了!" 被打印在原木上.建议一些代码请帮忙.先感谢您!

UPDATE

我已经更新了我的代码.这里是:

 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"未被识别.请帮忙.非常感谢你的时间!



1> Duncan C..:

一些事情:NSTimer的选择器应以冒号结尾(例如"update:"或"endTimer:"并且该函数应该采用单个参数:NSTimer.

其次,定时器调用的函数必须是目标的顶级函数.您的更新方法是您的hackDevice函数的本地函数,它不起作用.

第三,您需要使用scheduledTimerWithTimeInterval,如ShahiM的答案:

var timer = NSTimer.scheduledTimerWithTimeInterval(
  0.4, 
  target: self, 
  selector: "update:", 
  userInfo: nil, 
  repeats: true)

如果选择器中的函数是嵌套函数,那么代码会崩溃,因为它对计时器不可见.

最后,它看起来像你需要移动的变量diceRoll,并diceRollSecond从你的hackDevice功能,使他们的类的实例变量.

推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有