我想用scrollTransition创建一些翻译效果.

这是一个使用scaleEffect的示例.我try 使用transformEffect来进行翻译,但该修饰符似乎不适用于scrollTransition.还有其他方法可以创建翻译效果吗?

struct ContentView: View {
    var colors: [Color] = [.blue, .green, .yellow, .orange, .red]
    var body: some View {
        ScrollView(.horizontal) {
            LazyHStack(spacing: 0) {
                ForEach(colors, id: \.self) { color in
                    ZStack {
                        Rectangle()
                            .foregroundStyle(color)
                        
                        Rectangle()
                            .foregroundStyle(.black)
                            .frame(width: 100, height: 100)
                            .scrollTransition { content, phase in
                                content
                                    .scaleEffect(phase.isIdentity ? 1 : 0.6)
                            }
                    }
                    .containerRelativeFrame(.horizontal, count: 1, spacing: 0)
                }
            }
            .scrollTargetLayout()
        }
        .scrollTargetBehavior(.viewAligned)
    }
}

推荐答案

使用.offset(x:y:).示例:

struct ContentView: View {
    var colors: [Color] = [.blue, .green, .yellow, .orange, .red]
    var body: some View {
        ScrollView(.horizontal) {
            LazyHStack(spacing: 0) {
                ForEach(colors, id: \.self) { color in
                    ZStack {
                        Rectangle()
                            .foregroundStyle(color)
                        
                        Rectangle()
                            .foregroundStyle(.black)
                            .frame(width: 100, height: 100)
                            .scrollTransition { content, phase in
                                content
                                    .offset(y: phase.isIdentity ? 0 : 100)
                                    .scaleEffect(phase.isIdentity ? 1 : 0.6)
                                    .opacity(phase.isIdentity ? 1.0 : 0.0)
                            }
                    }
                    .containerRelativeFrame(.horizontal, count: 1, spacing: 0)
                }
            }
            .scrollTargetLayout()
        }
        .scrollTargetBehavior(.viewAligned)
    }
}

Swift相关问答推荐

UITableView未显示类数组数据

使用SWIFT宏从另一个枚举添加 case

按Esc键时执行操作(工作表)

SwiftUI - NavigationLink(value:)和navigationDestination不工作

仅当单击UIButton时才调用计时器函数

当将新值推送到 NavigationStack 时,SwiftUI 的 navigationDestination 已在堆栈的早期声明

Swift 数组拆分成重叠的块

在这个使用 URLSession 的简单 case 中是否创建了一个强引用循环?

列表不显示信息

如何延迟 swift 属性 didSet 使其每秒只触发一次

deinitialize() 与 deallocate()

在 Vapor 4 中使用协议的通用流利查询

P384 公钥获取IncorrectParameterSize

将阻塞函数集成到 Swift 异步中

(SwiftLint)如果有正文,如何编写规则(可能是自定义),在{之后总是\n(换行)?

为什么 `map(foo)` 和 `map{ foo($0) }` 返回不同的结果?

如何在界面生成器中创建 UILayoutGuide?

为什么'nil' 与 Swift 3 中的 'UnsafePointer' 不兼容?

Swift中方法的弃用和其他属性,如何?

Objective-C 方法与可选需求方法 Swift 冲突