-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cross-platform SwiftUI wrapper for AnimatedSwitch
- Loading branch information
Showing
10 changed files
with
489 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Created by Cal Stephens on 8/11/23. | ||
// Copyright © 2023 Airbnb Inc. All rights reserved. | ||
|
||
import Lottie | ||
import SwiftUI | ||
|
||
// MARK: - ControlsDemoView | ||
|
||
struct ControlsDemoView: View { | ||
|
||
var body: some View { | ||
List { | ||
LottieSwitchRow( | ||
animationName: "Samples/Switch", | ||
title: "Switch", | ||
onTimeRange: 0.5...1.0, | ||
offTimeRange: 0.0...0.5) | ||
|
||
LottieSwitchRow( | ||
animationName: "Samples/Switch", | ||
title: "Switch (Custom Colors)", | ||
onTimeRange: 0.5...1.0, | ||
offTimeRange: 0.0...0.5, | ||
colorValueProviders: [ | ||
"Checkmark Outlines.Group 1.Stroke 1.Color": [Keyframe(.black)], | ||
"Checkmark Outlines 2.Group 1.Stroke 1.Color": [Keyframe(.black)], | ||
"X Outlines.Group 1.Stroke 1.Color": [Keyframe(.black)], | ||
"Switch Outline Outlines.Fill 1.Color": [ | ||
Keyframe(value: LottieColor.black, time: 0), | ||
Keyframe(value: LottieColor(r: 0.76, g: 0.76, b: 0.76, a: 1), time: 75), | ||
Keyframe(value: LottieColor.black, time: 150), | ||
], | ||
]) | ||
|
||
// TODO: Add SwiftUI support for AnimatedButton | ||
// AnimatedButtonRow( | ||
// animationName: "Samples/TwitterHeartButton", | ||
// title: "Twitter Heart Button", | ||
// playRanges: [ | ||
// .init(fromMarker: "touchDownStart", toMarker: "touchDownEnd", event: .touchDown), | ||
// .init(fromMarker: "touchDownEnd", toMarker: "touchUpCancel", event: .touchUpOutside), | ||
// .init(fromMarker: "touchDownEnd", toMarker: "touchUpEnd", event: .touchUpInside), | ||
// ])) | ||
|
||
LottieSwitchRow( | ||
animationName: "Samples/Issues/issue_1877", | ||
title: "Issue #1877", | ||
onTimeRange: nil, // use the default (0...1) | ||
offTimeRange: nil, // use the default (1...0) | ||
colorValueProviders: ["**.Color": [Keyframe(.black)]]) | ||
} | ||
.navigationTitle("Controls Demo") | ||
} | ||
|
||
} | ||
|
||
extension LottieColor { | ||
static var black: LottieColor { | ||
.init(r: 0, g: 0, b: 0, a: 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Created by Cal Stephens on 8/11/23. | ||
// Copyright © 2023 Airbnb Inc. All rights reserved. | ||
|
||
import Lottie | ||
import SwiftUI | ||
|
||
// MARK: - LottieSwitchRow | ||
|
||
struct LottieSwitchRow: View { | ||
|
||
// MARK: Internal | ||
|
||
var animationName: String | ||
var title: String | ||
var onTimeRange: ClosedRange<AnimationProgressTime>? | ||
var offTimeRange: ClosedRange<AnimationProgressTime>? | ||
var colorValueProviders: [String: [Keyframe<LottieColor>]] = [:] | ||
|
||
var body: some View { | ||
HStack { | ||
LottieSwitch(animation: .named(animationName)) | ||
.isOn($isOn) | ||
.onAnimation( | ||
fromProgress: onTimeRange?.lowerBound ?? 0, | ||
toProgress: onTimeRange?.upperBound ?? 1) | ||
.offAnimation( | ||
fromProgress: offTimeRange?.lowerBound ?? 1, | ||
toProgress: offTimeRange?.upperBound ?? 0) | ||
.colorValueProviders(colorValueProviders) | ||
.frame(width: 80, height: 80) | ||
|
||
Text(verbatim: "\(title) (isOn=\(isOn))") | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
@State private var isOn = false | ||
} | ||
|
||
extension LottieSwitch { | ||
func colorValueProviders(_ colorValueProviders: [String: [Keyframe<LottieColor>]]) -> Self { | ||
var copy = self | ||
|
||
for (keypath, keyframes) in colorValueProviders { | ||
copy = copy.valueProvider( | ||
ColorValueProvider(keyframes), | ||
for: AnimationKeypath(keypath: keypath)) | ||
} | ||
|
||
return copy | ||
} | ||
} |
Oops, something went wrong.