我创建了一个SampleView,其中Button被包装在ZStack中,整个ZStack是在Button的轻击下设置动画的.如下所示:

enter image description here

struct SampleView: View {
    @State private var toggle: Bool = false
    var body: some View {
        ZStack {
            Button {
                toggle.toggle()
            } label: {
                Text("Tap here to move!")
                    .font(.system(size: 20, weight: .black))
                    .foregroundColor(.black)
            }
            .padding(10)
            .background(.red)
        }
        .offset(x: 0, y: toggle ? 0 : 200)
        .animation(.easeInOut(duration: 1), value: toggle)
    }
}

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        // ZStack {
            SampleView()
        // }
    }
}

我还有一堆其他的视图要呈现,所以我简单地将SampleView包装在VStack(或ZStack)中.Now, the animation started to break:

enter image description here

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        ZStack {
            SampleView()
        }
    }
}

我注意到,我可以通过用withAnimation包装按钮操作来解决这个问题.

withAnimation(.easeInOut(duration: 1)) {
    toggle.toggle()
}

不过,我想知道这到底是怎么回事.这是个窃听器吗?

推荐答案

这看起来当然像是一个漏洞.按钮的Text标签没有将.animation()值应用到它,但背景应用了.

这里有另一种修复它的方法.将.animation()修改器也应用于Text标签:

struct SampleView: View {
    @State private var toggle: Bool = false
    var body: some View {
        ZStack {
            Button {
                toggle.toggle()
            } label: {
                Text("Tap here to move!")
                    .font(.system(size: 20, weight: .black))
                    .foregroundColor(.black)
                    .animation(.easeInOut(duration: 1), value: toggle) // here
            }
            .padding(10)
            .background(.red)
        }
        .offset(x: 0, y: toggle ? 0 : 200)
        .animation(.easeInOut(duration: 1), value: toggle)
    }
}

More Evidence of the Bug

在这个例子中,我们有两个一起移动的按钮.这在iOS 15.5中可以正常运行,但在iOS 16中会中断.按下的按钮动画会中断,但对另一个按钮有效.

Showing bug in simulator

struct ContentView: View {
    @State private var toggle: Bool = false

    var body: some View {
        VStack {
            Button {
                toggle.toggle()
            } label: {
                Text("Tap here to move!")
                    .font(.system(size: 20, weight: .black))
                    .foregroundColor(.black)
            }
            .padding(10)
            .background(.red)

            Button {
                toggle.toggle()
            } label: {
                Text("Tap here to move!")
                    .font(.system(size: 20, weight: .black))
                    .foregroundColor(.black)
            }
            .padding(10)
            .background(.blue)

        }
        .offset(x: 0, y: toggle ? 0 : 200)
        .animation(.easeInOut(duration: 1), value: toggle)
    }
}

相同的修复方法也适用于此:将.animation()修饰符添加到每个按钮的Text标签中,或将按钮动作用withAnimation { }括起来.

Ios相关问答推荐

第三方框架的iOS隐私声明

UIImage大小调整工作,但转换为上传数据时恢复到原始大小

由于已存在同名项目,因此无法将Mapbox.xcframework-ios.sign复制到Signature中

swift|在具有自定义单元格的多个TableView中没有任何内容

如何在后台显示 Swift UI 中的通讯通知?

从包含 5 个以上项目的自定义选项卡栏的导航视图中删除更多按钮

AVAudioRecord 没有音频

多协议和类继承混乱,Swift

创建 Flutter 项目时,如何从我的系统中删除特定的开发者身份?

CGPath intersection(_:using:) 和朋友(iOS 16 中的新功能)坏了?

iOS - 判断开发者模式是否开启 (Swift)

CoreML 不能并发(多线程)工作?

从 PHAssets 生成图像时,iOS 应用程序因内存问题而崩溃

如何从 Locale Swift 获取货币符号

错误:无法根据成员environmentObject推断上下文基础

Swiftui为什么在点击第二行之前背景不起作用

SwiftUI 选项卡式视图防止滑动到下一个选项卡

从父母到子元素的 Swift Pass 事件

如何在 Swift 中使用包含多个值的查询参数构建一个 URL?

错误 itms-90035 - Xcode