Skip to content

Commit 230a2ad

Browse files
committed
Add support for dynamic animation durations
1 parent d4cc5b5 commit 230a2ad

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

Example/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ SPEC CHECKSUMS:
3838

3939
PODFILE CHECKSUM: b4841bd82e57283ff97d83f4bb89137cc01f6102
4040

41-
COCOAPODS: 1.11.3
41+
COCOAPODS: 1.14.3

Sources/Stagehand/Animation/Animation.swift

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2019 Square Inc.
2+
// Copyright 2024 Block Inc.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -407,10 +407,44 @@ public struct Animation<ElementType: AnyObject> {
407407
duration: TimeInterval? = nil,
408408
repeatStyle: AnimationRepeatStyle? = nil,
409409
completion: ((_ finished: Bool) -> Void)? = nil
410+
) -> AnimationInstance {
411+
return perform(
412+
on: element,
413+
delay: delay,
414+
durationProvider: FixedDurationProvider(duration: duration ?? implicitDuration),
415+
repeatStyle: repeatStyle,
416+
completion: completion
417+
)
418+
}
419+
420+
/// Perform the animation on the given `element`.
421+
///
422+
/// The duration for each cycle of the animation will be determined in order of preference by:
423+
/// 1. An explicit duration, if provided via the `duration` parameter
424+
/// 2. The animation's implicit duration, as specified by the `implicitDuration` property
425+
///
426+
/// The repeat style for the animation will be determined in order of preference by:
427+
/// 1. An explicit repeat style, if provided via the `repeatStyle` parameter
428+
/// 2. The animation's implicit repeat style, as specified by the `implicitRepeatStyle` property
429+
///
430+
/// - parameter element: The element to be animated.
431+
/// - parameter delay: The time interval to wait before performing the animation.
432+
/// - parameter durationProvider: The duration provider to use for the animation.
433+
/// - parameter repeatStyle: The repeat style to use for the animation.
434+
/// - parameter completion: The completion block to call when the animation has concluded, with a parameter
435+
/// indicated whether the animation completed (as opposed to being cancelled).
436+
/// - returns: An animation instance that can be used to check the status of or cancel the animation.
437+
@discardableResult
438+
public func perform(
439+
on element: ElementType,
440+
delay: TimeInterval = 0,
441+
durationProvider: AnimationDurationProvider,
442+
repeatStyle: AnimationRepeatStyle? = nil,
443+
completion: ((_ finished: Bool) -> Void)? = nil
410444
) -> AnimationInstance {
411445
let driver = DisplayLinkDriver(
412446
delay: delay,
413-
duration: duration ?? implicitDuration,
447+
duration: durationProvider.nextInstanceDuration(),
414448
repeatStyle: repeatStyle ?? implicitRepeatStyle,
415449
completion: completion
416450
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright 2024 Block Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import Foundation
18+
19+
public protocol AnimationDurationProvider {
20+
21+
func nextInstanceDuration() -> TimeInterval
22+
23+
}
24+
25+
// MARK: -
26+
27+
public struct FixedDurationProvider: AnimationDurationProvider {
28+
29+
// MARK: - Life Cycle
30+
31+
public init(duration: TimeInterval) {
32+
self.duration = duration
33+
}
34+
35+
// MARK: - Public Properties
36+
37+
public let duration: TimeInterval
38+
39+
// MARK: - AnimationDurationProvider
40+
41+
public func nextInstanceDuration() -> TimeInterval {
42+
return duration
43+
}
44+
45+
}

0 commit comments

Comments
 (0)