Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom transitions #11

Merged
merged 17 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update readme.
  • Loading branch information
erikdrobne committed May 15, 2023
commit f5e5b08441d37d533ec2af799ea27652d463923a
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import SwiftUICoordinator

class FadeTransition: NSObject, Transition {
func isEligible(from fromRoute: NavigationRoute, to toRoute: NavigationRoute) -> Bool {
return (fromRoute as? CustomShapesRoute == .customShapes && toRoute as? CustomShapesRoute == .star) ||
(fromRoute as? CustomShapesRoute == .customShapes && toRoute as? CustomShapesRoute == .tower)
return (fromRoute as? CustomShapesRoute == .customShapes && toRoute as? CustomShapesRoute == .star)
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
Expand Down
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ This protocol defines the available routes for navigation within a coordinator f
public protocol NavigationRoute {
/// This title can be used to set the navigation bar title when the route is shown.
var title: String? { get }
/// The type of transition to be used when the route is shown.
/// This can be a push transition, a modal presentation, or `nil` (for child coordinators).
var transition: NavigationTransition? { get }
/// Transition action to be used when the route is shown.
/// This can be a push action, a modal presentation, or `nil` (for child coordinators).
var action: TransitionAction? { get }
}
```

Expand All @@ -78,7 +78,7 @@ public protocol Navigator: ObservableObject {
/// This method is called when the navigator should start navigating.
func start() throws
/// Navigate to a specific route.
/// It creates a view for the route and adds it to the navigation stack using the specified transition.
/// It creates a view for the route and adds it to the navigation stack using the specified action (TransitionAction).
func show(route: Route) throws
/// Sets the navigation stack to a new array of routes.
/// It can be useful if you need to reset the entire navigation stack to a new set of views.
Expand Down Expand Up @@ -140,7 +140,7 @@ enum ShapesRoute: NavigationRoute {
}
}

var transition: NavigationTransition? {
var action: TransitionAction? {
switch self {
case .simpleShapes:
// We have to pass nil for the route presenting a child coordinator.
Expand Down Expand Up @@ -293,6 +293,52 @@ struct ShapesView<Coordinator: Routing>: View {
}
```

### Custom transitions

Create custom transition.

```Swift
class FadeTransition: NSObject, Transition {
func isEligible(from fromRoute: NavigationRoute, to toRoute: NavigationRoute) -> Bool {
return (fromRoute as? CustomShapesRoute == .customShapes && toRoute as? CustomShapesRoute == .star)
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3 // Set the duration of the fade animation
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toView = transitionContext.view(forKey: .to) else {
transitionContext.completeTransition(false)
return
}

let containerView = transitionContext.containerView
toView.alpha = 0.0

containerView.addSubview(toView)

UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
toView.alpha = 1.0
}) { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
```

Register transition in the coordinator initializer.

```Swift
init(startRoute: ShapesRoute? = nil) {
self.navigationController = NavigationController()
self.startRoute = startRoute
super.init()

navigationController.register(FadeTransition())
}
```

## 📒 Example project

For better understanding, I recommend that you take a look at the example project located in the `SwiftUICoordinatorExample` folder.