Using binding to support the animation control. This API design is the same as SwiftUI's Toggle/Sheet
This commit is contained in:
parent
d23f9b4bb0
commit
35190d2c0a
|
@ -48,6 +48,46 @@ public struct AnimatedImage : PlatformViewRepresentable {
|
||||||
var webOptions: SDWebImageOptions = []
|
var webOptions: SDWebImageOptions = []
|
||||||
var webContext: [SDWebImageContextOption : Any]? = nil
|
var webContext: [SDWebImageContextOption : Any]? = nil
|
||||||
|
|
||||||
|
/// A Binding to control the animation. You can bind external logic to control the animation status.
|
||||||
|
/// True to start animation, false to stop animation.
|
||||||
|
@Binding public var isAnimating: Bool
|
||||||
|
|
||||||
|
public init(url: URL?, placeholder: PlatformImage? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
|
||||||
|
self.init(url: url, placeholder: placeholder, options: options, context: context, isAnimating: .constant(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(url: URL?, placeholder: PlatformImage? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool>) {
|
||||||
|
self._isAnimating = isAnimating
|
||||||
|
self.placeholder = placeholder
|
||||||
|
self.webOptions = options
|
||||||
|
self.webContext = context
|
||||||
|
self.imageModel.url = url
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(name: String, bundle: Bundle? = nil) {
|
||||||
|
self.init(name: name, bundle: bundle, isAnimating: .constant(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(name: String, bundle: Bundle? = nil, isAnimating: Binding<Bool>) {
|
||||||
|
self._isAnimating = isAnimating
|
||||||
|
#if os(macOS)
|
||||||
|
let image = SDAnimatedImage(named: name, in: bundle)
|
||||||
|
#else
|
||||||
|
let image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil)
|
||||||
|
#endif
|
||||||
|
self.imageModel.image = image
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(data: Data, scale: CGFloat = 0) {
|
||||||
|
self.init(data: data, scale: scale, isAnimating: .constant(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(data: Data, scale: CGFloat = 0, isAnimating: Binding<Bool>) {
|
||||||
|
self._isAnimating = isAnimating
|
||||||
|
let image = SDAnimatedImage(data: data, scale: scale)
|
||||||
|
self.imageModel.image = image
|
||||||
|
}
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
public typealias NSViewType = AnimatedImageViewWrapper
|
public typealias NSViewType = AnimatedImageViewWrapper
|
||||||
#else
|
#else
|
||||||
|
@ -89,6 +129,13 @@ public struct AnimatedImage : PlatformViewRepresentable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if self.isAnimating != view.wrapped.isAnimating {
|
||||||
|
if self.isAnimating {
|
||||||
|
view.wrapped.startAnimating()
|
||||||
|
} else {
|
||||||
|
view.wrapped.stopAnimating()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
configureView(view, context: context)
|
configureView(view, context: context)
|
||||||
layoutView(view, context: context)
|
layoutView(view, context: context)
|
||||||
|
@ -310,30 +357,6 @@ extension AnimatedImage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initializer
|
|
||||||
extension AnimatedImage {
|
|
||||||
public init(url: URL?, placeholder: PlatformImage? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
|
|
||||||
self.placeholder = placeholder
|
|
||||||
self.webOptions = options
|
|
||||||
self.webContext = context
|
|
||||||
self.imageModel.url = url
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(name: String, bundle: Bundle? = nil) {
|
|
||||||
#if os(macOS)
|
|
||||||
let image = SDAnimatedImage(named: name, in: bundle)
|
|
||||||
#else
|
|
||||||
let image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil)
|
|
||||||
#endif
|
|
||||||
self.imageModel.image = image
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(data: Data, scale: CGFloat = 0) {
|
|
||||||
let image = SDAnimatedImage(data: data, scale: scale)
|
|
||||||
self.imageModel.image = image
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
struct AnimatedImage_Previews : PreviewProvider {
|
struct AnimatedImage_Previews : PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
|
|
Loading…
Reference in New Issue