https://github.com/simplisticated/direct
Library for transition between screens in iOS app
https://github.com/simplisticated/direct
screen swift transition uiwindow
Last synced: 9 months ago
JSON representation
Library for transition between screens in iOS app
- Host: GitHub
- URL: https://github.com/simplisticated/direct
- Owner: simplisticated
- License: mit
- Created: 2018-01-05T19:49:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-09T14:21:33.000Z (over 7 years ago)
- Last Synced: 2025-04-03T08:38:12.495Z (9 months ago)
- Topics: screen, swift, transition, uiwindow
- Language: Swift
- Homepage:
- Size: 838 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## At a Glance
`Direct` simplifies transitions between screens in iOS app.
## How To Get Started
- Copy content of `Source` folder to your project.
or
- Use `Direct` cocoapod
## Requirements
* iOS 9 and later
* Xcode 9 and later
* Swift 4
## Usage
### Preparations
Create extension for `Scene` class:
```swift
extension Scene {
static var main: Scene {
let navigationController = UINavigationController()
navigationController.viewControllers = [
MainViewController(nibName: "MainViewController", bundle: nil)
]
return Scene(rootController: navigationController)
}
}
```
Change `AppDelegate.swift`:
```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Navigator.shared
.createWindow()
.setScene(.main)
return true
}
```
Also, remove reference to window (`var window: UIWindow?`) from `AppDelegate` class.
### Window
With `Navigator` class you can create window in one line of code:
```swift
Navigator.shared.createWindow()
```
If you have a custom window class, it's possible to use it too:
```swift
Navigator.shared.createWindow(ofType: MyWindow.self)
```
It's recommended to use `createWindow()` method in `AppDelegate` (see example in [Preparations](#preparations) section).
Usually, Xcode creates `AppDelegate` class with a reference to `UIWindow` inside:
```swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
}
```
With `Navigator` you don't need to keep this reference, so you can remove it from `AppDelegate` class. Use `Navigator.window` instead.
### Scenes
Scene describes navigation stack including root controller. Here's an example:
```swift
let navigationController = UINavigationController()
navigationController.viewControllers = [
MyViewController(nibName: "MyViewController", bundle: nil)
]
let scene = Scene(rootController: navigationController)
```
It's highly recommended to create extension for `Scene` class and provide static variables describing new scenes like it's done in [Preparations](#preparations) section.
Switching between scenes is simple:
```swift
Navigator.shared.setScene(newScene)
```
### Transitions
Transition is an action that is performed with stack of navigation controller, for example: pushing, presenting, dismissing view controllers, etc. Below you can find list of examples how to manage navigation stack with `Direct` library.
Push view controller:
```swift
let someViewController = SomeViewController(nibName: "SomeViewController", bundle: nil)
Navigator.shared.performTransition(.push(viewController: someViewController, animated: true))
```
Pop:
```swift
Navigator.shared.performTransition(.pop(animated: true))
```
Pop to root view controller:
```swift
Navigator.shared.performTransition(.popToRootViewController(animated: true))
```
Present:
```swift
Navigator.shared.performTransition(.present(viewController: someViewController, animated: true, completion: {
}))
```
Dismiss:
```swift
Navigator.shared.performTransition(.dismiss(animated: true, completion: {
}))
```
Access to current navigation controller:
```swift
if let currentNavigationController = Navigator.shared.scene?.rootNavigationController {
// Do something with current navigation controller
}
```
### Syntax
`Navigator` supports call chains so you can write long expressions:
```swift
Navigator.shared
.createWindow()
.setScene(.main)
.performTransition(.push(viewController: someViewController, animated: false))
.performTransition(.present(viewController: popupViewController, animated: true, completion: {
}))
```
## License
`Direct` is available under the MIT license. See the [LICENSE](./LICENSE) file for more info.