我正在构建一个简单的旋转圆来加载动画.我让圆圈旋转,但容器视图在动画期间也在向下移动.我想不出是什么原因导致了这种奇怪的行为.

Example:

enter image description here

白色边框是父视图ZStack.红色边框是我放置的VStack,用来展示这个奇怪的动画.蓝色边框是ZStack,它包含两个设置动画的Circle.

Here is the code:

struct CustomLoadingView: View {

  let viewWidth: CGFloat = UIScreen.main.bounds.width * 0.5
  let backgroundCircleLineWidth: CGFloat = UIScreen.main.bounds.width * 0.025
  let foregroundCircleLineWidth: CGFloat = UIScreen.main.bounds.width * 0.02

  @State var rotationDegree: Angle = Angle.degrees(0)

  var body: some View {
    VStack(spacing: 0) {
        ZStack {
            Circle()
              .stroke(style: StrokeStyle(lineWidth: backgroundCircleLineWidth))
              .fill(Global.Colors.primary60)
            
            Circle()
              .trim(from: 0, to: 0.15)
              .stroke(style: StrokeStyle(lineWidth: 7, lineCap: .round))
              .fill(Global.Colors.secondary50)
              .rotationEffect(self.rotationDegree)
        }
          .frame(width: viewWidth, height: viewWidth)
          .border(Color.blue)
          .animation(Animation.linear(duration: 4.5).repeatForever(autoreverses: false), value: rotationDegree)
          .onAppear() {
              self.animateLoader()
          }
    }
      .border(Color.red)
}

   func animateLoader() {
      self.rotationDegree = .degrees(720)
   }
}

你知道为什么会发生这种事吗?我怎么才能让它停下来?谢谢.

推荐答案

我在Vacawama的帮助下想出了答案,他把to a thread in the comments道题联系了起来.这条帖子还回答了另外helpful link as well.个问题,这些问题基本上与我在这里遇到的问题相同.

Here's the short and sweet:

归根结底,它是隐含的动画与显式的动画.在我的代码中,这里,我使用了一个"显式"动画.而使用"隐式"动画(withAnimation())则可防止该问题.

显然,这可能与View被导航到时View的动画制作方式有关.就像我的情况一样,我在导航时展示了这View个.所以"导航动画"和我的显性动画混在了一起.而且,通过使用隐式动画,SwiftUI足够聪明,可以确定导航动画不应该是导航动画的一部分.

Here's the code:

var body: some View {
    ZStack {
        Circle()
          .stroke(style: StrokeStyle(lineWidth: backgroundCircleLineWidth))
          .fill(Global.Colors.primary60)
        
        Circle()
          .trim(from: 0, to: 0.15)
          .stroke(style: StrokeStyle(lineWidth: foregroundCircleLineWidth, lineCap: .round))
          .fill(Global.Colors.secondary50)
          .rotationEffect(self.rotationDegree)
    }
      .frame(width: viewWidth, height: viewWidth)
      .onAppear() {
          DispatchQueue.main.async {
              withAnimation(Animation.linear(duration: 2.5).repeatForever(autoreverses: false)) {
                  self.rotationDegree = .degrees(720)
              }
          }
      }
}

我只是在withAnimation()个区块内运行动画.现在,它的运行情况与预期相符.

Ios相关问答推荐

缺少预期的键:';NSPrival yCollectedDataTypes';

从后台线程调用DispatchObject的方法安全吗?

如何在SwiftUI中顺时针和逆时针两个方向应用旋转效果?

在滚动视图中固定页眉,但在页眉上方嵌入元素

SwiftUI标题/标题文本背景 colored颜色 屏幕宽度

Xcode 15模拟器用x86_64编译

为什么在Actor内部使用withTaskGroupwork并行运行?

如何在 SwiftUI 中对表格行使用 Transferable

SwiftUI 图像与下拉视图切换一起 skip

如何在 SwiftUI 的 TabView 中添加底部曲线?

如何为视图的 colored颜色 和宽度设置动画?

在 iPad 上删除列表的顶部和底部行

使用 @MainActor 更新 UI 的适当策略是什么?

如何在不同设备的segment控制中管理文本大小(text size)?

每次运行后,Xcode 6 都会在 iOS8 模拟器中重命名我的应用程序目录.

构建失败:ld:重复符号 _OBJC_CLASS_$_Algebra5FirstViewController

如何判断是否为 64 位构建了静态库?

以编程方式 Select UITextField 中的所有文本

呈现和关闭模态视图控制器

是否可以将 Socket.io 与 AWS Lambda 一起使用?