-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathImageSourceView.swift
More file actions
81 lines (74 loc) · 2.84 KB
/
Copy pathImageSourceView.swift
File metadata and controls
81 lines (74 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#if canImport(SwiftUI) && canImport(Combine)
import ImageIOSwift
import SwiftUI
extension ImageSource: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
}
/// A view that displays an image source using BindableImageSourceController.
///
/// Use this when you want to customize the display of an image source, for instance to show animation progress or info about the image.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct ImageControllerView<Content: View>: View {
/// The image source to dipslay.
public var imageSource: ImageSource
/// When true, animation will start once the image is loaded.
public var isAnimationEnabled: Bool
/// The contents to use to render the image source.
public var content: (BindableImageSourceController) -> Content
/// Create an image controller view.
/// - Parameter imageSource: The image source to dipslay.
/// - Parameter isAnimationEnabled: When true, animation will start once the image is loaded.
/// - Parameter content: The content to render for each frame of the image source.
public init(imageSource: ImageSource, isAnimationEnabled: Bool = true, content: @escaping (BindableImageSourceController) -> Content) {
self.imageSource = imageSource
self.isAnimationEnabled = isAnimationEnabled
self.content = content
}
public var body: some View {
Derived(
from: imageSource,
using: BindableImageSourceController.init
) { controller in
self.content(controller)
.onAppear {
if self.isAnimationEnabled {
controller.startAnimating()
}
}
.onDisappear {
controller.stopAnimating()
}
}
}
}
/// A SwiftUI view that displays an image source, updating as it loads.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct ImageSourceView: View {
/// The image source to dipslay.
public var imageSource: ImageSource
/// When true, animation will start once the image is loaded.
public var isAnimationEnabled: Bool
/// The label associated with the image. The label is used for things like accessibility.
public var label: Text
/// Create a image source view.
/// - Parameter imageSource: The image source to dipslay.
/// - Parameter isAnimationEnabled: When true, animation will start once the image is loaded.
/// - Parameter label: The label associated with the image. The label is used for things like accessibility.
public init(imageSource: ImageSource, isAnimationEnabled: Bool = true, label: Text) {
self.imageSource = imageSource
self.isAnimationEnabled = isAnimationEnabled
self.label = label
}
public var body: some View {
ImageControllerView(imageSource: imageSource, isAnimationEnabled: isAnimationEnabled) { controller in
StaticImageSourceView(
image: controller.currentImage,
properties: controller.currentProperties,
label: self.label
)
}
}
}
#endif