123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- //
- // Created by Tom Baranes on 16/07/16.
- // Copyright © 2016 Jake Lin. All rights reserved.
- //
- import UIKit
- /// `AnimatableModalViewController` is a customised modal view controller used as the `presentedViewController` for `UIPresentationController`. We can use it in Interface Builder to style the modal view and dimming view. Also configure the presentation and dismissal animations.
- open class AnimatableModalViewController: UIViewController, PresentationDesignable {
- // MARK: - AnimatablePresentationController
- public var contextFrameForPresentation: (() -> CGRect)? {
- didSet {
- configurePresenterFrameForPresentation()
- }
- }
- @IBInspectable var _presentationAnimationType: String? {
- didSet {
- if let animationType = PresentationAnimationType(string: _presentationAnimationType) {
- presentationAnimationType = animationType
- }
- }
- }
- public var presentationAnimationType: PresentationAnimationType = .cover(from: .bottom) {
- didSet {
- if oldValue.stringValue != presentationAnimationType.stringValue {
- configurePresenter()
- }
- }
- }
- @IBInspectable var _dismissalAnimationType: String? {
- didSet {
- if let animationType = PresentationAnimationType(string: _dismissalAnimationType) {
- dismissalAnimationType = animationType
- }
- }
- }
- public var dismissalAnimationType: PresentationAnimationType = .cover(from: .bottom) {
- didSet {
- if oldValue.stringValue != dismissalAnimationType.stringValue {
- configurePresenter()
- }
- }
- }
- @IBInspectable public var transitionDuration: Double = .nan {
- didSet {
- presenter?.transitionDuration = transitionDuration
- }
- }
- @IBInspectable var _modalPosition: String? {
- didSet {
- modalPosition = PresentationModalPosition(string: _modalPosition ?? "")
- }
- }
- public var modalPosition: PresentationModalPosition = .center {
- didSet {
- presenter?.presentationConfiguration?.modalPosition = modalPosition
- }
- }
- @IBInspectable var _modalWidth: String? {
- didSet {
- let modalWidth = PresentationModalSize(string: _modalWidth) ?? .default
- modalSize = (modalWidth, modalSize.height)
- }
- }
- @IBInspectable var _modalHeight: String? {
- didSet {
- let modalHeight = PresentationModalSize(string: _modalHeight) ?? .default
- modalSize = (modalSize.width, modalHeight)
- }
- }
- public var modalSize: ModalSize = (.half, .half) {
- didSet {
- presenter?.presentationConfiguration?.modalSize = modalSize
- }
- }
- @IBInspectable public var cornerRadius: CGFloat = .nan {
- didSet {
- presenter?.presentationConfiguration?.cornerRadius = cornerRadius
- }
- }
- @IBInspectable public var dismissOnTap: Bool = true {
- didSet {
- presenter?.presentationConfiguration?.dismissOnTap = dismissOnTap
- }
- }
- @IBInspectable public var backgroundColor: UIColor = .black {
- didSet {
- presenter?.presentationConfiguration?.backgroundColor = backgroundColor
- }
- }
- @IBInspectable public var opacity: CGFloat = 0.7 {
- didSet {
- presenter?.presentationConfiguration?.opacity = opacity
- }
- }
- /// The blur effect style of the dimming view. If use this property, `backgroundColor` and `opacity` are ignored.
- open var blurEffectStyle: UIBlurEffect.Style? {
- didSet {
- presenter?.presentationConfiguration?.blurEffectStyle = blurEffectStyle
- }
- }
- @IBInspectable var _blurEffectStyle: String? {
- didSet {
- blurEffectStyle = UIBlurEffect.Style(string: _blurEffectStyle)
- }
- }
- @IBInspectable public var blurOpacity: CGFloat = .nan {
- didSet {
- presenter?.presentationConfiguration?.blurOpacity = blurOpacity
- }
- }
- @IBInspectable public var shadowColor: UIColor? {
- didSet {
- presenter?.presentationConfiguration?.shadowColor = shadowColor
- }
- }
- @IBInspectable public var shadowRadius: CGFloat = 0.7 {
- didSet {
- presenter?.presentationConfiguration?.shadowRadius = shadowRadius
- }
- }
- @IBInspectable public var shadowOpacity: CGFloat = CGFloat.nan {
- didSet {
- presenter?.presentationConfiguration?.shadowOpacity = shadowOpacity
- }
- }
- @IBInspectable public var shadowOffset: CGPoint = .zero {
- didSet {
- presenter?.presentationConfiguration?.shadowOffset = shadowOffset
- }
- }
- @IBInspectable var _keyboardTranslation: String = "" {
- didSet {
- keyboardTranslation = ModalKeyboardTranslation(string: _keyboardTranslation) ?? .none
- }
- }
- public var keyboardTranslation: ModalKeyboardTranslation = .none {
- didSet {
- presenter?.presentationConfiguration?.keyboardTranslation = keyboardTranslation
- }
- }
- public var presenter: PresentationPresenter?
- // MARK: Life cycle
- public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
- super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
- configurePresenter()
- }
- public required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- configurePresenter()
- }
- // MARK: Life cycle
- override open func viewDidLoad() {
- super.viewDidLoad()
- }
- open override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- configureDismissalTransition()
- }
- open override func viewDidLayoutSubviews() {
- super.viewDidLayoutSubviews()
- configurePresenterFrameForPresentation()
- }
- }
|