我有一个简单的代码,在网格中显示array.是否可以根据可用宽度和项目编号将项目显示为方舟状array.

import SwiftUI

struct ContentView: View {
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]    
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 15) {
                ForEach(0..<20) { number in
                    
                    Rectangle()
                        .foregroundColor(Color.red)
                        .frame(width: 50, height: 80)
                        .cornerRadius(10)
                        .shadow(color: .black, radius: 5, x: 2, y: 2)
                        .overlay(
                            Text("\(number)").foregroundColor(.white)
                        )
                }
            }.padding(.all, 10)
        }
    }
}

结果是这样的

Results

我想像下面的图片.

Desired

非常感谢.

推荐答案

这只是一个计算变换所需值的数学,通常是旋转加平移.

这是一个简化的原始演示(您可以更准确地或以不同方式计算Angular 和偏移-这不是演示的目标).使用Xcode 13.4/iOS 15.5测试(不依赖于平台)

demo

仅主要部分(因 for each 项目根据列中的位置进行转换):

ForEach(0..<20) { number in
    ItemView(value: number)  // << separate for simplicity
}

struct ItemView: View {
    var value: Int

    let baseAngl = CGFloat.pi/30
    var body: some View {
        //Button(action: {}) {  // << uncomment for demo
        Rectangle()
            .foregroundColor(Color.red)
            .frame(width: 50, height: 80)
            .cornerRadius(10)
            .shadow(color: .black, radius: 3, x: 1, y: 1)
            .overlay(
                Text("\(value)").foregroundColor(.white)
            )
            .rotationEffect(.radians(CGFloat(value % 5 - 2) * baseAngl), anchor: .bottom)
            .offset(y: UIScreen.main.bounds.height * (1 - cos(CGFloat(value % 5 - 2) * baseAngl)))
        //}
    }
}

Test module on GitHub

Swift相关问答推荐

如何更新Square Order?

SwiftData:线程1:&Quot;NSFetchRequest找不到实体名称';提醒&q;的NSEntityDescription

了解SWIFT中的任务行为

如何才能在同一线程上调用类中的每个方法,而不对每个调用使用同步块?

是否可以将具有关联值的枚举强制转换为其类型与关联值匹配的变量?

为什么这个快速代码不显示文本字段?

SwiftData:非法try 在不同上下文中的对象之间建立关系

如何使 onKeyPress 与 TextField 快速配合使用?

如何为我的项目设置生命周期选项?

找出touch 点的屏幕亮度

OSX 中的 Popover 无法确定透明度

RealityKit - 从中心zoom 模型

是否可以利用 Codable 从 Dictionary 初始化符合类型

If 语句在 Swift 中有 2 个日期

如何在 Swift 中使用子定义覆盖父类扩展

P384 公钥获取IncorrectParameterSize

Swift - 如何更新多目录中的对象

为什么 swift 中的类没有存储类型属性?

哪些版本的 Xcode 支持哪些版本的 Swift?

swift 中的@property/@synthesize 类似功能