Skip to content

Feature: Supports coordinate with native UIKit/AppKit view #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var body: some View {
- [x] Supports animation control using the SwiftUI Binding
- [x] Supports indicator and transition powered by SDWebImage and CoreAnimation
- [x] Supports advanced control like loop count, incremental load, buffer size
- [x] Supports coordinate with native UIKit/AppKit/WKInterface view

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.

Expand Down
36 changes: 35 additions & 1 deletion SDWebImageSwiftUI/Classes/AnimatedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ final class AnimatedImageModel : ObservableObject {
@Published var progressBlock: ((Int, Int) -> Void)?
}

// Coordinator Life Cycle Binding Object
final class AnimatedImageCoordinator : ObservableObject {
@Published var viewCreateBlock: ((PlatformView) -> Void)?
@Published var viewUpdateBlock: ((PlatformView) -> Void)?
}

// Layout Binding Object
final class AnimatedImageLayout : ObservableObject {
@Published var contentMode: ContentMode = .fill
Expand Down Expand Up @@ -58,6 +64,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
@ObservedObject var imageModel = AnimatedImageModel()
@ObservedObject var imageLayout = AnimatedImageLayout()
@ObservedObject var imageConfiguration = AnimatedImageConfiguration()
@ObservedObject var imageCoordinator = AnimatedImageCoordinator()

var url: URL?
var placeholder: PlatformImage?
Expand Down Expand Up @@ -196,7 +203,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
}

func makeView(context: PlatformViewRepresentableContext<AnimatedImage>) -> AnimatedImageViewWrapper {
AnimatedImageViewWrapper()
let view = AnimatedImageViewWrapper()
if let viewCreateBlock = imageCoordinator.viewCreateBlock {
viewCreateBlock(view)
}
return view
}

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

configureView(view, context: context)
layoutView(view, context: context)
if let viewUpdateBlock = imageCoordinator.viewUpdateBlock {
viewUpdateBlock(view)
}
}

static func dismantleView(_ view: AnimatedImageViewWrapper, coordinator: ()) {
Expand Down Expand Up @@ -554,6 +568,26 @@ extension AnimatedImage {
}
}

// View Coordinator Handler
extension AnimatedImage {

/// Provide the action when view representable create the native view.
/// - Parameter action: The action to perform. The first arg is the native view.
/// - Returns: A view that triggers `action` when view representable create the native view.
public func onViewCreate(perform action: ((PlatformView) -> Void)? = nil) -> AnimatedImage {
imageCoordinator.viewCreateBlock = action
return self
}

/// Provide the action when view representable update the native view.
/// - Parameter action: The action to perform. The first arg is the native view.
/// - Returns: A view that triggers `action` when view representable update the native view.
public func onViewUpdate(perform action: ((PlatformView) -> Void)? = nil) -> AnimatedImage {
imageCoordinator.viewUpdateBlock = action
return self
}
}

#if os(macOS) || os(iOS) || os(tvOS)
// Web Image convenience
extension AnimatedImage {
Expand Down
2 changes: 1 addition & 1 deletion SDWebImageSwiftUI/Classes/Indicator/Indicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct Indicator {
}
}

#if os(macOS) || os(iOS) || os(iOS)
#if os(macOS) || os(iOS) || os(tvOS)
extension Indicator {
/// Activity Indicator
public static var activity: Indicator {
Expand Down