Is there any way to remove the fading animation on .onDelete SwiftUI
A possible solution is to force redraw the List
:
struct ContentView: View {
@State private var names = ["david", "john", "amber"]
@State private var listId = UUID()
var body: some View {
List {
ForEach(names, id: \.self) { item in
Text(item)
}
.onDelete(perform: deleteItem)
}
.id(listId)
}
private func deleteItem(at indexSet: IndexSet) {
names.remove(atOffsets: indexSet)
listId = UUID()
}
}
RxSwift, remove cell with animation
Sorry for the delay. Here is the example for animatable tableview in rxswift.
enum MySectionItem {
case a(presentable: APresentable)
case b(presentable: BPresentable)
case c(presentable: CPresentable)
}
extension MySectionItem: IdentifiableType, Equatable {
typealias Identity = String
var identity: Identity {
switch self {
case let .a(presentable):
return presentable.title //Something which can define uniqueness
case let .b(presentable):
return presentable.title
case let .c(presentable):
return presentable.title
}
}
}
For SectionModel also you can do the same.
enum MySectionModel {
case a(title: String, items: [MySectionItem])
case b(title: String, items: [MySectionItem])
case c(title: String, items: [MySectionItem])
}
extension MySectionModel: AnimatableSectionModelType {
typealias Item = MySectionItem
var items: [Item] {
switch self {
case let .a(title: _, items: items),
let .b(title: _, items: items),
let .c(title: _, items: items):
return items.map { $0 }
}
}
var identity: String {
switch self {
case let .a(title: title, items: _),
let .b(title: title, items: _),
let .c(title: title, items: _):
return title
}
}
init(original: MySectionModel, items: [Item]) {
switch original {
case let .a(title: title, items: _):
self = .a(title: title, items: items)
case let .b(title: title, items: _):
self = .b(title: title, items: items)
case let .c(title: title, items: _):
self = .c(title: title, items: items)
}
}
}
How can I animate a SwiftUI view based on the change of a TextEditor's text?
Use animation on container, like
HStack {
Spacer()
if !newEntryString.isEmpty{
Button(action: {
addEntry()
}) {
Text("Add Entry")
}.buttonStyle(JournalButtonStyle())
}
}.animation(Animation.default.speed(1)) // << here !!
SwiftUI List animation when changing data source
If your button is wrapped as you suggested and I added a simple direction boolean:
Button(action: {
withAnimation {
slideRight = true
self.previous()
}
}) {
Text("<")
}
And opposite for the other direction:
Button(action: {
withAnimation {
slideRight = false
self.next()
}
}) {
Text(">")
}
Then you can transition your view like this:
List(viewingModels, id: \.self) { model in
Text(model)
}
.id(UUID())
.transition(.asymmetric(insertion: .move(edge: slideRight ? .leading : .trailing),
removal: .move(edge: slideRight ? .trailing : .leading)))
Note that in order for the list to not animate, we need to give the list a new unique ID each time, see this article: https://swiftui-lab.com/swiftui-id/
UPDATE:
I wanted to provide the full shortened code that works, also removed the UUID() usage based on comments below.
import SwiftUI
struct ContentView: View {
private let models = [
["a", "b", "c", "d", "e", "f"],
["g", "h"],
["i", "j", "k", "l"],
]
@State private var selectedCategory = 0
@State private var slideRight = true
private var viewingModels: [String] {
models[selectedCategory]
}
var body: some View {
VStack(spacing: 0.0) {
HStack {
Button(action: {
withAnimation {
if(self.selectedCategory - 1 < 0) { self.selectedCategory = self.models.count - 1 }
else { self.selectedCategory -= 1 }
self.slideRight = true
}
}) {
Image(systemName: "arrow.left")
}
Text("\(selectedCategory + 1)")
Button(action: {
withAnimation {
if(self.selectedCategory + 1 > self.models.count - 1) { self.selectedCategory = 0 }
else { self.selectedCategory += 1 }
self.slideRight = false
}
}) {
Image(systemName: "arrow.right")
}
}.font(.title)
List(viewingModels, id: \.self) { model in
Text(model)
}
.id(selectedCategory)
.transition(.asymmetric(insertion: .move(edge: slideRight ? .leading : .trailing),
removal: .move(edge: slideRight ? .trailing : .leading)))
}.padding(10)
}
}
Related Topics
Encode Struct and Convert to Dictionary [String: Any]
Realitykit - Updating Entity's Translation Returns Unexpected Values
How to Set Local Notifications Between 8Am and 8Pm Every Day
Mapping a Dictionary to an Array of Structs in Swift
How to Get The Advantages of Scenekit's Level Editor Programatically
Swift-Animate Cashapelayer Stroke Color
Nsdocumentcontroller.Opendocument Not Allowing Selection of Custom File Type
Multiple Enum Implementing Protocols Questions
Swift: Draw a Semi-Sphere in Mkmapview
Streaming .M3U8 Using Mpmovieplayercontroller Does Not Work
Firebase Remove Snapshot Children Swift
Trying to Display Location Data from Firebase to Mapkit
Skphysicscontact Not Detecting Categorybitmask Collision
Need Clarification on Anyobject in Swift
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property