-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from noppefoxwolf/updatelink
Use UIUpdateLink in iOS18
- Loading branch information
Showing
6 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 27 additions & 15 deletions
42
Sources/AnimatedImage/BaseClass/AnimatableCGImageView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,40 @@ | ||
public import UIKit | ||
import UpdateLink | ||
|
||
open class AnimatableCGImageView: CGImageView, DisplayLinkTarget { | ||
private var displayLink: CADisplayLink? = nil | ||
open class AnimatableCGImageView: CGImageView { | ||
var updateLink: (any UpdateLink)! | ||
|
||
open func startAnimating() { | ||
stopAnimating() | ||
displayLink = CADisplayLink( | ||
target: DisplayLinkProxy(target: self), | ||
selector: #selector(DisplayLinkProxy<Self>.updateContents) | ||
public override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
if #available(iOS 18.0, *) { | ||
updateLink = UIUpdateLink(view: self) | ||
} else { | ||
updateLink = BackportUpdateLink(view: self) | ||
} | ||
updateLink.isEnabled = true | ||
updateLink.preferredFrameRateRange = CAFrameRateRange( | ||
minimum: 1, | ||
maximum: 60 | ||
) | ||
displayLink?.preferredFrameRateRange = CAFrameRateRange(minimum: 1, maximum: 60) | ||
displayLink?.add(to: .main, forMode: .common) | ||
updateLink.addAction(handler: { [unowned self] _, info in | ||
willUpdateContents(&contents, for: info.modelTime) | ||
}) | ||
updateLink.requiresContinuousUpdates = true | ||
} | ||
|
||
open func stopAnimating() { | ||
displayLink?.invalidate() | ||
displayLink = nil | ||
@MainActor public required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func updateContents(_ displayLink: CADisplayLink) { | ||
willUpdateContents(&contents, for: displayLink.targetTimestamp) | ||
open func startAnimating() { | ||
updateLink.isEnabled = true | ||
} | ||
|
||
open func stopAnimating() { | ||
updateLink.isEnabled = false | ||
} | ||
|
||
open func willUpdateContents(_ contents: inout CGImage?, for targetTimestamp: TimeInterval) { | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import UIKit | ||
|
||
public final class BackportUpdateLink: UpdateLink, DisplayLinkTarget { | ||
private var displayLink: CADisplayLink! | ||
|
||
public init(view: UIView) { | ||
displayLink = CADisplayLink( | ||
target: DisplayLinkProxy(target: self), | ||
selector: #selector(DisplayLinkProxy<Self>.updateContents) | ||
) | ||
} | ||
|
||
public var isEnabled: Bool { | ||
get { !displayLink.isPaused } | ||
set { displayLink.isPaused = !newValue } | ||
} | ||
|
||
public var requiresContinuousUpdates: Bool = false { | ||
didSet { | ||
if requiresContinuousUpdates { | ||
displayLink.add(to: .main, forMode: .common) | ||
} else { | ||
displayLink.remove(from: .main, forMode: .common) | ||
} | ||
} | ||
} | ||
|
||
public var preferredFrameRateRange: CAFrameRateRange { | ||
get { displayLink?.preferredFrameRateRange ?? .default } | ||
set { displayLink?.preferredFrameRateRange = newValue } | ||
} | ||
|
||
private var handlers: [(any UpdateLink, any UpdateInfo) -> Void] = [] | ||
public func addAction( | ||
handler: @escaping (any UpdateLink, any UpdateInfo) -> Void | ||
) { | ||
handlers.append(handler) | ||
} | ||
|
||
func updateContents(_ displayLink: CADisplayLink) { | ||
let info = BackportUpdateInfo(modelTime: displayLink.targetTimestamp) | ||
handlers.forEach { action in | ||
action(self, info) | ||
} | ||
} | ||
} | ||
|
||
final class BackportUpdateInfo: UpdateInfo { | ||
var modelTime: TimeInterval | ||
|
||
init(modelTime: TimeInterval) { | ||
self.modelTime = modelTime | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import UIKit | ||
|
||
@available(iOS 18.0, *) | ||
extension UIUpdateInfo: UpdateInfo {} | ||
|
||
@available(iOS 18.0, *) | ||
extension UIUpdateLink: UpdateLink { | ||
public func addAction( | ||
handler: @escaping (any UpdateLink, any UpdateInfo) -> Void | ||
) { | ||
addAction { (link: UIUpdateLink, info: UIUpdateInfo) in | ||
handler(link, info) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import UIKit | ||
|
||
@MainActor | ||
public protocol UpdateLink: AnyObject { | ||
var isEnabled: Bool { get set } | ||
var requiresContinuousUpdates: Bool { get set } | ||
var preferredFrameRateRange: CAFrameRateRange { get set } | ||
func addAction(handler: @escaping (any UpdateLink, any UpdateInfo) -> Void) | ||
} | ||
|
||
@MainActor | ||
public protocol UpdateInfo: AnyObject { | ||
var modelTime: TimeInterval { get } | ||
} | ||
|