Try to search your question here, if you can't find : Ask Any Question Now ?

Timer for touchesBegan and touchesEnd apple pencil

HomeCategory: stackoverflowTimer for touchesBegan and touchesEnd apple pencil
Avatarrupesh asked 2 weeks ago

I am trying to create an app that records the time it takes to draw a clock.

I am new to swift and have to somehow create this app. I figured out how to allow me to draw, but i need to record the time it takes to draw an image (time drawing and total time – time in between drawing)

I have a timer, but want it to runTimer when I start drawing and stop when I am not drawing.

The app is for Alzheimer’s research, I would appreciate any help. Thank you.

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var canvasView: CanvasView!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

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

        seconds = 0    
    }
    @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)
    }

    @objc func updateTimer() {
            seconds += 1     //This will up the seconds.
        timeDrawing.text = "(seconds)" //This will update the label.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let touches:UITouch = touches.anyObject(in: self) as UITouch
        let touchLocation = touches.canvasView(in: self)

        if touches.type == .pencil  {
        if isTimerRunning == false {
                    runTimer()
                 } else {
                  timer.invalidate()
                 }
}

These were the two errors I keep getting.

Value of type ‘Set’ has no member ‘anyObject’
Value of type ‘UITouch’ has no member ‘canvasView’

Here is an image of the app UI

Thank you for help.

1 Answers
Best Answer
AvatarAmit answered 2 weeks ago
Your Answer

1 + 8 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com