【问题标题】:Nil cannot be assigned to type 'Timer'?不能将 Nil 分配给类型“计时器”?
【发布时间】:2019-12-19 23:48:11
【问题描述】:

您好,我正在完成一些关于阿尔茨海默病的研究 - 我希望能够记录完成绘画所需的时间(患者绘画应该只需要几秒钟)。我想记录在平板电脑上使用 Apple Pencil 所花费的时间以及完成绘画所花费的总体时间(在平板电脑上的时间加上笔画之间的时间)。

到目前为止,我已经创建了这个应用程序,但无法让计时器工作。

我已经把绘图/涂鸦板放下了。

我尝试了许多不同的方法,但我只是没有足够的代码经验来弄清楚为什么当苹果铅笔碰到平板电脑时它没有启动计时器。以下代码用于 ViewController 脚本。

到目前为止,仅创建了一个用于 = 绘制时间的计时器。但我什至无法让它发挥作用。

尝试更改脚本,尝试询问朋友。我对 swift 很陌生,因此非常感谢任何帮助。


import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var canvasView: CanvasView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func clearCanvas(_ sender: Any) {
        canvasView.clearCanvas()
        timer.invalidate()

        seconds = 0    
        timeDrawing.text = "\(seconds)"
    }
    @IBOutlet weak var timeDrawing: UILabel!

    var seconds = 0 
    var timer = Timer()
    var isTimerRunning = false //This will be used to make sure only one timer is created at a time.
    var resumeTapped = false
    var touchPoint:CGPoint!
    var touches:UITouch!

    func runTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
    }
    private(set) var from: Date?

    @objc func updateTimer() {
        let to = Date()
        let timeIntervalFrom = (from ?? to).timeIntervalSince1970
        let time = to.timeIntervalSince1970 - timeIntervalFrom
        timeDrawing.text = "\(round(time))" //This will update the label.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        guard let touch = touches.first else { return }
        let touches = touch.location(in: canvasView) // or maybe ...(in: self)

        if touch.type == .pencil  {
            if !isTimerRunning {
                from = Date()
                runTimer()
            }
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        if !isTimerRunning {
            timer.invalidate()
            timer = nil
            from = nil
            timeDrawing.text = "0"
        }
}
}

我希望当苹果铅笔碰到平板电脑时它会启动计时器。然后,当铅笔离开数位板时,它会停止一个计时器并启动其中一个计时器(尚未实现)。 (我还没有为笔画之间的时间添加另一个计时器,任何帮助也将不胜感激。)

【问题讨论】:

  • Timer 必须是可选的才能采用nilvar timer: Timer?。您需要打开它才能访问它:timer?.invalidate()
  • 感谢 vacawama,我做到了,但应用程序仍然无法按预期工作。接下来我该怎么办?就像它一样,计时器只是不工作并且不在屏幕上显示数字。谢谢。
  • 调试。您是否正在访问 if touch.type == .pencil 中的代码?您永远不会将 isTimerRunning 设置为 true
  • 好的,我将如何将其设置为 true?非常感谢。
  • runTimer()中设置为true

标签: swift ipad iso apple-pencil


【解决方案1】:

只有可选项可以取 nil 值,您可以像这样将计时器对象设为可选项

 var timer:Timer?

是的,这是有道理的。已编辑

【讨论】:

  • 分配一个未使用的计时器是没有意义的。只需var timer: Timer?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2014-11-25
  • 2022-01-27
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多