我有以下代码,每次定时器触发时都会产生奇怪的闪烁效果。完整的:
import UIKit
import PlaygroundSupport
var runCounter = 0
class MyView: UIView {
override func draw(_ rect: CGRect) {
if runCounter == 1 {
UIColor.white.setFill()
print(runCounter) //debugging
print(self.bounds) //debugging
UIBezierPath(rect: self.bounds).fill()
}
}
}
let view = MyView()
PlaygroundPage.current.liveView = view
Timer.scheduledTimer(withTimeInterval: 1/5, repeats: true, block: {timer in
view.setNeedsDisplay()
runCounter += 1
})
正如您所看到的,这会导致闪烁效果,即使runCounter只能设置为1次。我希望它永远保持白色,或者在下次通话中清除并永远保持黑色。我不明白为什么会这样。这是UIKit的bug吗?