Reputation: 254
I am continuously reading data (CGFloat array) from a source, and I want to draw a line on a UIView from the data points on that source.
I understand that I need to use UIBezierPath to create the line. I am able to do so initially, but I do not know how to keep on updating the drawing on the UIView [like a series of path.move(..)].
Does anyone know how to implement this?
TIA
Upvotes: 0
Views: 1943
Reputation: 1252
Sub-class UIView and override the draw
rect method:
import UIKit
class MyLineView: UIView {
// Edit:add the data to draw
public var lineData : Array<Float>?
override func draw(_ rect: CGRect) {
// Read the values and draw the lines
// using your bezier functions
// Edit: just to test - you can see the new data
if let data = lineData {
print("Redrawing \(data.count) elements")
}
}
}
In your view controller create a property of type Timer
and start it going at an appropriate point with your desired frame rate:
import UIKit
class ViewController: UIViewController {
var animationTimer : Timer?
var myLineView : MyLineView?
**// Edit: add an array of data**
var myData : Array<Float>?
override func viewDidLoad() {
super.viewDidLoad()
self.myLineView = MyLineView(frame: self.view.bounds)
self.view.addSubview(myLineView!)
// Edit: init data
self.myData = Array()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Update the view at roughly 10Hz
animationTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (timer) in
//Edit: example update of the data before redrawing
self.myData?.append(Float(1))
self.myLineView!.lineData = self.myData!
self.myLineView!.setNeedsDisplay()
})
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Stop updating
animationTimer!.invalidate()
animationTimer = nil
}
}
This will call drawRect on your subclass at an approximate rate of 10Hz.
Upvotes: 1