1

This is a follow-up question to the following topic.

How can I fix the "Hello World" card inside the row to the top of its cell? It shouldn't move when the second card is coming out by tapping.

Current State:

Screen Shot

But inside the row cell it should be alignment to it's top like sketched in this image:

Screen Shot

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello!")
            List {
                Detail(isExpanded: false)
                Detail(isExpanded: false)
                Detail(isExpanded: false)
            }
        }
    }
}

struct Detail: View {
@State var isExpanded :Bool = false
var body: some View {
    VStack {
        ZStack (alignment: .bottom){
            ZStack (alignment: .center) {
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color(red: 0.0, green: 1.0, blue: 1.0, opacity: 0.5)).frame(height: 115)
                Text("Helloo!")
            }.zIndex(3).frame(height: 115).contentShape(Rectangle()).onTapGesture {
                withAnimation (.linear(duration: 0.1)){
                    self.isExpanded.toggle()
                }
            }
            Button(action: {
            }) {
                ZStack {
                    RoundedRectangle(cornerRadius: 50)
                        .fill(Color(red: 1.0, green: 0.0, blue: 1.0, opacity: 0.5)).frame(height: 70)
                        .cornerRadius(8)
                        .shadow(radius: 3)
                    Text("Test")
                }
            }.padding([.leading, .trailing], 12)
                //.padding(.top, 6)
                .frame(height: 70)
                .buttonStyle(BorderlessButtonStyle())
                .offset(y: self.isExpanded ? 80 : 0)
                .disabled(!self.isExpanded)
        }
        if(self.isExpanded) {
            Spacer()
        }
        
    }.modifier(AnimatingCellHeight(height: self.isExpanded ? 210 : 115)).background(Color(red: 1.0, green: 0.5, blue: 1, opacity: 0.5))
    
 }
}

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

EDIT

The end result should look like this. The inverted isExpanded Bool is my fault...should be the other way around.

Screen Shot

EDIT 2 Code above is updated to the newest try. which you will see in the gif below.

Almost got there....The blue card which I am pressing to expand is moving a little bit up and down (you can clearly see when increasing the click frequency) in the following gif. Otherwise the behaviour is perfect, just the card should do this little wiggle up and down...

Screen Shot

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
cocos2dbeginner
  • 2,185
  • 1
  • 31
  • 58
  • Well, actually as for it is not clear what should be achieved at the end (also that *inverted* "isExpanded" flag confuses me). Should all content just be moved to the top (then why is that free space below)? Should Hello button be resized to top? How should new layout behave on animation? etc. ... Because strict understanding of your request resulted in what Chris proposed below (my first try was the same, but I did not like what appeared). – Asperi May 20 '20 at 15:47
  • the inverted flag is my fault. You're right. But I added a screenshot of what I am trying to achieve. From a concept point of view Chris is very close. But the blue card is moving a little bit up and down upon a click. It should just stay steady without moving. And the second view underneath should be moved below the "Hello!" card (like in the drawing). – cocos2dbeginner May 20 '20 at 16:16

1 Answers1

1

do you mean this way? (added VStack + Spacer)

struct Detail: View {
    @State var isExpanded :Bool = false
    var body: some View {
        VStack {
        ZStack {
            ZStack {
                RoundedRectangle(cornerRadius: 8)
                .fill(Color(red: 0.0, green: 1.0, blue: 1.0, opacity: 1.0)).frame(height: 115)
                Text("Hello!")
            }.zIndex(3).frame(height: 115).contentShape(Rectangle()).onTapGesture {
                withAnimation {
                    self.isExpanded.toggle()
                }
            }
            Button(action: {
            }) {
                ZStack {
                    RoundedRectangle(cornerRadius: 50)
                        .fill(Color(red: 1.0, green: 0.0, blue: 1.0, opacity: 1.0)).frame(height: 70)
                        .cornerRadius(8)
                        .shadow(radius: 3)
                        Text("Test")
                }
            }.padding([.leading, .trailing], 12)
                .padding(.top, 6)
                .frame(height: 70)
                .buttonStyle(BorderlessButtonStyle())
                .offset(y: self.isExpanded ? 0 : 40)
                .disabled(!self.isExpanded)
        }
            Spacer()
        }.modifier(AnimatingCellHeight(height: self.isExpanded ? 120 : 200))
    }
}

enter image description here

Chris
  • 7,579
  • 3
  • 18
  • 38
  • Very close. When you click on "Hello!" the "Hello!" rect is moving like 2points up and down. It should just stay completely steady without movement. Only the purple view should move. The remaining list is of course allowed to move since more space is being occupied. Other than that. That's what I am trying to achieve. – cocos2dbeginner May 20 '20 at 16:12
  • I also added a screenshot of what I am trying to achieve in my post. But if you know how to keep the card steady I'll just accept the answer. I can handle the rest. I just need the concept of how it works. – cocos2dbeginner May 20 '20 at 16:12
  • I am busy with something similar, have the same issue. It is jumping. I wonder if this is on purpose as in intended by design; kind of ( a failed ) string effect? If not intended, did you get this solved? – iPadawan Jul 08 '20 at 14:00
  • BTW, I am not using a List, but the effect is almost the same, just a little move. Did test with and without a List container :( – iPadawan Jul 08 '20 at 14:14