forked from michaelhenry/ImageViewer.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageViewerTransitionPresentationManager.swift
216 lines (178 loc) · 7.27 KB
/
ImageViewerTransitionPresentationManager.swift
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// ImageViewerTransitionPresentationManager.swift
// ImageViewer.swift
//
// Created by Michael Henry Pantaleon on 2020/08/19.
//
import Foundation
import UIKit
protocol ImageViewerTransitionViewControllerConvertible {
// The source view
var sourceView: UIImageView? { get }
// The final view
var targetView: UIImageView? { get }
}
final class ImageViewerTransitionPresentationAnimator:NSObject {
let isPresenting: Bool
let imageContentMode: UIView.ContentMode
init(isPresenting: Bool, imageContentMode: UIView.ContentMode) {
self.isPresenting = isPresenting
self.imageContentMode = imageContentMode
super.init()
}
}
// MARK: - UIViewControllerAnimatedTransitioning
extension ImageViewerTransitionPresentationAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?)
-> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let key: UITransitionContextViewControllerKey = isPresenting ? .to : .from
guard let controller = transitionContext.viewController(forKey: key)
else { return }
let animationDuration = transitionDuration(using: transitionContext)
if isPresenting {
presentAnimation(
transitionView: transitionContext.containerView,
controller: controller,
duration: animationDuration) { finished in
transitionContext.completeTransition(finished)
}
} else {
dismissAnimation(
transitionView: transitionContext.containerView,
controller: controller,
duration: animationDuration) { finished in
transitionContext.completeTransition(finished)
}
}
}
private func createDummyImageView(frame: CGRect, image:UIImage? = nil)
-> UIImageView {
let dummyImageView:UIImageView = UIImageView(frame: frame)
dummyImageView.clipsToBounds = true
dummyImageView.contentMode = imageContentMode
dummyImageView.alpha = 1.0
dummyImageView.image = image
return dummyImageView
}
private func presentAnimation(
transitionView:UIView,
controller: UIViewController,
duration: TimeInterval,
completed: @escaping((Bool) -> Void)) {
guard
let transitionVC = controller as? ImageViewerTransitionViewControllerConvertible,
let sourceView = transitionVC.sourceView
else { return }
sourceView.alpha = 0.0
controller.view.alpha = 0.0
transitionView.addSubview(controller.view)
transitionVC.targetView?.alpha = 0.0
transitionVC.targetView?.tintColor = sourceView.tintColor
let dummyImageView = createDummyImageView(
frame: sourceView.frameRelativeToWindow(),
image: sourceView.image)
dummyImageView.contentMode = .scaleAspectFit
dummyImageView.tintColor = sourceView.tintColor
transitionView.addSubview(dummyImageView)
UIView.animate(withDuration: duration, animations: {
dummyImageView.frame = UIScreen.main.bounds
controller.view.alpha = 1.0
}) { finished in
transitionVC.targetView?.alpha = 1.0
dummyImageView.removeFromSuperview()
completed(finished)
}
}
private func dismissAnimation(
transitionView:UIView,
controller: UIViewController,
duration:TimeInterval,
completed: @escaping((Bool) -> Void)) {
guard
let transitionVC = controller as? ImageViewerTransitionViewControllerConvertible
else { return }
let sourceView = transitionVC.sourceView
let targetView = transitionVC.targetView
let dummyImageView = createDummyImageView(
frame: targetView?.frameRelativeToWindow() ?? UIScreen.main.bounds,
image: targetView?.image)
dummyImageView.tintColor = sourceView?.tintColor
transitionView.addSubview(dummyImageView)
targetView?.isHidden = true
controller.view.alpha = 1.0
UIView.animate(withDuration: duration, animations: {
if let sourceView = sourceView {
// return to original position
dummyImageView.frame = sourceView.frameRelativeToWindow()
} else {
// just disappear
dummyImageView.alpha = 0.0
}
controller.view.alpha = 0.0
}) { finished in
sourceView?.alpha = 1.0
controller.view.removeFromSuperview()
completed(finished)
}
}
}
final class ImageViewerTransitionPresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
var frame: CGRect = .zero
frame.size = size(forChildContentContainer: presentedViewController,
withParentContainerSize: containerView!.bounds.size)
return frame
}
override func containerViewWillLayoutSubviews() {
presentedView?.frame = frameOfPresentedViewInContainerView
}
}
final class ImageViewerTransitionPresentationManager: NSObject {
private let imageContentMode: UIView.ContentMode
public init(imageContentMode: UIView.ContentMode) {
self.imageContentMode = imageContentMode
}
}
// MARK: - UIViewControllerTransitioningDelegate
extension ImageViewerTransitionPresentationManager: UIViewControllerTransitioningDelegate {
func presentationController(
forPresented presented: UIViewController,
presenting: UIViewController?,
source: UIViewController
) -> UIPresentationController? {
let presentationController = ImageViewerTransitionPresentationController(
presentedViewController: presented,
presenting: presenting)
return presentationController
}
func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
return ImageViewerTransitionPresentationAnimator(isPresenting: true, imageContentMode: imageContentMode)
}
func animationController(
forDismissed dismissed: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
return ImageViewerTransitionPresentationAnimator(isPresenting: false, imageContentMode: imageContentMode)
}
}
// MARK: - UIAdaptivePresentationControllerDelegate
extension ImageViewerTransitionPresentationManager: UIAdaptivePresentationControllerDelegate {
func adaptivePresentationStyle(
for controller: UIPresentationController,
traitCollection: UITraitCollection
) -> UIModalPresentationStyle {
return .none
}
func presentationController(
_ controller: UIPresentationController,
viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle
) -> UIViewController? {
return nil
}
}