I'm trying to implement a custom view which visualizes a value which is changing in real time (like for example the volume of the audio output), and to achieve this I would like to have drawRect execute every animation frame to render the correct view state for the value.
The only solution I can think of is to set a timer to execute 60 times per second, and call view.setNeedsDisplay()
but I'm sure this is not the best way to get this behavior and guarantee smooth performance.
edit to give a slightly more concrete example
So the situation I have is basically this:
//There is a variable which is changing continuously
var dynamicFoo : Double!
//I want to create a view class which renders foo in real-time
class fooVisualizerView : UIView {
//I have drawing code which renders foo at any given value
override func drawRect(rect: CGRect) {
valueDependantDrawFunction(dynamicFoo)
}
}
Ideally I would like fooVisualizerView
to redraw on every animation frame.
A CADisplayLink
sounds like what you want - this is effectively a timer that is called every time the screen is updated. You can call setNeedsDisplay
from the display link method.
Note though that your code needs to complete in 1/60 of a second for this method or it will look terrible.