mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Use a shared CADisplayLink whenever possible
This commit is contained in:
@@ -34,55 +34,6 @@ private let completionKey = "CAAnimationUtils_completion"
|
||||
public let kCAMediaTimingFunctionSpring = "CAAnimationUtilsSpringCurve"
|
||||
public let kCAMediaTimingFunctionCustomSpringPrefix = "CAAnimationUtilsSpringCustomCurve"
|
||||
|
||||
private final class FrameRangeContext {
|
||||
private var animationCount: Int = 0
|
||||
private var displayLink: CADisplayLink?
|
||||
|
||||
init() {
|
||||
}
|
||||
|
||||
func add() {
|
||||
self.animationCount += 1
|
||||
self.update()
|
||||
}
|
||||
|
||||
func remove() {
|
||||
self.animationCount -= 1
|
||||
if self.animationCount < 0 {
|
||||
self.animationCount = 0
|
||||
assertionFailure()
|
||||
}
|
||||
self.update()
|
||||
}
|
||||
|
||||
@objc func displayEvent() {
|
||||
}
|
||||
|
||||
private func update() {
|
||||
if self.animationCount != 0 {
|
||||
if self.displayLink == nil {
|
||||
let displayLink = CADisplayLink(target: self, selector: #selector(self.displayEvent))
|
||||
|
||||
if #available(iOS 15.0, *) {
|
||||
let maxFps = Float(UIScreen.main.maximumFramesPerSecond)
|
||||
if maxFps > 61.0 {
|
||||
displayLink.preferredFrameRateRange = CAFrameRateRange(minimum: 60.0, maximum: maxFps, preferred: maxFps)
|
||||
}
|
||||
}
|
||||
|
||||
self.displayLink = displayLink
|
||||
displayLink.add(to: .main, forMode: .common)
|
||||
displayLink.isPaused = false
|
||||
}
|
||||
} else if let displayLink = self.displayLink {
|
||||
self.displayLink = nil
|
||||
displayLink.invalidate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let frameRangeContext = FrameRangeContext()
|
||||
|
||||
public extension CAAnimation {
|
||||
var completion: ((Bool) -> Void)? {
|
||||
get {
|
||||
@@ -103,18 +54,16 @@ public extension CAAnimation {
|
||||
|
||||
private func adjustFrameRate(animation: CAAnimation) {
|
||||
if #available(iOS 15.0, *) {
|
||||
if let animation = animation as? CABasicAnimation {
|
||||
if animation.keyPath == "opacity" {
|
||||
return
|
||||
}
|
||||
}
|
||||
let maxFps = Float(UIScreen.main.maximumFramesPerSecond)
|
||||
if maxFps > 61.0 {
|
||||
#if DEBUG
|
||||
//let _ = frameRangeContext.add()
|
||||
#endif
|
||||
|
||||
animation.preferredFrameRateRange = CAFrameRateRange(minimum: 30.0, maximum: maxFps, preferred: maxFps)
|
||||
var preferredFps: Float = maxFps
|
||||
if let animation = animation as? CABasicAnimation {
|
||||
if animation.keyPath == "opacity" {
|
||||
preferredFps = 60.0
|
||||
return
|
||||
}
|
||||
}
|
||||
animation.preferredFrameRateRange = CAFrameRateRange(minimum: 30.0, maximum: preferredFps, preferred: maxFps)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user