SwiftUI – 如何动画化(maxHeight: .infinity)?

huangapple go评论85阅读模式
英文:

SwiftUI - How to animate (maxHeight: .infinity)?

问题

我将为您翻译所需的部分:

"I simplified my code into the example below.
将我的代码简化为以下示例。

After the view above has animated, its size changed. How can I apply animation to the frame of the view below?
在上面的视图动画完成后,它的大小发生了变化。我如何对下面视图的框架应用动画?

  1. View1()
  2. View2()
  3. .frame(maxHeight: .infinity)
  4. }
  5. struct View1: View {
  6. @State private var animate = false
  7. var body: some View {
  8. Circle()
  9. .frame(animate ? 100 : 200)
  10. .onTapGesture {
  11. animate.toggle()
  12. }
  13. .animation(.easeInOut, value: animate)
  14. }
  15. }
  16. struct View2: View {
  17. var body: some View {
  18. Color.red
  19. }
  20. }

I tried this, it works. But everything else inside View2 would also animate, which is not what I want.
我尝试过这个,它可以工作。但是 View2 中的其他内容也会进行动画,这不是我想要的。

  1. .animation(.easeInOut)
  2. ```"
  3. <details>
  4. <summary>英文:</summary>
  5. I simplified my code into the example below.
  6. After the view above has animated, its size changed. How can I apply animation to the frame of the view below?

VStack {
View1()

  1. View2()
  2. .frame(maxHeight: .infinity)

}

struct View1: View {
@State private var animate = false
var body: some View {
Circle()
.frame(animate ? 100 : 200)
.onTapGesture {
animate.toggle()
}
.animation(.easeInOut, value: animate)
}
}

struct View2: View {
var body: some View {
Color.red
}
}

  1. I tried this, it works. But everything else inside View2 would also animate, which is not what I want.

View2()
.animation(.easeInOut)

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 看一下`.transaction`修饰符。
  5. ```swift
  6. View2()
  7. .frame(maxHeight: .infinity)
  8. .transaction { transform in
  9. transform.animation = .easeInOut
  10. }
英文:

Look at the .transaction modifier.

  1. View2()
  2. .frame(maxHeight: .infinity)
  3. .transaction { transform in
  4. transform.animation = .easeInOut
  5. }

答案2

得分: 1

如果我理解正确:

  • 您在VStack中有两个视图
  • 当顶部视图变小时,您希望底部视图填充空间,带有动画效果
  • 底部视图的内容不应该有过多的动画效果。

我认为实际上您需要关注的是VStackVStack实际上会自动为其内容的更改添加动画效果,因此只需在切换标志时使用withAnimation,而不是显式使用.animation修饰符。

我认为以下代码可以实现您的需求:

  1. struct View1: View {
  2. @State private var animate = false
  3. var body: some View {
  4. Circle()
  5. .frame(width: animate ? 100 : 200, height: animate ? 100 : 200)
  6. .onTapGesture {
  7. withAnimation {
  8. animate.toggle()
  9. }
  10. }
  11. }
  12. }
  13. struct View2: View {
  14. var body: some View {
  15. Color.red
  16. }
  17. }
  18. struct ContentView: View {
  19. var body: some View {
  20. VStack {
  21. View1()
  22. View2()
  23. .frame(maxHeight: .infinity)
  24. }
  25. }
  26. }

所以总结一下,使用withAnimation而不是.animation()

英文:

If I understand correctly:

  • you have two views in a VStack
  • when the top view gets smaller, you want the bottom view to fill the space, with animation
  • the content of the bottom view should not be over-animated.

I think it is actually the VStack where you need to focus on. A VStack actually animates changes to its contents anyway, so it is enough if you use withAnimation to toggle the flag, instead of an explicit .animation modifier.

I think this works as you were wanting:

  1. struct View1: View {
  2. @State private var animate = false
  3. var body: some View {
  4. Circle()
  5. .frame(width: animate ? 100 : 200, height: animate ? 100 : 200)
  6. .onTapGesture {
  7. withAnimation {
  8. animate.toggle()
  9. }
  10. }
  11. }
  12. }
  13. struct View2: View {
  14. var body: some View {
  15. Color.red
  16. }
  17. }
  18. struct ContentView: View {
  19. var body: some View {
  20. VStack {
  21. View1()
  22. View2()
  23. .frame(maxHeight: .infinity)
  24. }
  25. }
  26. }

So in summary, use withAnimation instead of .animation().

huangapple
  • 本文由 发表于 2023年7月12日 20:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定