Sam*_*Sam 5 ios swift ios-animations swiftui swiftui-form
我在 SwiftUI 表单中使用 DatePickers 获得了一些奇怪的动画行为。一张图值一千字,所以我敢肯定一个视频值一百万字:https : //imgur.com/a/UHXqXOh
我试图让日期选择器在表单中展开然后折叠,就像在 Calendar.app 中创建新事件时的行为一样
发生在我身上的是:
这些行为仅在表单中某处存在非 DatePicker 元素(例如 Text、Slider)时才会发生(不必在该特定部分)
这是我的内容视图:
struct ContentView: View {
@State var date = Date()
@State var isDateShown = false
var body: some View {
Form {
Section(header: Text("Title")) {
DatePicker("Test", selection:$date)
DatePicker("Test", selection:$date)
Text("Pick a date").onTapGesture {
withAnimation {
self.isDateShown.toggle()
}
}
if(isDateShown) {
DatePicker("", selection: $date).datePickerStyle(WheelDatePickerStyle()).labelsHidden()
}
}
Section(header: Text("hello")) {
Text("test")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
很高兴提供任何其他需要的东西
以下是 iOS <14 的两种可能的解决方法:1) 简单的一种是完全禁用动画,2) 复杂的一种是通过注入自定义动画修饰符来减轻不正确的动画
使用 Xcode 11.4 / iOS 13.4 测试
1)简单的解决方案- 包装DatePicker到容器中并将动画设置为 nil
VStack {
DatePicker("Test", selection:$date).id(2)
}.animation(nil)
Run Code Online (Sandbox Code Playgroud)
2)配合物溶液-抓斗DatePicker使用)观看偏好读者改变帧ViewHeightKey和b)该动画帧明确使用AnimatingCellHeight从我的其他解决方案。
struct TestDatePickersInForm: View {
@State var date = Date()
@State var isDateShown = false
@State private var height = CGFloat.zero
var body: some View {
Form {
Section(header: Text("Title")) {
// demo of complex solution
VStack {
DatePicker("Test", selection:$date).id(1)
.background(GeometryReader {
Color.clear.preference(key: ViewHeightKey.self,
value: $0.frame(in: .local).size.height) })
}
.onPreferenceChange(ViewHeightKey.self) { self.height = $0 }
.modifier(AnimatingCellHeight(height: height))
.animation(.default)
// demo of simple solution
VStack {
DatePicker("Test", selection:$date).id(2)
}.animation(nil)
Text("Pick a date").onTapGesture {
withAnimation {
self.isDateShown.toggle()
}
}
if(isDateShown) {
DatePicker("", selection: $date).datePickerStyle(WheelDatePickerStyle()).labelsHidden().id(3)
}
}
Section(header: Text("hello")) {
Text("test")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1516 次 |
| 最近记录: |