Skip to content

Commit f09878b

Browse files
authored
Merge pull request #27 from SDWebImage/feature_animated_image_view_event
Feature: Supports coordinate with native UIKit/AppKit view
2 parents 6dc0002 + 0bd0006 commit f09878b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var body: some View {
106106
- [x] Supports animation control using the SwiftUI Binding
107107
- [x] Supports indicator and transition powered by SDWebImage and CoreAnimation
108108
- [x] Supports advanced control like loop count, incremental load, buffer size
109+
- [x] Supports coordinate with native UIKit/AppKit/WKInterface view
109110

110111
Note: `AnimatedImage` supports both image url or image data for animated image format. Which use the SDWebImage's [Animated ImageView](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#animated-image-50) for internal implementation. Pay attention that since this base on UIKit/AppKit representable, if you need advanced customized layout and animation, you need CoreAnimation to help.
111112

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ final class AnimatedImageModel : ObservableObject {
2020
@Published var progressBlock: ((Int, Int) -> Void)?
2121
}
2222

23+
// Coordinator Life Cycle Binding Object
24+
final class AnimatedImageCoordinator : ObservableObject {
25+
@Published var viewCreateBlock: ((PlatformView) -> Void)?
26+
@Published var viewUpdateBlock: ((PlatformView) -> Void)?
27+
}
28+
2329
// Layout Binding Object
2430
final class AnimatedImageLayout : ObservableObject {
2531
@Published var contentMode: ContentMode = .fill
@@ -58,6 +64,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
5864
@ObservedObject var imageModel = AnimatedImageModel()
5965
@ObservedObject var imageLayout = AnimatedImageLayout()
6066
@ObservedObject var imageConfiguration = AnimatedImageConfiguration()
67+
@ObservedObject var imageCoordinator = AnimatedImageCoordinator()
6168

6269
var url: URL?
6370
var placeholder: PlatformImage?
@@ -196,7 +203,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
196203
}
197204

198205
func makeView(context: PlatformViewRepresentableContext<AnimatedImage>) -> AnimatedImageViewWrapper {
199-
AnimatedImageViewWrapper()
206+
let view = AnimatedImageViewWrapper()
207+
if let viewCreateBlock = imageCoordinator.viewCreateBlock {
208+
viewCreateBlock(view)
209+
}
210+
return view
200211
}
201212

202213
func updateView(_ view: AnimatedImageViewWrapper, context: PlatformViewRepresentableContext<AnimatedImage>) {
@@ -238,6 +249,9 @@ public struct AnimatedImage : PlatformViewRepresentable {
238249

239250
configureView(view, context: context)
240251
layoutView(view, context: context)
252+
if let viewUpdateBlock = imageCoordinator.viewUpdateBlock {
253+
viewUpdateBlock(view)
254+
}
241255
}
242256

243257
static func dismantleView(_ view: AnimatedImageViewWrapper, coordinator: ()) {
@@ -554,6 +568,26 @@ extension AnimatedImage {
554568
}
555569
}
556570

571+
// View Coordinator Handler
572+
extension AnimatedImage {
573+
574+
/// Provide the action when view representable create the native view.
575+
/// - Parameter action: The action to perform. The first arg is the native view.
576+
/// - Returns: A view that triggers `action` when view representable create the native view.
577+
public func onViewCreate(perform action: ((PlatformView) -> Void)? = nil) -> AnimatedImage {
578+
imageCoordinator.viewCreateBlock = action
579+
return self
580+
}
581+
582+
/// Provide the action when view representable update the native view.
583+
/// - Parameter action: The action to perform. The first arg is the native view.
584+
/// - Returns: A view that triggers `action` when view representable update the native view.
585+
public func onViewUpdate(perform action: ((PlatformView) -> Void)? = nil) -> AnimatedImage {
586+
imageCoordinator.viewUpdateBlock = action
587+
return self
588+
}
589+
}
590+
557591
#if os(macOS) || os(iOS) || os(tvOS)
558592
// Web Image convenience
559593
extension AnimatedImage {

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct Indicator {
2525
}
2626
}
2727

28-
#if os(macOS) || os(iOS) || os(iOS)
28+
#if os(macOS) || os(iOS) || os(tvOS)
2929
extension Indicator {
3030
/// Activity Indicator
3131
public static var activity: Indicator {

0 commit comments

Comments
 (0)