-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviewImagesView.swift
More file actions
97 lines (90 loc) · 3.41 KB
/
Copy pathPreviewImagesView.swift
File metadata and controls
97 lines (90 loc) · 3.41 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import SwiftUI
import UIKit
struct PreviewImagesView: View {
private let images: [UIImage]
private var initialImageIndex: Int
private var onDismiss: () -> Void
private var downloadAction: (UIImage) -> Void
private var shareAction: (UIImage) -> Void
@State private var currentImageIndex: Int
@State private var showFullScreen: Bool = true
private var safeAreaInsets: UIEdgeInsets {
UIApplication.shared.keyWindow?.safeAreaInsets ?? UIEdgeInsets()
}
private var topPadding: CGFloat { safeAreaInsets.top + 4 }
private let toolbarPadding: CGFloat = 8
private let imageHeight: CGFloat = 40
init(
images: [UIImage],
initialImageIndex: Int = 0,
onDismiss: @escaping () -> Void,
downloadAction: @escaping (UIImage) -> Void,
shareAction: @escaping (UIImage) -> Void
) {
self.images = images
self.initialImageIndex = initialImageIndex
self.onDismiss = onDismiss
self.downloadAction = downloadAction
self.shareAction = shareAction
_currentImageIndex = State(initialValue: initialImageIndex)
}
var body: some View {
ZStack {
ImagePreviewToolbar(
onDismiss: onDismiss,
downloadAction: { downloadAction(images[currentImageIndex]) },
shareAction: { shareAction(images[currentImageIndex]) }
)
.frame(maxHeight: .infinity, alignment: .top)
.padding(.top, topPadding)
.padding(.horizontal, toolbarPadding)
.zIndex(2)
.opacity(showFullScreen ? 1 : 0)
.allowsHitTesting(showFullScreen)
TabView(selection: $currentImageIndex) {
ForEach(images.indices, id: \.self) { index in
ZoomableImage(
image: images[index],
resetBehavior: .onPageChange(
currentPageIndex: $currentImageIndex,
pageIndex: index
)
)
.onZoomStarted {
withAnimation {
showFullScreen = false
}
}
.onZoomEnded { zoom in
if zoom == 1 {
withAnimation {
showFullScreen = true
}
}
}
.onSingleTap {
withAnimation {
showFullScreen.toggle()
}
}
.ignoresSafeArea()
.tag(index)
}
}
.frame(maxHeight: .infinity, alignment: .center)
.tabViewStyle(.page(indexDisplayMode: .never))
.animation(.default, value: currentImageIndex)
ThumbnailPickerView(
images: images,
imageHeight: imageHeight,
currentImageIndex: $currentImageIndex
)
.frame(maxHeight: .infinity, alignment: .bottom)
.padding(.bottom, safeAreaInsets.bottom)
.opacity(showFullScreen ? 1 : 0)
.allowsHitTesting(showFullScreen)
}
.ignoresSafeArea()
.background(.ultraThinMaterial)
}
}