Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clean up code, swift 4 porting
  • Loading branch information
MarioIannotta committed Oct 19, 2017
1 parent e27583e commit 1bb9191
Show file tree
Hide file tree
Showing 31 changed files with 192 additions and 1,214 deletions.
Binary file modified .DS_Store
Binary file not shown.
11 changes: 0 additions & 11 deletions MIBlurPopup.podspec

This file was deleted.

Expand Up @@ -10,158 +10,122 @@ import Foundation
import UIKit

public protocol MIBlurPopupDelegate: class {

var popupView: UIView { get }
var blurEffectStyle: UIBlurEffectStyle { get }
var initialScaleAmmount: CGFloat { get }
var animationDuration: TimeInterval { get }

}

open class MIBlurPopup: NSObject {

// MARK: Default config

private struct DefaultConfig {
static let initialScaleAmmount: CGFloat = 0.7
static let animationDuration: TimeInterval = 0.3
static let blurEffectStyle: UIBlurEffectStyle = .dark
}

fileprivate weak var delegate: MIBlurPopupDelegate?

private static var shared = MIBlurPopup()

fileprivate var visualEffectBlurView = UIVisualEffectView()
fileprivate var isPresenting = false
private var visualEffectBlurView = UIVisualEffectView()
private var isPresenting = false

open static func show(_ viewControllerToPresent: UIViewController, on parentViewController: UIViewController) {

viewControllerToPresent.modalPresentationStyle = .overCurrentContext
viewControllerToPresent.transitioningDelegate = shared

if let popupDelegate = viewControllerToPresent as? MIBlurPopupDelegate {
shared.delegate = popupDelegate
} else {
if !(viewControllerToPresent is MIBlurPopupDelegate) {
assertionFailure("ERROR: \(viewControllerToPresent) does not conform to protocol 'MIBlurPopupDelegate'")
}

parentViewController.present(viewControllerToPresent, animated: true, completion: nil)

}

// MARK: Transitions

fileprivate func animatePresentationWithTransitionContext(_ transitionContext: UIViewControllerContextTransitioning) {
private func animatePresent(transitionContext: UIViewControllerContextTransitioning) {
guard
let presentedViewController = transitionContext.viewController(forKey: .to),
let presentedControllerDelegate = presentedViewController as? MIBlurPopupDelegate
else { return }

guard let presentedControllerView = transitionContext.view(forKey: UITransitionContextViewKey.to) else { return }
presentedViewController.view.alpha = 0

presentedControllerView.alpha = 0

transitionContext.containerView.addSubview(presentedControllerView)
transitionContext.containerView.addSubview(presentedViewController.view)

visualEffectBlurView.frame = transitionContext.containerView.bounds
visualEffectBlurView.alpha = 1

transitionContext.containerView.insertSubview(visualEffectBlurView, at: 0)

delegate?.popupView.alpha = 0
delegate?.popupView.transform = CGAffineTransform(scaleX: delegate?.initialScaleAmmount ?? DefaultConfig.initialScaleAmmount, y: delegate?.initialScaleAmmount ?? DefaultConfig.initialScaleAmmount)
presentedControllerDelegate.popupView.alpha = 0
presentedControllerDelegate.popupView.transform = CGAffineTransform(scaleX: presentedControllerDelegate.initialScaleAmmount,
y: presentedControllerDelegate.initialScaleAmmount)

// blur view animation workaround: need that to avoid the "blur-flashes"
UIView.animate(withDuration: transitionDuration(using: transitionContext) * 0.75) {

self.visualEffectBlurView.effect = UIBlurEffect(style: self.delegate?.blurEffectStyle ?? DefaultConfig.blurEffectStyle)

self.visualEffectBlurView.effect = UIBlurEffect(style: presentedControllerDelegate.blurEffectStyle)
}

// actual popup animation

UIView.animate(

withDuration: transitionDuration(using: transitionContext),
delay: 0.0,
usingSpringWithDamping: 1.0,
initialSpringVelocity: 0.0,
options: .allowUserInteraction,

animations: {

presentedControllerView.alpha = 1

self.delegate?.popupView.alpha = 1
self.delegate?.popupView.transform = CGAffineTransform(scaleX: 1, y: 1)

presentedViewController.view.alpha = 1
presentedControllerDelegate.popupView.alpha = 1
presentedControllerDelegate.popupView.transform = CGAffineTransform(scaleX: 1, y: 1)
},
completion: { (completed) in

transitionContext.completeTransition(completed)

completion: { isCompleted in
transitionContext.completeTransition(isCompleted)
}

)

}
fileprivate func animateDismissalWithTransitionContext(_ transitionContext: UIViewControllerContextTransitioning) {

guard let presentedControllerView = transitionContext.view(forKey: UITransitionContextViewKey.from) else { return }

fileprivate func animateDismiss(transitionContext: UIViewControllerContextTransitioning) {
guard
let presentedViewController = transitionContext.viewController(forKey: .from),
let presentedControllerDelegate = presentedViewController as? MIBlurPopupDelegate
else { return }

UIView.animate(

withDuration: transitionDuration(using: transitionContext),
delay: 0.0,
usingSpringWithDamping: 2,
initialSpringVelocity: 0.0,
options: .allowUserInteraction,
animations: {

presentedControllerView.alpha = 0

presentedViewController.view.alpha = 0
self.visualEffectBlurView.alpha = 0
self.delegate?.popupView.transform = CGAffineTransform(scaleX: self.delegate?.initialScaleAmmount ?? DefaultConfig.initialScaleAmmount, y: self.delegate?.initialScaleAmmount ?? DefaultConfig.initialScaleAmmount)

}, completion: {(completed: Bool) -> Void in
presentedControllerDelegate.popupView.transform = CGAffineTransform(scaleX: presentedControllerDelegate.initialScaleAmmount,
y: presentedControllerDelegate.initialScaleAmmount)
},
completion: { isCompleted in
self.visualEffectBlurView.effect = nil
self.visualEffectBlurView.removeFromSuperview()

transitionContext.completeTransition(completed)

transitionContext.completeTransition(isCompleted)
}

)

}

}

// MARK: - Storyboard Segue

open class MIBlurPopupSegue: UIStoryboardSegue {

open override func perform() {

MIBlurPopup.show(destination, on: source)

}

}

// MARK: - UIViewControllerTransitioningDelegate

extension MIBlurPopup: UIViewControllerTransitioningDelegate {

// MARK: - UIViewControllerTransitioningDelegate

public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

isPresenting = true

return self

}

public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

isPresenting = false

return self

}

}
Expand All @@ -171,18 +135,21 @@ extension MIBlurPopup: UIViewControllerTransitioningDelegate {
extension MIBlurPopup: UIViewControllerAnimatedTransitioning {

public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

return self.delegate?.animationDuration ?? 0.3

if let toViewControllerDelegate = transitionContext?.viewController(forKey: .to) as? MIBlurPopupDelegate {
return toViewControllerDelegate.animationDuration
}
if let fromViewControllerDelegate = transitionContext?.viewController(forKey: .from) as? MIBlurPopupDelegate {
return fromViewControllerDelegate.animationDuration
}
return 0
}

public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

if isPresenting {
animatePresentationWithTransitionContext(transitionContext)
animatePresent(transitionContext: transitionContext)
} else {
animateDismissalWithTransitionContext(transitionContext)
animateDismiss(transitionContext: transitionContext)
}

}

}

0 comments on commit 1bb9191

Please sign in to comment.