-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ Impl: Add
swift-programmatic-modal
- Loading branch information
1 parent
b6ff2dd
commit 971602b
Showing
13 changed files
with
2,145 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
experiments/swift-programmatic-modal/RNIDynamicModal/RNIDynamicModal.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
]); | ||
}; | ||
}; |
55 changes: 55 additions & 0 deletions
55
experiments/swift-programmatic-modal/RNIDynamicModal/RootViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}; |
Oops, something went wrong.