Skip to content

Commit

Permalink
Merge pull request #1 from stonko1994/feature/add-orientation-config-…
Browse files Browse the repository at this point in the history
…option

Add possibility to configure the animation orientation
  • Loading branch information
stonko1994 authored May 24, 2023
2 parents a6f2f24 + dc4e4fc commit 0628973
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Foundation
import SwiftUI

class DelayedPresentationConfiguration: ObservableObject {
enum DefaultValues {
static let animationDuration: Double = 0.6
static let delay: Double = 0.2
static let offset: Double = 20
static let orientation: Axis.Set = .vertical
}

/// Acts as the trigger for the animation
Expand All @@ -16,14 +17,18 @@ class DelayedPresentationConfiguration: ObservableObject {
let delay: Double
/// Define the movement when the view appears
let offset: Double
/// Define the orientation when the view appears
let orientation: Axis.Set

init(
animationDuration: Double,
delay: Double,
offset: Double
offset: Double,
orientation: Axis.Set
) {
self.animationDuration = animationDuration
self.delay = delay
self.offset = offset
self.orientation = orientation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ struct DelayedPresentationViewModifier: ViewModifier {
let showAnimationDuration: Double?
let itemDelay: Double?
let offset: Double?
let orientation: Axis.Set?

func body(content: Content) -> some View {
content
.opacity(delayedPresentationState.isVisible ? 1 : 0)
.offset(y: delayedPresentationState.isVisible ? 0 : (offset ?? delayedPresentationState.offset))
.modifier(ConditionalOffsetViewModifier(offset: offset, orientation: orientation))
.animation(
.easeOut(duration: showAnimationDuration ?? delayedPresentationState.animationDuration)
.delay(0.1 + (Double(viewNumber) * (itemDelay ?? delayedPresentationState.delay))),
Expand All @@ -20,25 +21,45 @@ struct DelayedPresentationViewModifier: ViewModifier {
}
}

private struct ConditionalOffsetViewModifier: ViewModifier {
@EnvironmentObject var delayedPresentationState: DelayedPresentationConfiguration

let offset: Double?
let orientation: Axis.Set?

func body(content: Content) -> some View {
if (orientation ?? delayedPresentationState.orientation) == .vertical {
content
.offset(y: delayedPresentationState.isVisible ? 0 : (offset ?? delayedPresentationState.offset))
} else {
content
.offset(x: delayedPresentationState.isVisible ? 0 : (offset ?? delayedPresentationState.offset))
}
}
}

public extension View {
/// Enables the view to be transitioned. Must be used within a `DelayedTransitionView`
/// - Parameters:
/// - viewIndex: The index of the view. Starts at `0`.
/// - animationDuration: The duration of the animation in seconds. Default `0.6`. Overrides the value of `DelayedTransitionView`.
/// - delay: The delay before the animation starts. Default `0.2`. Overrides the value of `DelayedTransitionView`.
/// - offset: The `y` offset for the view which will be animated. Default is `20`. Overrides the value of `DelayedTransitionView`.
/// - orientation: The orientation in which the view should appear. Default is `.vertical`. Overrides the value of `DelayedTransitionView`.
func delayedPresentation(
viewIndex: Int,
animationDuration: Double? = nil,
delay: Double? = nil,
offset: Double? = nil
offset: Double? = nil,
orientation: Axis.Set? = nil
) -> some View {
modifier(
DelayedPresentationViewModifier(
viewNumber: viewIndex,
showAnimationDuration: animationDuration,
itemDelay: delay,
offset: offset
offset: offset,
orientation: orientation
)
)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/DelayedTransitionView/DelayedTransitionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ public struct DelayedTransitionView<Content: View>: View {
/// - animationDuration: The duration of the animation in seconds. Default `0.6`
/// - delay: The delay before the animation starts. Default `0.2`
/// - offset: The `y` offset for the view which will be animated. Default is `20`
/// - orientation: The orientation in which the view should appear. Default is `.vertical`
/// - content: A view builder that creates the content of this stack.
public init(
animationDuration: Double? = nil,
delay: Double? = nil,
offset: Double? = nil,
orientation: Axis.Set? = nil,
@ViewBuilder content: @escaping () -> Content
) {
self.content = content

let configuration = DelayedPresentationConfiguration(
animationDuration: animationDuration ?? DelayedPresentationConfiguration.DefaultValues.animationDuration,
delay: delay ?? DelayedPresentationConfiguration.DefaultValues.delay,
offset: offset ?? DelayedPresentationConfiguration.DefaultValues.offset
offset: offset ?? DelayedPresentationConfiguration.DefaultValues.offset,
orientation: orientation ?? DelayedPresentationConfiguration.DefaultValues.orientation
)
self._delayedPresentationConfiguration = StateObject(wrappedValue: configuration)
}
Expand Down

0 comments on commit 0628973

Please sign in to comment.