【发布时间】:2018-07-03 17:08:54
【问题描述】:
我正在创建AVPlayerController 的子类,它观察玩家并处理玩家的状态。如果AVPlayerItemStatus 是.failed,我会在玩家的框架上添加一个自定义UIView。目前,我的自定义视图代码的重要部分如下所示:
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()
}
}
我正在从其他 ViewController 调用方法 animate(),以保留 NoiseView 对象。
问题是,它按应有的方式工作(视图正在制作动画并产生白噪声),但应用程序开始运行缓慢。在不导致性能下降的情况下重绘视图的正确方法应该是什么?
顺便说一下,下降发生的间隔为 0.1 秒(10 FPS)。我想要完成的是具有至少 30 FPS 的恒定白噪声,看起来像合法的电视噪声。
【问题讨论】:
-
你试图通过用高度和宽度为 1px 的随机条纹填充整个屏幕来实现什么动画?顺便说一句,减轻 CPU 的负担并利用使用 GPU 的核心图形 API 将解决减少帧丢失的问题。但是我不能在不知道你想要实现的动画的情况下发布代码
-
我正在尝试实现白噪声。它应该使用一组新的灰点重新渲染每一帧。