如何解决SwiftUI listRowBackground 不能动画?
我试图模仿当你点击一个单元格时它的正常 UITableView 行为,然后淡出。我非常接近,除了:
-
.background()
不会填充整个列表单元格,它只是适合内容。 -
.listRowBackground()
确实填充了单元格,但没有动画。
有没有办法为列表行背景设置动画?
以下是完整上下文的来源。正如所写,因为 listRowBackground
没有动画,所以您根本看不到背景变化。如果您将其更改为 background
,它会按预期进行动画处理,但不会填充单元格。
struct Profile {
let name: String
var selected: Bool
var hilited: Bool = false
}
extension Profile: Identifiable {
var id: String { name }
}
struct ProfilesPicker: View {
@State var profiles: [Profile]
var body: some View {
List {
ForEach(0..<profiles.count) { index in
let profile = profiles[index]
CheckCell(name: profile.name,checked: profile.selected)
// using .background() gets a proper fade but doesn't fill the cell
.listRowBackground(Color(profile.hilited ? UIColor.systemFill : UIColor.systemBackground))
.onTapGesture {
profiles[index].hilited = true
withAnimation(.easeIn) {
profiles[index].hilited = false
profiles[index].selected.toggle()
}
}
}
}
}
}
struct CheckCell: View {
let name: String
let checked: Bool
var body: some View {
HStack {
Text(name)
Spacer()
if checked {
Image(systemName: "checkmark")
}
}
.contentShape(Rectangle())
}
}
解决方法
使用延迟并为 listRowBackground 添加动画。
struct ProfilesPicker: View {
@State var profiles: [Profile]
var body: some View {
List {
ForEach(0..<profiles.count) { index in
let profile = profiles[index]
CheckCell(name: profile.name,checked: profile.selected)
.onTapGesture {
profiles[index].hilited = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { //<==Here
profiles[index].hilited = false
profiles[index].selected.toggle()
}
}
.animation(.default)
.listRowBackground(Color(profile.hilited ? UIColor.systemFill : UIColor.systemBackground).animation(.easeInOut)) //<==Here
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。