forked from LcTwisk/SimpleImageViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageViewerPresentationTransition.swift
52 lines (43 loc) · 1.94 KB
/
ImageViewerPresentationTransition.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
import UIKit
final class ImageViewerPresentationTransition: NSObject, UIViewControllerAnimatedTransitioning {
private let fromImageView: UIImageView
init(fromImageView: UIImageView) {
self.fromImageView = fromImageView
super.init()
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
let fromParentView = fromImageView.superview!
let imageView = AnimatableImageView()
imageView.image = fromImageView.image
imageView.frame = fromParentView.convert(fromImageView.frame, to: nil)
imageView.contentMode = fromImageView.contentMode
let fadeView = UIView(frame: containerView.bounds)
fadeView.backgroundColor = .black
fadeView.alpha = 0.0
toView.frame = containerView.bounds
toView.isHidden = true
fromImageView.isHidden = true
containerView.addSubview(toView)
containerView.addSubview(fadeView)
containerView.addSubview(imageView)
UIView.animate(withDuration: transitionDuration(using: transitionContext),
delay: 0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0,
options: .curveEaseOut, animations: {
imageView.contentMode = .scaleAspectFit
imageView.frame = containerView.bounds
fadeView.alpha = 1.0
}, completion: { _ in
toView.isHidden = false
fadeView.removeFromSuperview()
imageView.removeFromSuperview()
transitionContext.completeTransition(true)
})
}
}