For WatchOS, I'm trying to build a ScrollView that auto scrolls to the last item added to a list. This all works fine, however once the ScrollView is full of content, the scrollTo
func becomes very sluggish and slow. Setting the animation duration for withAnimation
has no effect at this point. Any ideas why the animation begins to slow down? Thanks in advance.
struct ContentView: View {
@State private var items: [Date] = []
static var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm:ss:SSS"
return formatter
}()
var body: some View {
VStack {
ScrollView {
ScrollViewReader { scrollProxy in
LazyVStack {
ForEach(items, id: \.self) { item in
Text(Self.dateFormatter.string(from: item))
.font(.system(size: 16.0))
.padding()
.background(
RoundedRectangle(cornerRadius: 8.0)
.fill(Color.blue)
)
.id(item)
}
}
.onReceive(items.publisher) { date in
withAnimation(.easeInOut(duration: 0.25)) {
scrollProxy.scrollTo(date, anchor: .bottom)
}
}
}
}
Button("Add Item", action: { items.append(Date()) })
}
}
}