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

slide-inverse #125

Open
lorenzo-vecchio opened this issue Feb 29, 2024 · 0 comments
Open

slide-inverse #125

lorenzo-vecchio opened this issue Feb 29, 2024 · 0 comments

Comments

@lorenzo-vecchio
Copy link

lorenzo-vecchio commented Feb 29, 2024

Would it be possible to add a slide-inverse animation also? I mean in a ltr application from left to right or if you prefer from leading to trailing.
It would be a convenient feature to have out of the box.
It's really fast to code too, I made it in my project like this:

import AtomicTransition
import SwiftUI
import NavigationTransitions

extension AnyNavigationTransition {
    /// A transition that moves both views in and out along the specified axis.
    ///
    /// This transition:
    /// - Pushes views left-to-right and pops views left-to-right when `axis` is `horizontal`.
    /// - Pushes views top-to-bottom and pops views top-to-bottom when `axis` is `vertical`.
    public static func inverseSlide(axis: Axis) -> Self {
        .init(InverseSlide(axis: axis))
    }
}

extension AnyNavigationTransition {
    /// Equivalent to `slide(axis: .horizontal)`.
    @inlinable
    public static var slide: Self {
        .inverseSlide(axis: .horizontal)
    }
}

/// A transition that moves both views in and out along the specified axis.
///
/// This transition:
/// - Pushes views left-to-right and pops views left-to-right when `axis` is `horizontal`.
/// - Pushes views top-to-bottom and pops views top-to-bottom when `axis` is `vertical`.
public struct InverseSlide: NavigationTransition {
    private let axis: Axis

    public init(axis: Axis) {
        self.axis = axis
    }

    /// Equivalent to `Move(axis: .horizontal)`.
    @inlinable
    public init() {
        self.init(axis: .horizontal)
    }

    public var body: some NavigationTransition {
        switch axis {
        case .horizontal:
            MirrorPush {
                OnInsertion {
                    Move(edge: .leading)
                }
                OnRemoval {
                    Move(edge: .trailing)
                }
            }
        case .vertical:
            MirrorPush {
                OnInsertion {
                    Move(edge: .top)
                }
                OnRemoval {
                    Move(edge: .bottom)
                }
            }
        }
    }
}

extension InverseSlide: Hashable {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant