Skip to content

Commit

Permalink
Remove guard statements that could put the app in non-recoverable und…
Browse files Browse the repository at this point in the history
…efined state
  • Loading branch information
LcTwisk committed Jun 30, 2017
1 parent de1deb3 commit 0fb900d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
10 changes: 3 additions & 7 deletions ImageViewer/AnimatableImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class AnimatableImageView: UIView {
didSet { update() }
}

var image: UIImage = UIImage() {
var image: UIImage? {
didSet {
imageView.image = image
update()
Expand All @@ -25,19 +25,15 @@ final class AnimatableImageView: UIView {
imageView.contentMode = .scaleToFill
}

convenience init(image: UIImage) {
self.init()
self.image = image
self.imageView.image = image
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

private extension AnimatableImageView {
func update() {
guard let image = image else { return }

switch contentMode {
case .scaleToFill:
imageView.bounds = Utilities.rect(forSize: bounds.size)
Expand Down
29 changes: 11 additions & 18 deletions ImageViewer/Transitions/ImageViewerDismissalTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ final class ImageViewerDismissalTransition: NSObject, UIViewControllerAnimatedTr
fileprivate var transitionContext: UIViewControllerContextTransitioning?

fileprivate let fromImageView: UIImageView
fileprivate let toImageView: UIImageView
fileprivate var toImageView: UIImageView

fileprivate var animatableImageview = AnimatableImageView()
fileprivate var fromView: UIView?
Expand Down Expand Up @@ -50,28 +50,21 @@ final class ImageViewerDismissalTransition: NSObject, UIViewControllerAnimatedTr

func start(_ transitionContext: UIViewControllerContextTransitioning) {
self.transitionContext = transitionContext
self.fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
let containerView = transitionContext.containerView
let fromSuperView = fromImageView.superview!
let image = fromImageView.image ?? toImageView.image

let container = transitionContext.containerView
guard
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from),
let image = fromImageView.image ?? toImageView.image else {
transitionContext.completeTransition(true)
return
}

self.fromView = fromView

fromView.isHidden = true
transitionContext.view(forKey: UITransitionContextViewKey.to)?.isHidden = false
animatableImageview.image = image
animatableImageview.frame = container.bounds
animatableImageview.frame = fromSuperView.convert(fromImageView.frame, to: nil)
animatableImageview.contentMode = .scaleAspectFit

fadeView.frame = container.bounds
fromView?.isHidden = true
fadeView.frame = containerView.bounds
fadeView.backgroundColor = .black

container.addSubview(fadeView)
container.addSubview(animatableImageview)
containerView.addSubview(fadeView)
containerView.addSubview(animatableImageview)
}

func cancel() {
Expand Down Expand Up @@ -119,7 +112,7 @@ private extension ImageViewerDismissalTransition {
case .end:
self.animatableImageview.contentMode = self.toImageView.contentMode
self.animatableImageview.transform = .identity
self.animatableImageview.frame = self.toImageView.superview!.convert(self.toImageView.frame, to: UIScreen.main.coordinateSpace)
self.animatableImageview.frame = self.toImageView.superview!.convert(self.toImageView.frame, to: nil)
self.fadeView.alpha = 0.0
}
}
Expand Down
35 changes: 18 additions & 17 deletions ImageViewer/Transitions/ImageViewerPresentationTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,34 @@ final class ImageViewerPresentationTransition: NSObject, UIViewControllerAnimate
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let container = transitionContext.containerView
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to),
let parentView = fromImageView.superview,
let image = fromImageView.image else {
transitionContext.completeTransition(true)
return
}

let imageView = AnimatableImageView(image: image)
imageView.frame = parentView.convert(fromImageView.frame, to: UIScreen.main.coordinateSpace)
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: container.bounds)
let fadeView = UIView(frame: containerView.bounds)
fadeView.backgroundColor = .black
fadeView.alpha = 0.0

toView.frame = container.bounds
toView.frame = containerView.bounds
toView.isHidden = true
fromImageView.isHidden = true

container.addSubview(toView)
container.addSubview(fadeView)
container.addSubview(imageView)
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: {
UIView.animate(withDuration: transitionDuration(using: transitionContext),
delay: 0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0,
options: .curveEaseOut, animations: {
imageView.contentMode = .scaleAspectFit
imageView.frame = container.bounds
imageView.frame = containerView.bounds
fadeView.alpha = 1.0
}, completion: { _ in
toView.isHidden = false
Expand Down

0 comments on commit 0fb900d

Please sign in to comment.