2

I have a View that contains a HStack and a DatePicker. When you tap on the HStack, the DatePicker is shown / hidden. I want to animate this action like the animation of Starts and Ends row in iOS Calendar's New Event View.

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))
        }
    }
}

I have used the following code for animation. It almost works. The only problem is that HStack jumps. And I can not fix it.

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)
    }
}

How to fix this issue? How to animate visibility of the DatePicker?

koen
  • 5,383
  • 7
  • 50
  • 89
mahan
  • 12,366
  • 5
  • 48
  • 83

1 Answers1

1

It's simple, you don't need extra ViewModifier

    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 {
                    isDatePickerVisible.toggle()
                }
                
                if isDatePickerVisible {
                    DatePicker(""
                         , selection: $model.startTime
                         , displayedComponents: [.hourAndMinute]
                    ).datePickerStyle(WheelDatePickerStyle())
                }
            }.animation(.spring())
        }
    }
Quang Hà
  • 4,613
  • 3
  • 24
  • 40