Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
flocked committed Mar 29, 2024
1 parent 0e936d3 commit 712e2af
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 146 deletions.
30 changes: 18 additions & 12 deletions Sources/Anima/Anima/Anima+AnimationOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ public extension Anima {
/// The animation runs backwards and forwards (must be combined with the ``repeats`` option).
public static let autoreverse = AnimationOptions(rawValue: 1 << 2)

/// The animation automatically starts when the ``target`` value changes.
public static let autoStart = AnimationOptions(rawValue: 1 << 3)

/**
The velocity of spring animated properties will be reset.

Usually the animation velocity is perserved when you spring animate it to another value. This option will reset the velocity for any new spring animation.
*/
public static let resetSpringVelocity = AnimationOptions(rawValue: 1 << 3)

public static let resetSpringVelocity = AnimationOptions(rawValue: 1 << 4)
#if os(iOS) || os(tvOS)
/// Prevents the user to interact with views while they are being animated.
public static let preventUserInteraction = AnimationOptions(rawValue: 1 << 4)
public static let preventUserInteraction = AnimationOptions(rawValue: 1 << 5)
#endif

/// Creates a structure that represents animation options.
Expand All @@ -45,20 +48,22 @@ extension Anima.AnimationOptions: CustomStringConvertible {
#if os(iOS) || os(tvOS)
"""
AnimationOptions(
integralizeValues: \(contains(.integralizeValues))
repeats: \(contains(.repeats))
autoreverse: \(contains(.autoreverse))
resetSpringVelocity: \(contains(.resetSpringVelocity))
preventUserInteraction: \(contains(.preventUserInteraction))
integralizeValues: \(integralizeValues)
repeats: \(repeats)
autoreverse: \(autoreverse)
autoStarts: \(autoStarts)
resetSpringVelocity: \(resetSpringVelocity)
preventUserInteraction: \(preventUserInteraction)
)
"""
#else
"""
AnimationOptions(
integralizeValues: \(contains(.integralizeValues))
repeats: \(contains(.repeats))
autoreverse: \(contains(.autoreverse))
resetSpringVelocity: \(contains(.resetSpringVelocity))
integralizeValues: \(integralizeValues)
repeats: \(repeats)
autoreverse: \(autoreverse)
autoStarts: \(autoStarts)
resetSpringVelocity: \(resetSpringVelocity)
)
"""
#endif
Expand All @@ -70,6 +75,7 @@ extension Anima.AnimationOptions {
var integralizeValues: Bool { contains(.integralizeValues) }
var autoreverse: Bool { contains(.autoreverse) }
var resetSpringVelocity: Bool { contains(.resetSpringVelocity) }
var autoStarts: Bool { contains(.autoStart) }
#if os(iOS) || os(tvOS)
var preventUserInteraction: Bool { contains(.preventUserInteraction) }
#endif
Expand Down
16 changes: 8 additions & 8 deletions Sources/Anima/Animations/Decay/DecayAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ open class DecayAnimation<Value: AnimatableProperty>: PropertyAnimation<Value> {
didSet {
guard state != .running, oldValue != _velocity else { return }
_startVelocity = _velocity
if autoStarts, _velocity != .zero {
if options.autoStarts, _velocity != .zero {
start(afterDelay: 0.0)
}
}
Expand Down Expand Up @@ -155,17 +155,17 @@ open class DecayAnimation<Value: AnimatableProperty>: PropertyAnimation<Value> {

let animationFinished = _velocity.magnitudeSquared < 0.05

if animationFinished, repeats {
if animationFinished, options.repeats {
_value = _startValue
_velocity = _startVelocity
}

runningTime = runningTime + deltaTime

let callbackValue = integralizeValues ? value.scaledIntegral : value
let callbackValue = options.integralizeValues ? value.scaledIntegral : value
valueChanged?(callbackValue)

if animationFinished, !repeats {
if animationFinished, !options.repeats {
stop(at: .current)
}
}
Expand All @@ -184,10 +184,10 @@ open class DecayAnimation<Value: AnimatableProperty>: PropertyAnimation<Value> {
decelerationRate: \(decelerationRate)
isReversed: \(isReversed)
repeats: \(repeats)
autoreverse: \(autoreverse)
integralizeValues: \(integralizeValues)
autoStarts: \(autoStarts)
repeats: \(options.repeats)
autoreverse: \(options.autoreverse)
integralizeValues: \(options.integralizeValues)
autoStarts: \(options.autoStarts)
valueChanged: \(valueChanged != nil)
completion: \(completion != nil)
Expand Down
18 changes: 9 additions & 9 deletions Sources/Anima/Animations/Easing/EasingAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class EasingAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
if state == .running {
fractionComplete = 0.0
completion?(.retargeted(from: Value(oldValue), to: target))
} else if autoStarts, _target != _value {
} else if options.autoStarts, _target != _value {
start(afterDelay: 0.0)
}
}
Expand Down Expand Up @@ -96,8 +96,8 @@ open class EasingAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
let animationFinished = (isReversed ? fractionComplete <= 0.0 : fractionComplete >= 1.0) || !isAnimated

if animationFinished {
if repeats, isAnimated {
if autoreverse {
if options.repeats, isAnimated {
if options.autoreverse {
isReversed = !isReversed
}
fractionComplete = isReversed ? 1.0 : 0.0
Expand All @@ -107,10 +107,10 @@ open class EasingAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
}
}

let callbackValue = integralizeValues ? value.scaledIntegral : value
let callbackValue = options.integralizeValues ? value.scaledIntegral : value
valueChanged?(callbackValue)

if (animationFinished && !repeats) || !isAnimated {
if (animationFinished && !options.repeats) || !isAnimated {
stop(at: .current)
}
}
Expand All @@ -137,10 +137,10 @@ open class EasingAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
timingFunction: \(timingFunction.name)
duration: \(duration)
isReversed: \(isReversed)
repeats: \(repeats)
autoreverse: \(autoreverse)
integralizeValues: \(integralizeValues)
autoStarts: \(autoStarts)
repeats: \(options.repeats)
autoreverse: \(options.autoreverse)
integralizeValues: \(options.integralizeValues)
autoStarts: \(options.autoStarts)
valueChanged: \(valueChanged != nil)
completion: \(completion != nil)
Expand Down
38 changes: 13 additions & 25 deletions Sources/Anima/Animations/Property/PropertyAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,15 @@ open class PropertyAnimation<Value: AnimatableProperty>: AnimationProviding, Cus

/// The delay (in seconds) after which the animations begin.
open internal(set) var delay: TimeInterval = 0.0

/// A Boolean value indicating whether the animation repeats indefinitely.
open var repeats: Bool = false

/// A Boolean value indicating whether the animation is running backwards and forwards (must be combined with ``repeats`` `true`).
public var autoreverse: Bool = false

/// Additional animation options.
open var options: Anima.AnimationOptions = []

/// A Boolean value indicating whether the animation is running in the reverse direction.
open var isReversed: Bool = false

/// A Boolean value that indicates whether the value returned in ``valueChanged`` should be integralized to the screen's pixel boundaries. This helps prevent drawing frames between pixels, causing aliasing issues.
open var integralizeValues: Bool = false

/// A Boolean value that indicates whether the animation automatically starts when the ``target`` value changes.
open var autoStarts: Bool = false

var isReversed: Bool = false

var runningTime: TimeInterval = 0.0


/// The _current_ value of the animation. This value will change as the animation executes.
public var value: Value {
get { Value(_value) }
Expand Down Expand Up @@ -100,7 +90,7 @@ open class PropertyAnimation<Value: AnimatableProperty>: AnimationProviding, Cus
guard oldValue != _target else { return }
if state == .running {
completion?(.retargeted(from: Value(oldValue), to: target))
} else if autoStarts, _target != _value {
} else if options.autoStarts, _target != _value {
start(afterDelay: 0.0)
}
}
Expand Down Expand Up @@ -167,9 +157,7 @@ open class PropertyAnimation<Value: AnimatableProperty>: AnimationProviding, Cus
/// Configurates the animation with the specified settings.
func configure(with configuration: Anima.AnimationConfiguration) {
groupID = configuration.groupID
repeats = configuration.options.repeats
autoreverse = configuration.options.autoreverse
integralizeValues = configuration.options.integralizeValues
options = configuration.options
}

/**
Expand All @@ -179,7 +167,7 @@ open class PropertyAnimation<Value: AnimatableProperty>: AnimationProviding, Cus
*/
open func updateAnimation(deltaTime: TimeInterval) {
guard state == .running else { return }
let callbackValue = integralizeValues ? value.scaledIntegral : value
let callbackValue = options.integralizeValues ? value.scaledIntegral : value
valueChanged?(callbackValue)

if _value == _target {
Expand Down Expand Up @@ -281,12 +269,12 @@ open class PropertyAnimation<Value: AnimatableProperty>: AnimationProviding, Cus
target: \(target)
startValue: \(startValue)
velocity: \(velocity)
isReversed: \(isReversed)
repeats: \(repeats)
autoreverse: \(autoreverse)
integralizeValues: \(integralizeValues)
autoStarts: \(autoStarts)
repeats: \(options.repeats)
autoreverse: \(options.autoreverse)
integralizeValues: \(options.integralizeValues)
autoStarts: \(options.autoStarts)
valueChanged: \(valueChanged != nil)
completion: \(completion != nil)
Expand Down
17 changes: 9 additions & 8 deletions Sources/Anima/Animations/Spring/SpringAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ open class SpringAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
let animationFinished = (runningTime >= settlingTime) || !isAnimated

if animationFinished {
if repeats, isAnimated {
if autoreverse {
if options.repeats, isAnimated {
if options.autoreverse {
isReversed = !isReversed
}
_value = isReversed ? _target : _startValue
Expand All @@ -93,10 +93,10 @@ open class SpringAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
runningTime = 0.0
}

let callbackValue = integralizeValues ? value.scaledIntegral : value
let callbackValue = options.integralizeValues ? value.scaledIntegral : value
valueChanged?(callbackValue)

if animationFinished, !repeats || !isAnimated {
if animationFinished, !options.repeats || !isAnimated {
stop(at: .current)
}
}
Expand All @@ -115,11 +115,12 @@ open class SpringAnimation<Value: AnimatableProperty>: PropertyAnimation<Value>
mode: \(spring.response > 0 ? "animated" : "nonAnimated")
settlingTime: \(settlingTime)
isReversed: \(isReversed)
repeats: \(repeats)
autoreverse: \(autoreverse)
integralizeValues: \(integralizeValues)
autoStarts: \(autoStarts)
repeats: \(options.repeats)
autoreverse: \(options.autoreverse)
integralizeValues: \(options.integralizeValues)
autoStarts: \(options.autoStarts)
valueChanged: \(valueChanged != nil)
completion: \(completion != nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- ``integralizeValues``
- ``repeats``
- ``autoreverse``
- ``autoStart``
- ``resetSpringVelocity``
- ``preventUserInteraction``

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
### Accessing animations

- ``Anima/AnimationProvider/animations``
- ``Anima/AnimationProvider/animation(for:)-80x48``

### Accessing animation values

- ``Anima/AnimationProvider/animationValue(for:)-91c63``
- ``Anima/AnimationProvider/animationVelocity(for:)-1k944``
- ``Anima/AnimationProvider/animation(for:)-6k79l``

### Providing animation handlers

- ``Anima/AnimationProvider/setAnimationHandler(_:handler:)-523cw``
- ``Anima/AnimationProvider/setAnimationHandler(_:handler:)-6lu9p``

### Animation provider

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
- ``stop(at:immediately:)``
- ``pause()``
- ``delay``

### Updating the animation

- ``updateAnimation(deltaTime:)``

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,35 @@

### Starting and stopping the animation

- ``start(afterDelay:)``
- ``stop(at:immediately:)``
- ``pause()``
- ``delay``
- ``state``
- ``Anima/PropertyAnimation/start(afterDelay:)``
- ``Anima/PropertyAnimation/stop(at:immediately:)``
- ``Anima/PropertyAnimation/pause()``
- ``Anima/PropertyAnimation/delay``
- ``Anima/PropertyAnimation/state``

### Accessing value, velocity and target

- ``target``
- ``value``
- ``Anima/PropertyAnimation/value``
- ``velocity``

### Handling animation updates

- ``completion``
- ``Anima/PropertyAnimation/completion``
- ``updateAnimation(deltaTime:)``
- ``valueChanged``
- ``Anima/PropertyAnimation/valueChanged``

### Accessing animation parameters

- ``Anima/PropertyAnimation/id``
- ``Anima/PropertyAnimation/groupID``
- ``Anima/PropertyAnimation/relativePriority``
- ``Anima/PropertyAnimation/options``

### Accessing decay parameters

- ``decelerationRate``

- ``id``
- ``groupID``
- ``relativePriority``
### Description

- ``integralizeValues``
- ``autoreverse``
- ``autoStarts``
- ``isReversed``
- ``repeats``
- ``description``
Loading

0 comments on commit 712e2af

Please sign in to comment.