【问题标题】:SwiftUI Strange animation behavior inside a TabViewTabView 中的 SwiftUI 奇怪的动画行为
【发布时间】:2021-01-29 03:03:57
【问题描述】:

我正在为一个栏的进度制作动画(如下),我将它嵌入到 2 个父视图中:父 1 和父 2。两个父视图都是 TabView 中的选项卡。当应用程序在父视图 1 中按预期加载动画时,但是当点击选项卡导航到父视图 2 时,栏似乎从屏幕的左下方飞入? ????

struct BarView: View {
    
    var progress: Double = 0.0
    
     var progressAnimation: Animation {
        Animation
            .linear
            .speed(0.5)
            .delay(0.5)
     }
    
    var body: some View {
        
        ZStack {
                ZStack(alignment: .leading) {
                    RoundedRectangle(cornerRadius: 12.0)
                        .fill(Color(.lightGray))
                        .opacity(0.1)
                        .frame(height: 15)
                        .overlay(GeometryReader { geometry in
                    RoundedRectangle(cornerRadius: 12.0)
                        .fill(getColorForBar(progress: progress))
                        .frame(width: getFillWidth(progress: progress, geometry: geometry), height: 15)
                        .animation(self.progressAnimation)
                         }, alignment: .leading)
                }
        }
    }

【问题讨论】:

  • 进度动画可以放。

标签: ios swift animation swiftui


【解决方案1】:

尝试将动画链接到进度状态的值,例如

RoundedRectangle(cornerRadius: 12.0)
    .fill(getColorForBar(progress: progress))
    .frame(width: getFillWidth(progress: progress, geometry: geometry), height: 15)
    .animation(self.progressAnimation, value: progress)   // << here !!
     }, alignment: .leading)

【讨论】:

    猜你喜欢
    • 2022-07-05
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2013-03-17
    • 1970-01-01
    • 2020-03-16
    • 2020-07-12
    相关资源
    最近更新 更多