I'm creating a subclass of AVPlayerController, that observes the player and handles player's states. If AVPlayerItemStatus is .failed, I add a custom UIView over the player's frame. At present, the important parts of my custom view's code looks like this:
class NoiseView: UIView { ... var timer: Timer? func animate() { timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } func stopAnimating() { timer?.invalidate() } @objc func timerAction() { self.setNeedsDisplay() } override func draw(_ rect: CGRect) { super.draw(rect) let context = UIGraphicsGetCurrentContext()! let h = rect.height let w = rect.width var val: CGFloat! var color: UIColor! for i in 0...Int(w-1) { for j in 0...Int(h-1) { val = .random color = UIColor(red: val, green: val, blue: val, alpha: 1.0) context.setFillColor(color.cgColor) context.fill(CGRect(x: i, y: j, width: 1, height: 1)) } } context.flush() } } I'm calling the method animate() from other ViewController that keeps the NoiseView object.
The thing is, it's working as it's supposed to work (view is animating and creating a white noise) but the app starts to work slowly. What should be a proper approach to redraw the view without causing such a performance drop?
By the way, the drop happened with 0.1s interval (10 FPS). What I want to accomplish is having a constant white noise with at least 30 FPS to look like a legit tv noise.