Расширение строк списка SwiftUI не анимируется плавно; предметы ниже резко подпрыгиваютIOS

Программируем под IOS
Ответить
Anonymous
 Расширение строк списка SwiftUI не анимируется плавно; предметы ниже резко подпрыгивают

Сообщение Anonymous »

Я пытаюсь анимировать расширение строки внутри списка, когда пользователь нажимает на значок заметки. Заворачиваю изменение состояния в withAnimation, но заметка появляется с плохой анимацией и строки ниже без анимации продавливаются вниз (резко "подпрыгивают"). Как правильно анимировать расширение, включая сдвиг макета строк ниже?
Вот минимальный рабочий пример, воспроизводящий такое поведение (скопируйте в новый проект SwiftUI и запустите):
import SwiftUI

@main
struct ExpansionApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

struct HistoryItem: Identifiable, Hashable {
let id: UUID
let title: String
let note: String?
}

// MARK: - View

struct ContentView: View {
// Keeps the sample aligned with the real screen behavior for note expansion.
@State private var expandedNoteItems: Set = []

private let items: [HistoryItem] = [
HistoryItem(id: UUID(), title: "Client A - Morning shift", note: "Short note for entry 1."),
HistoryItem(id: UUID(), title: "Client B - Field visit", note: "Longer note for entry 2 so the row expands more than the others."),
HistoryItem(id: UUID(), title: "Client C - Training", note: nil),
HistoryItem(id: UUID(), title: "Client D - Support", note: "Another note for entry 4."),
HistoryItem(id: UUID(), title: "Client E - Wrap up", note: "Final note for entry 5.")
]

var body: some View {
NavigationStack {
List {
ForEach(items, id: \.id) { item in
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 12) {
Text(item.title)
.font(.headline)

Spacer()

if item.note != nil {
Button {
toggleNote(for: item.id)
} label: {
Image(systemName: "note.text")
.imageScale(.medium)
}
.buttonStyle(.plain)
}
}

if let note = item.note, expandedNoteItems.contains(item.id) {
Text(note)
.font(.subheadline)
.foregroundStyle(.secondary)
.padding(10)
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
}
}
.padding(.vertical, 8)
}
}
.listStyle(.plain)
.navigationTitle("History")
}
}

private func toggleNote(for id: UUID) {
withAnimation {
if expandedNoteItems.contains(id) {
expandedNoteItems.remove(id)
} else {
expandedNoteItems.insert(id)
}
}
}
}

// MARK: - Preview

#Preview {
ContentView()
}


Подробнее здесь: https://stackoverflow.com/questions/798 ... p-abruptly
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»