我在ZStack中有一副卡片组,我想一张张地向下移动纸牌.我正在努力实现这一点,当移动的卡片触底时,即将到来的卡片应该开始向下移动.我面临的问题是,整个ZStack正在向下移动,而不是一张卡.

import SwiftUI

struct Card: Identifiable {
    
    let id = UUID()
    let color: Color
    let name: String
}

struct CardView: View {
    
    let card: Card
        
    var body: some View {
        Rectangle()
            .fill(card.color)
            .frame(width: 120, height: 120)
            .cornerRadius(10)
            .overlay(
                Text(card.name)
                    .font(.headline)
                    .foregroundColor(.white)
            )
    }
}

struct ContentView: View {
    var cards: [Card] = [
        Card(color: Color.red, name: "First Card"),
        Card(color: Color.green, name: "Second Card"),
        Card(color: Color.orange, name: "Third Card"),
    ]
    
    @State var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
    @State var movingDownSpeed = CGPoint(x: 0, y:10)
    @State var cardPosition = CGPoint(x: UIScreen.main.bounds.size.width * 0.5, y:75)

 
    var body: some View {
        
        ZStack {
            ForEach(cards) { card in
               CardView(card: card)
                    .position(cardPosition)
                    .onReceive(self.timer) { _ in
                        moveDown()
                    }
            }
        }
        
    }
    
    func moveDown() {
        if cardPosition.y < UIScreen.main.bounds.height - 120 {
            withAnimation(.default){
                cardPosition.y += movingDownSpeed.y
            }
        }
    }
    
}

推荐答案

您可以try :

struct TestView: View {
    var cards: [Card] = [
        Card(color: Color.red, name: "First Card"),
        Card(color: Color.green, name: "Second Card"),
        Card(color: Color.orange, name: "Third Card"),
    ]
    
    @State var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    @State var movingDownSpeed = CGPoint(x: 0, y:120)
    @State var cardPosition = CGPoint(x: UIScreen.main.bounds.size.width * 0.5, y:75)
    
    @State var cardPositions = [CGPoint]()
    @State var count = 0

 
    var body: some View {
        ZStack {
            ForEach(self.cardPositions.indices, id: \.self) { index in
               CardView(card: cards[index])
                    .position(cardPositions[index])
            }
        }
        .onReceive(self.timer) { _ in
            moveDown()
        }
        .onAppear() {
            cardPositions = Array(repeating: CGPoint(x: UIScreen.main.bounds.size.width * 0.5, y:75), count:  cards.count)
            
        }
        
    }
    
    func moveDown() {
        if count < cardPositions.count {
            if cardPositions[count].y < UIScreen.main.bounds.height - 120 {
                withAnimation(.default){
                    cardPositions[count].y += movingDownSpeed.y
                }
            }
            count += 1
            if count == cardPositions.count {
                count = 0
            }
        }
    }
}

Swift相关问答推荐

如何在AudioKit中实现自定义音效 node ?

如何在应用程序区域永久更改 NSCursor?

使用`JSONSerialiser`时,省略通用可选项

根据其属性组合 2 个模型以在 Swift 中创建另一个模型

如何制作进度加载动画?

为什么在Swift中每次调用Decimal类型的formatted()方法都会越来越慢?

是否可以使用 .task 修饰符更新基于两个值的视图

deinitialize() 与 deallocate()

自定义 DispatchQueue 服务质量

`let case(value)` 和 `case(let value)` 之间的区别 (Swift)

Swift UI 不重绘 NSViewRepresentable 包装的文本字段

不要从 NumberFormatter 获取货币符号

Vapor Swift 如何配置客户端连接超时

如何使用 Date.ParseStrategy 在 Swift 5.6 中将字符串解析为日期?

Swiftwhere数组扩展

如何为多个类 Swift 进行扩展

在 Swift 框架中加载资源(例如故事板)

如何交换快速数组中的元素?

如何快速格式化用户显示(社交网络等)的时间间隔?

Swift3 中的 NSIndexPath 初始化有何变化?