Skip to content

Commit

Permalink
SebastianBoldt#41 - Added perspective to Cover Animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Isom committed Mar 26, 2019
1 parent 5a39c0d commit ea91a94
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Example/Jelly/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MainViewController: UIViewController {
let uiConfiguration = PresentationUIConfiguration(cornerRadius: 10, backgroundStyle: .dimmed(alpha: 0.5), isTapBackgroundToDismissEnabled: true, corners: [.layerMaxXMaxYCorner,.layerMaxXMinYCorner,.layerMinXMaxYCorner,.layerMinXMinYCorner])
let size = PresentationSize(width: .fullscreen, height: .halfscreen)
let alignment = PresentationAlignment(vertical: .bottom, horizontal: .center)
let presentation = CoverPresentation(directionShow: .bottom, directionDismiss: .bottom, uiConfiguration: uiConfiguration, size: size, alignment: alignment, marginGuards: marginGuards)
let presentation = CoverPresentation(directionShow: .bottom, directionDismiss: .bottom, uiConfiguration: uiConfiguration, size: size, alignment: alignment, marginGuards: marginGuards, depthScale: 0.80)
let animator = Animator(presentation: presentation)
animator.prepare(presentedViewController: viewControllerToPresent)
self.animator = animator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
final class CoverAnimator: NSObject {
private let presentationType : PresentationType
private let presentation : CoverPresentation
private var screenshot: UIView?

init(presentationType: PresentationType, presentation: CoverPresentation) {
self.presentationType = presentationType
Expand Down Expand Up @@ -56,6 +57,19 @@ extension CoverAnimator : UIViewControllerAnimatedTransitioning {
let propertyAnimator = UIViewPropertyAnimator(duration: animationDuration, timingParameters: timingCurveProvider)

propertyAnimator.addAnimations {
if isPresentation {
if let presentingViewController = transitionContext.viewController(forKey: .from) {
presentingViewController.view.transform = CGAffineTransform(scaleX: self.presentation.depthScale,
y: self.presentation.depthScale)
if UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20 {
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: presentingViewController.view.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 38.5, height: 38.5)).cgPath
presentingViewController.view.layer.mask = maskLayer
}
}
} else {
transitionContext.viewController(forKey: .to)!.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}
controllerToAnimate.view.frame = finalFrame
}

Expand Down
5 changes: 5 additions & 0 deletions Jelly/Classes/private/PresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ protocol PresentationControllerProtocol {
func updatePresentation(presentation: Presentation, duration: Duration)
var dimmingView: UIView { get }
var blurView: UIVisualEffectView { get }
var screenshot: UIView { get }
}

/// A PresentationController tells UIKit what exactly to do with the View that should be presented
Expand All @@ -17,6 +18,7 @@ final class PresentationController: UIPresentationController, PresentationContro
// using an edge pan gesture. This must be done, because it wont work other wise with non full screen viewController transitions
private(set) var dimmingView: UIView = UIView()
private(set) var blurView: UIVisualEffectView = UIVisualEffectView()
private(set) var screenshot: UIView = UIView()

init(presentedViewController: UIViewController, presentingViewController: UIViewController?, presentation: Presentation) {
self.presentation = presentation
Expand Down Expand Up @@ -45,6 +47,8 @@ final class PresentationController: UIPresentationController, PresentationContro
self.setupDimmingView(withAlpha: alpha)
animateDimmingView(alpha: alpha)
}
screenshot = presentingViewController.view.snapshotView(afterScreenUpdates: false)!
presentingViewController.view!.addSubview(screenshot)
}

override func dismissalTransitionWillBegin() {
Expand All @@ -55,6 +59,7 @@ final class PresentationController: UIPresentationController, PresentationContro
case .dimmed:
animateDimmingView(alpha: 0.0)
}
screenshot.removeFromSuperview()
}

override func containerViewWillLayoutSubviews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public struct CoverPresentation: Presentation,
public var presentationAlignment: PresentationAlignmentProtocol
public var spring: Spring
public var marginGuards: UIEdgeInsets
public var depthScale: CGFloat
public var interactionConfiguration: InteractionConfiguration?

public init(directionShow: Direction,
Expand All @@ -23,6 +24,7 @@ public struct CoverPresentation: Presentation,
size: PresentationSizeProtocol = PresentationSize(),
alignment: PresentationAlignmentProtocol = PresentationAlignment.centerAlignment,
marginGuards: UIEdgeInsets = .zero ,
depthScale: CGFloat = 1.0,
timing: PresentationTimingProtocol = PresentationTiming(),
spring: Spring = .none,
interactionConfiguration: InteractionConfiguration? = nil) {
Expand All @@ -35,6 +37,7 @@ public struct CoverPresentation: Presentation,
self.presentationAlignment = alignment
self.spring = spring
self.marginGuards = marginGuards
self.depthScale = depthScale
self.presentationAlignment = alignment
self.interactionConfiguration = interactionConfiguration
}
Expand Down

0 comments on commit ea91a94

Please sign in to comment.