Any Clue why timer only executes on Fire() and not time Intervals?

import UIKit



class Lines: UIView{
    
  
    var numL=4
    
    var lin = [[Double]](repeating: [Double](repeating: 0.0,count: 4), count: 5)
    var li:Double = 0.0
    var xn:Double = 0.0
    var xt:Double = 0.0
    var yn:Double = 0.0
    var yt:Double = 0.0
    
    var gl:Int = 0

    func setArray(){
        
        
      
        
        for l in 0..<numL{
            li = Double(l)
            lin[l][0]=0.0
            lin[l][1]=20.0+(li*20.0)
            lin[l][2]=200.0
            lin[l][3]=20.0+(li*20.0)
            
        }
        
    }
    
    func drLine(_ Ln: Int ){
        xn = lin[Ln][0]
        yn = lin[Ln][1]
        xt = lin[Ln][2]
        yt = lin[Ln][3]
        
        let context = UIGraphicsGetCurrentContext()
        context?.setLineWidth(1.0)
        context?.setStrokeColor(UIColor.red.cgColor)
        context?.move(to: CGPoint(x: xn, y: yn))
        context?.addLine(to: CGPoint(x: xt, y: yt))
        context?.strokePath()
        setNeedsDisplay()
    }
    
    @objc func mk(){
    
        drLine(gl)
        setNeedsDisplay()
            
        }
    
    
   
    
    
    
    func Run() {
        
        setArray()
        gl=0
        for j in  0..<numL{
           // MakeLine(j)
            gl=j
            //mk()
           // DispatchQueue.global(qos: .default).async(){
            
            
            //DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0){
            //self.mk()
                
                
            //}
            //}
                
              //  self.mk()
               // self.setNeedsDisplay()
            //}
            
            
           //timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(self.mk), userInfo: nil, repeats: true)
             //timer.fire()
            //}
           // }
            
            //perform(#selector(mk),with: nil,afterDelay: 2.0)
              // self.MakeLine(j)
            
            //}
            //timer.fire()
           // DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            //self.MakeLine(j)
            //}
            
            
            //makeLine(j)
        }
        
    }
    var timer = Timer()
    override func draw(_ rect: CGRect){
       
       setArray()
        //drLine(1)
        
        //Run()
       //drLine()
        for j in 0..<numL{
            gl=j
            //DispatchQueue.global(qos: .default).async(){
                
                
              //  DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0){
               // self.mk()
                
                
                //}
               // }
            
            timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(self.mk), userInfo: nil, repeats: true)
            //timer.fire()
            
            
            //let runLoop = RunLoop.current
            //runLoop.add(time, forMode: .commonModes)
            //runLoop.run()
            
            
        }
    }
    }

What are we supposed to make of this code? What do the commented-out lines represent? Code you think doesn't work? Code you tried and discarded? Code you haven't tested yet?


Anyway, you're mis-using both timers and the "draw" method.


— If you want a timer to repeat, you shouldn't recreate it every time through your draw loop. Create it once, set it going, and then leave it alone.


— A "draw" method should just do drawing.


— You shouldn't try to draw anything outside a "draw" method. In particular, you cannot call "mk" unless you're calling it directly from "draw", because that's the only time there's a drawing context. (The system sets up the context and calls "draw".)


— Your actual drawing code (inside "mk", apparently) shouldn't use "setNeedsDisplay", because you'll be drawing constantly. Every invocation will trigger another invocation, which is not a good idea.


If you're trying to draw something new every two seconds, you should break the problem down. When the view first appears (there's a "viewDidAppear" override you can use), start the timer. When the timer fires every 2 seconds, invoke "setNeedsDisplay". When "draw" is called, just draw.

It was my way of showing I did trial and error before asking for help. Yea looks like I got the timer working now with your suggestions. If only the lines printed now that'd be great. They print and disappear if I do a timer.fire() after the timer with interval but I choose Dispatch.queue instead because the time intervals are constant . Man I wish there was a website where you could offer bounties on code you need help on. But yea I guess I should've cleaned it up some. Here's it cleaned up some> I wish there was something I could offer you if you'd be willing to help me get these lines to print...I do have a pay pal account..Not sure if its against forum policy to offer some $

class Lines: UIView{

    var numL=40
    //var context =[int](repeating: )
    var lin = [[Double]](repeating: [Double](repeating: 0.0,count: 4), count: 40)
    var li:Double = 0.0
    var xn:Double = 0.0
    var xt:Double = 0.0
    var yn:Double = 0.0
    var yt:Double = 0.0
    var Ln:Int = 0
    var gl:Int = 0

    func setArray(){
        
        
      
        
        for l in 0..<numL{
            li = Double(l)
            lin[l][0]=0.0
            lin[l][1]=20.0+(li*20.0)
            lin[l][2]=200.0
            lin[l][3]=20.0+(li*20.0)
            
        }
        
    }
    @objc func Delta(){
        gl=gl+1
        print (gl)
        
    }
    @objc func drLine(){
        Ln = gl
        xn = lin[Ln][0]
        yn = lin[Ln][1]
        xt = lin[Ln][2]
        yt = lin[Ln][3]
        
        let context = UIGraphicsGetCurrentContext()
        context?.setLineWidth(1.0)
        context?.setStrokeColor(UIColor.red.cgColor)
        context?.move(to: CGPoint(x: xn, y: yn))
        context?.addLine(to: CGPoint(x: xt, y: yt))
        context?.strokePath()
        //self.setNeedsDisplay()
        gl=gl+1
        print(gl)
        
    }

   var timer = Timer()
   
    override func draw(_ rect: CGRect){
        if gl<1{
            setArray()
        }
       // timer = Timer.scheduledTimer(timeInterval:.now() + 5.0, target: self, selector: #selector(self.drLine), userInfo: nil, repeats: true)
            //setNeedsDisplay()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0){
            self.drLine()
            self.setNeedsDisplay()
            }
        
        }
    }

   

This seems to print the first line just fine but not the ones after

  override func draw(_ rect: CGRect){
        if gl<1{
            setArray()
        }
       
        timer = Timer.scheduledTimer(timeInterval:2.0, target: self, selector: #selector(self.Delta), userInfo: nil, repeats: true)
        drLine()
        setNeedsDisplay()
        
       
    
        }
Any Clue why timer only executes on Fire() and not time Intervals?
 
 
Q