Skip to content

Commit

Permalink
⭐️ Impl: Add swift-programmatic-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed May 22, 2023
1 parent b6ff2dd commit 971602b
Show file tree
Hide file tree
Showing 13 changed files with 2,145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// RNIDynamicModal.swift
// swift-programmatic-modal
//
// Created by Dominic Go on 5/15/23.
//

import UIKit

class RNIDynamicModalAnimator: NSObject, UIViewControllerTransitioningDelegate {

enum Direction {
case left, right, top, bottom;
};

let direction: Direction;
let isPresentation: Bool;

init(
direction: Direction,
isPresentation: Bool
) {
self.direction = direction;
self.isPresentation = isPresentation;

super.init();
};

// MARK: - UIViewControllerTransitioningDelegate
// ---------------------------------------------

func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
return 0.3
}

func animateTransition(
using transitionContext: UIViewControllerContextTransitioning
) {

// 1
let key: UITransitionContextViewControllerKey = isPresentation ? .to : .from

guard let controller = transitionContext.viewController(forKey: key)
else { return }

// 2
if isPresentation {
transitionContext.containerView.addSubview(controller.view)
}

// 3
let presentedFrame = transitionContext.finalFrame(for: controller)
var dismissedFrame = presentedFrame;

switch direction {
case .left:
dismissedFrame.origin.x = -presentedFrame.width

case .right:
dismissedFrame.origin.x = transitionContext.containerView.frame.size.width

case .top:
dismissedFrame.origin.y = -presentedFrame.height

case .bottom:
dismissedFrame.origin.y = transitionContext.containerView.frame.size.height
}

// 4
let initialFrame = isPresentation ? dismissedFrame : presentedFrame
let finalFrame = isPresentation ? presentedFrame : dismissedFrame

// 5
let animationDuration = transitionDuration(using: transitionContext)
controller.view.frame = initialFrame
UIView.animate(
withDuration: animationDuration,
animations: {
controller.view.frame = finalFrame
}, completion: { finished in
if !self.isPresentation {
controller.view.removeFromSuperview()
}
transitionContext.completeTransition(finished)
})
}
};



class RNIDynamicModalViewController: UIViewController {

override func loadView() {
let rootView = UIView();
rootView.backgroundColor = .white;

self.view = rootView;

let label = UILabel();
label.text = "Hello World!";
label.textColor = .black;


rootView.addSubview(label);
label.translatesAutoresizingMaskIntoConstraints = false;

NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
]);
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// RootViewController.swift
// swift-programmatic-modal
//
// Created by Dominic Go on 5/17/23.
//

import UIKit

class RootViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white;

let label = UILabel();
label.text = "Show Modal!";
label.textColor = .black;
label.isUserInteractionEnabled = true;

label.addGestureRecognizer(
UITapGestureRecognizer(
target: self,
action: #selector(self.onPressButton(_:))
)
);

view.addSubview(label);

view.translatesAutoresizingMaskIntoConstraints = false;
label.translatesAutoresizingMaskIntoConstraints = false;


NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
]);

self.view = view
};

@objc func onPressButton(_ sender: UITapGestureRecognizer){

let modalVC = RNIDynamicModalViewController();

// let transitionController = RNIDynamicModalTransitionController(
// presentedViewController: modalVC,
// presenting: self
// );
//
// modalVC.modalPresentationStyle = .custom;
// modalVC.transitioningDelegate = transitionController;

self.present(modalVC, animated: true);
};
};
Loading

0 comments on commit 971602b

Please sign in to comment.