swift - 动画卡在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)
}
我应该在哪里应用过渡和动画?
解决方案
推荐阅读
- java - Asynchronous Programming and Reactive Programming
- arrays - How to optimize conditional statement in for loop over image?
- c# - 在委托操作中标签不显示
- excel - 复制一堆工作表并使用原始工作表的范围保存?
- palantir-foundry - 从 Slate 主页删除/隐藏“操作”按钮
- python - 为什么我不能使用 Discord.py 禁止/踢人?
- php - 从数组PHP中搜索数组的第一个数组中获取密钥
- laravel - Problem with laravel/ui in laravel after the last update
- javascript - 有没有办法在视觉上重新格式化这段代码?为什么会这样?
- ios - 如何在 SwiftUI 中访问“发送者”的手势?