animation - 如何在 ZStack 中捕获自定义卡片视图上的点击?
问题描述
我正在努力重新创建类似 Wallet 视图的东西,但无法真正弄清楚。我已经将偏移硬编码到我的 ZStack 中,但我当然也想避免这种情况。似乎偏移量使抽头根本无法注册。到目前为止,这是我的代码,点击卡片应将其标题的颜色更改为白色和黄色。
最后,当然,我想将动画添加到卡片堆栈中,并能够从其中“拉”一个以使其成为主要视图。但我必须先弄清楚如何抓住水龙头......
我以这个视频为基础,但它并没有达到我想要的程度:
https://nsscreencast.com/episodes/399-swiftui-transforms-and-animations
import SwiftUI
struct BetterCards: View {
var body: some View {
ScrollView (.vertical) {
ZStack {
CardView(title: "adidas", color: .blue, offset: 0.0)
CardView(title: "Le Crueset", color: .pink, offset: 90.0)
CardView(title: " Card", color: .black, offset: 180.0)
CardView(title: "Sandusk", color: .orange, offset: 270.0)
}
}
}
}
struct CardView: View {
@State var isYellow: Bool = false
let title: String
let color: Color
let offset: CGFloat
var body: some View {
Button(action: {
self.isYellow.toggle()
}) {
ZStack {
Rectangle()
.fill(color)
.cornerRadius(10)
.frame(width: 320, height: 210)
VStack {
Text(title)
.font(.largeTitle)
.bold()
.foregroundColor(isYellow ? .yellow : .white)
.padding()
Spacer()
}
}.shadow(radius: 2.0)
.offset(x: 0, y: offset)
}.onTapGesture {
print("Tapped: \(self.title)")
}
}
}
struct BetterCards_Previews: PreviewProvider {
static var previews: some View {
BetterCards()
}
}
解决方案
struct ContentView: View {
var body: some View {
ScrollView (.vertical) {
ZStack(alignment: .top) {
CardView(title: "adidas", color: .blue, paddingOffset: 0)
CardView(title: "Le Crueset", color: .pink, paddingOffset: 90)
CardView(title: " Card", color: .black, paddingOffset: 180)
CardView(title: "Sandusk", color: .orange, paddingOffset: 270)
}
}
}
}
struct CardView: View {
@State var isYellow: Bool = false
let title: String
let color: Color
var paddingOffset: CGFloat
var body: some View {
ZStack {
Rectangle()
.fill(color)
VStack {
Text(title)
.font(.largeTitle)
.bold()
.foregroundColor(isYellow ? .yellow : .white)
.padding()
Spacer()
}
}.shadow(radius: 2.0)
.cornerRadius(10)
.frame(width: 320, height: 210)
.padding(.top, paddingOffset)
.onTapGesture {
self.isYellow.toggle()
print("Tapped: \(self.title)")
}
}
}
推荐阅读
- python - 计算滞后并按列分组
- ios - Target 具有传递依赖项,包括 podspec 中的静态链接二进制文件
- php - 找不到课程表
- css - Angular Material Design 卡:如何修复对齐?
- mysql - 使用 group by 和减法 SQL 进行聚合
- sql-function - 如何从包含递归公用表表达式的 SQL 函数返回?
- rasa - 尽管正确识别了意图,但 RASA 响应始终为空
- linux - awk:根据列组中的最大值过滤文件
- python - FFmpegAudio 和 discord.py,加速音轨
- vue.js - 将 route.params.id 传递给 vuex 操作