我正在建立一个简单的设置屏幕。
然后激活第一个设置时,将显示速度控制。关闭时,速度控制将消失。
根据我对SwiftUI的了解,这应该根据以下代码自动进行动画处理,但它只会出现并消失。
如何通过SwiftUI使动画更好,因此它会从其上方的单元格向下滑动,就像在this presentation中53:00一样?
import SwiftUI
import Combine
struct ContentView : View {
@ObjectBinding var settingsStore: SettingsStore
var body: some View {
NavigationView {
Form {
Toggle(isOn: $settingsStore.settingActivated.animation(.basic(duration: 3, curve: .easeInOut))) {
Text("Setting Activated")
}
if settingsStore.settingActivated {
VStack(alignment: .leading) {
Text("Speed Control")
HStack {
Image(systemName: "tortoise")
Slider(value: .constant(10), from: 0, through: 50, by: 1)
Image(systemName: "hare")
}
}.transition(.opacity)
}
}.navigationBarTitle(Text("Settings"))
}
}
}
class SettingsStore: BindableObject {
let didChange = PassthroughSubject<Void, Never>()
var settingActivated: Bool = UserDefaults.settingActivated {
didSet {
UserDefaults.settingActivated = settingActivated
didChange.send()
}
}
}
extension UserDefaults {
private struct Keys {
static let settingActivated = "SettingActivated"
}
static var settingActivated: Bool {
get {
return UserDefaults.standard.bool(forKey: Keys.settingActivated)
}
set {
UserDefaults.standard.set(newValue, forKey: Keys.settingActivated)
}
}
}