You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中Scoped Animation内Offset modifier无法正常工作

SwiftUI Scoped Animation中.offset()动画不生效的问题

在iOS 17的Scoped Animation(作用域动画)场景下,给目标视图同时添加.opacity()和.offset()修饰符时出现异常:当状态animate改变时,仅.opacity()能正常执行过渡动画,.offset()完全无法触发动画效果——只有在使用withAnimation{}包裹状态变更,或者使用全局.animation()修饰符时,.offset()的动画才会生效。按Scoped Animation的设计逻辑,这两个修饰符的动画表现应该完全一致。

问题代码片段

Text("Hello, world!")
    .animation(.default) {
        $0
            .opacity(animate ? 1 : 0.2)
            .offset(y: animate ? 0 : 100) // <-- 无法生效
    }

完整测试代码

import SwiftUI

struct ContentView: View {
    
    @State private var animate = false
    
    var body: some View {
        VStack(spacing: 20) {
            Button("Toggle Scoped Animation") {
                animate.toggle()
            }
            Button("Toggle withAnimation{}") {
                withAnimation {
                    animate.toggle()
                }
            }
            Text("Hello, world!")
                .animation(.default) {
                    $0
                        .opacity(animate ? 1 : 0.2)
                        .offset(y: animate ? 0 : 100) // <-- 无法生效
                }
        }
    }
}

#Preview {
    ContentView()
}

@main
struct ScopedAnimationOffsetBugApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

测试环境

  • Xcode 15.3 (15E204a)
  • iOS 17.4 模拟器及iPhone真机
  • 已向苹果提交Bug报告:FB13693703

疑问

  • 该现象是否属于SwiftUI的已知Bug?
  • 是否有其他开发者遇到过相同问题?

内容的提问来源于stack exchange,提问作者clns

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 15:27:45