【发布时间】:2021-03-03 06:25:56
【问题描述】:
我有一个View,其中包含一个HStack 和一个DatePicker。当您点击HStack 时,DatePicker 会显示/隐藏。我想为这个动作设置动画,就像 iOS 日历的新事件视图中的开始和结束行的动画一样。
struct TimePicker: View {
@Binding var startTime: Date
@State private var isDatePickerVisible: Bool = false
var body: some View {
VStack(alignment: .center) {
HStack {
ListItemView(icon: "start-time",
leadingText: "Start Time",
trailingText: startTime.stringTime())
}
.onTapGesture {
withAnimation {
self.isDatePickerVisible.toggle()
}
}
Group {
if isDatePickerVisible {
DatePicker("", selection: $startTime, displayedComponents: [.hourAndMinute])
.datePickerStyle(WheelDatePickerStyle())
}
}
.background(Color.red)
.modifier(AnimatingCellHeight(height: isDatePickerVisible ? 300 : 0))
}
}
}
我已将以下代码用于动画。它几乎可以工作。唯一的问题是HStack 跳转。而且我无法修复它。
https://stackoverflow.com/a/60873883/8292178
struct AnimatingCellHeight: AnimatableModifier {
var height: CGFloat = 0
var animatableData: CGFloat {
get { height }
set { height = newValue }
}
func body(content: Content) -> some View {
content.frame(height: height)
}
}
如何解决这个问题?如何为DatePicker 的可见性设置动画?
【问题讨论】: