85 lines
2.6 KiB
Swift
85 lines
2.6 KiB
Swift
//
|
|
// SmoothTransition.swift
|
|
// Privado
|
|
//
|
|
// Created by Viktor on 30.06.2020.
|
|
// Copyright © 2020 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
class SmoothTransition: NSObject, UIViewControllerAnimatedTransitioning {
|
|
|
|
private enum Constants {
|
|
static let duration: TimeInterval = 0.2
|
|
static let dy: CGFloat = 0
|
|
}
|
|
|
|
var popStyle: Bool = false
|
|
|
|
private var duration: TimeInterval = Constants.duration
|
|
private var dy: CGFloat = Constants.dy
|
|
|
|
init(duration: TimeInterval = Constants.duration, dy: CGFloat = Constants.dy) {
|
|
super.init()
|
|
self.duration = duration
|
|
self.dy = dy
|
|
}
|
|
|
|
func transitionDuration(
|
|
using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
|
|
return self.duration
|
|
}
|
|
|
|
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
|
|
if popStyle {
|
|
|
|
animatePop(using: transitionContext)
|
|
return
|
|
}
|
|
|
|
guard let fz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) else {
|
|
return
|
|
}
|
|
guard let tz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) else {
|
|
return
|
|
}
|
|
|
|
let f = transitionContext.finalFrame(for: tz)
|
|
|
|
let fOff = f.offsetBy(dx: f.width, dy: self.dy)
|
|
tz.view.frame = fOff
|
|
|
|
transitionContext.containerView.insertSubview(tz.view, aboveSubview: fz.view)
|
|
|
|
UIView.animate(
|
|
withDuration: transitionDuration(using: transitionContext),
|
|
animations: {
|
|
tz.view.frame = f
|
|
}, completion: {_ in
|
|
transitionContext.completeTransition(true)
|
|
})
|
|
}
|
|
|
|
func animatePop(using transitionContext: UIViewControllerContextTransitioning) {
|
|
|
|
let fz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
|
|
let tz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
|
|
|
|
let f = transitionContext.initialFrame(for: fz)
|
|
let fOffPop = f.offsetBy(dx: f.width, dy: self.dy)
|
|
|
|
transitionContext.containerView.insertSubview(tz.view, belowSubview: fz.view)
|
|
|
|
UIView.animate(
|
|
withDuration: transitionDuration(using: transitionContext),
|
|
animations: {
|
|
fz.view.frame = fOffPop
|
|
}, completion: {_ in
|
|
transitionContext.completeTransition(true)
|
|
})
|
|
}
|
|
}
|