1 //  Copyright (c) 2018, Applidium. All rights reserved
2 //  OverlayContainerPresentationDimmingView.swift
3 //  OverlayContainer
4 //
5 //  Created by Gaétan Zanella on 08/04/2020.
6 //
7 
8 import UIKit
9 
10 /// An interface that enables a view to coordinate its appearance change alongside the container presentation animations and the presented overlay translations.
11 public protocol OverlayContainerSheetDimmingView: UIView {
12     /// Notifies the view that the presentation animations are about to start.
presentationTransitionWillBeginnull13     func presentationTransitionWillBegin()
14     /// Notifies the view that the presentation animations starts. This method is called in an animation block.
15     func presentationTransitionDidBegin()
16     /// Notifies the view that the dismissal animations are about to start.
17     func dismissalTransitionWillBegin()
18     /// Notifies the view that the dismissal animations starts. This method is called in an animation block.
19     func dismissalTransitionDidBegin()
20     /// Notifies the view that a presented overlay is about to be moved. This method is called in an animation block.
21     ///
22     /// - parameter context: The context object containing information about the current overlay container state.
23     func overlayViewControllerWillTranslate(context: OverlayContainerTransitionCoordinatorContext)
24 }
25 
26 public extension OverlayContainerSheetDimmingView {
27     func presentationTransitionWillBegin() {}
28     func presentationTransitionDidBegin() {}
29     func dismissalTransitionWillBegin() {}
30     func dismissalTransitionDidBegin() {}
31     func overlayViewControllerWillTranslate(context: OverlayContainerTransitionCoordinatorContext) {}
32 }
33 
34 /// An `OverlayContainerSheetDimmingView` class that coordinates its alpha value alongside the container presentation animations
35 /// and the presented overlay translations.
36 open class TransparentOverlayContainerSheetDimmingView: UIView, OverlayContainerSheetDimmingView {
37 
38     /// The view alpha when the container is dismissed.
39     open var dismissedAlpha: CGFloat = 0.0
40     /// The view alpha when the overlay reaches its minimum notch.
41     open var minimumAlpha: CGFloat = 0.3
42     /// The view alpha when the overlay reaches its maximum notch.
43     open var maximumAlpha: CGFloat = 0.6
44 
45     // MARK: - Life Cycle
46 
47     public override init(frame: CGRect) {
48         super.init(frame: frame)
49         setUp()
50     }
51 
52     public required init?(coder aDecoder: NSCoder) {
53         super.init(coder: aDecoder)
54         setUp()
55     }
56 
57     // MARK: - OverlayContainerSheetDimmingView
58 
presentationTransitionWillBeginnull59     open func presentationTransitionWillBegin() {
60         alpha = dismissedAlpha
61     }
62 
presentationTransitionDidBeginnull63     open func presentationTransitionDidBegin() {
64         alpha = minimumAlpha
65     }
66 
dismissalTransitionDidBeginnull67     public func dismissalTransitionDidBegin() {
68         alpha = dismissedAlpha
69     }
70 
overlayViewControllerWillTranslatenull71     public func overlayViewControllerWillTranslate(context: OverlayContainerTransitionCoordinatorContext) {
72         let target = minimumAlpha + context.translationProgress() * (maximumAlpha - minimumAlpha)
73         alpha = max(min(target, 1.0), 0)
74     }
75 
76     // MARK: - Private
77 
setUpnull78     private func setUp() {
79         backgroundColor = .black
80     }
81 }
82