首页 > 解决方案 > 动画卡在swiftUI中进入网格

问题描述

我有一个在 SwiftUI 中构建的自定义网格,模型中有 12 张卡片。当 iPhone 启动时,网格会将这 12 张卡片加载到屏幕上。但是,我希望卡片从随机位置滑入它们各自的位置。我是 SwiftUI 新手,无法理解动画和过渡。如果我必须使用@State,那么如何将其同步到模型中的某个变量。这是代码,这是主视图。为了更容易集中,我省略了 var body:

Grid(self.setGameViewModel.playingTwelveSetCards) { setCard in
    SetCardView(setCard: setCard)
        .onTapGesture {
            withAnimation(.easeInOut(duration: 0.3)) {
                self.setGameViewModel.choose(setCard: setCard)
            }
         }
}

这是卡片视图:

ZStack {
    RoundedRectangle(cornerRadius: cornerRadius)
        .stroke(isSelected ? Color.black : Color.gray, lineWidth: lineWidth)
    VStack {
        ForEach(0..<count) { _ in
            self.getShapes(color: color)
        }
    }
}
.padding(5)
.scaleEffect(isSelected ? 1.07 : 1)
                        

self.setGameViewModel.playingTwelveSetCards包含从 viewModel 中提取的 12 张集合卡片,一旦应用程序启动playingTwelveSetCards就会填充 12 张集合卡片并且 Grid 刚刚被绘制,但我不希望卡片只是出现我希望它们从随机位置滑动,因为我有这个小辅助函数

func generateRandomNumberForOffset() -> CGFloat {
    CGFloat(arc4random_uniform(201) + 100)
}

我应该在哪里应用过渡和动画?

标签: swiftanimationswiftuitransition

解决方案


推荐阅读