简体   繁体   English

如何为切换视图设置动画,使其向下而不是向上滑动?

[英]How can I animate a toggled view so that it slides downwards, and not upwards?

I have the following pseudocode:我有以下伪代码:

VStack {
  Image1.gesture(
    TapGesture().onEnd({showDetails.toggle()})
  )
  if showDetails:
    Text
  Image2
}

GIF: https://www.loom.com/share/faff28b4a2764fa2a61d826a12a3e0b6 GIF: https://www.loom.com/share/faff28b4a2764fa2a61d826a12a3e0b6

When I tap Image1, the details expand below.当我点击 Image1 时,详细信息将在下面展开。

I wanted to make this transition more smooth, by having it slide instead of suddenly appear.我想让这个过渡更加平滑,让它滑动而不是突然出现。

So I tried to change所以我试图改变

  Image1.gesture(
    TapGesture().onEnd({showDetails.toggle()})
  )

to

  Image1.gesture(
    TapGesture().onEnd({withAnimation{showDetails.toggle()}})
  )

GIF: https://www.loom.com/share/09c0544108174bf3ac5ef518d2c7d21c GIF: https://www.loom.com/share/09c0544108174bf3ac5ef518d2c7d21c

However, this causes the Image1 to move upwards, which is not what I want.但是,这会导致 Image1 向上移动,这不是我想要的。 I want Image2 to move downwards.我希望 Image2 向下移动。

Does anyone have any suggestions?有没有人有什么建议?

check this out:看一下这个:

i just put the explanation under the image with zstack and move it down on toggle.我只是用 zstack 将解释放在图像下,然后在切换时将其向下移动。

struct ContentView: View {

    @State var showDetails = false

    var body: some View {
        VStack {

            ZStack {
                Text("explanation")
                    .offset(y: showDetails ? 200 : 0)
                Image(systemName: "circle")
                    .resizable()
                    .frame(width:300,height: 300)
                    .background(Color.red)
                    .onTapGesture {
                        withAnimation{
                            self.showDetails.toggle()
                        }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向上取消ViewControll而不是向下取消 - Dismissing a ViewControll Upwards instead of downwards 如何从Alpha值1到0设置视图动画,隐藏视图,从0到1设置视图动画,然后取消隐藏视图? - How can I animate a view from alpha value 1 to 0, hide the view, animate the view from 0 to 1, then unhide the view? 如何在iPhone应用程序中为此自定义视图转换设置动画? - How can I animate this custom view transition in my iPhone app? 如何沿曲线路径设置视图或图像的移动动画? - How can I animate the movement of a view or image along a curved path? 如何在 Swift UI 中向上移动水平堆栈 - How can i move a Horizontal Stack upwards in Swift UI 如何为两个模态视图控制器之间的过渡设置动画? - How can I animate the transition between two modal view controllers? 当用户触摸文本视图时,如何在显示键盘的情况下向上推视图? - How would I push a view upwards when keyboard is shown when user touches a text view? 我可以为视图设置动画以使其从隐藏状态淡入吗? - Can I animate a view to fade in from hidden? 如何在Scroll上为UICollectionViewCell的大小设置动画,以使中间的最大? - How Can I Animate the Size of A UICollectionViewCell on Scroll so the middle one is largest? 如何使pageControl滑动效果? 滚动视图至今连续滑动 - How to make the pageControl swipe effect? The scroll view slides continuously so far
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM