DisableRendering.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import SwiftUI
  2. import Spine
  3. struct DisableRendering: View {
  4. @StateObject
  5. var controller = SpineController(
  6. onInitialized: { controller in
  7. controller.animationState.setAnimationByName(
  8. trackIndex: 0,
  9. animationName: "walk",
  10. loop: true
  11. )
  12. }
  13. )
  14. @State
  15. var isRendering: Bool?
  16. var body: some View {
  17. VStack {
  18. List {
  19. VStack(alignment: .leading) {
  20. Text("Scroll spine boy out of the viewport")
  21. Text("Rendering is disabled when the spine view moves out of the viewport, preserving CPU/GPU resources.")
  22. .foregroundColor(.secondary)
  23. }
  24. SpineView(
  25. from: .bundle(atlasFileName: "spineboy-pma.atlas", skeletonFileName: "spineboy-pro.skel"),
  26. controller: controller,
  27. isRendering: $isRendering
  28. )
  29. .frame(minHeight: 200)
  30. .onAppear {
  31. isRendering = true
  32. print("rendering enabled")
  33. }
  34. .onDisappear {
  35. isRendering = false
  36. print("rendering disabled")
  37. }
  38. Text("Foo")
  39. .frame(minHeight: 400)
  40. Text("Bar")
  41. .frame(minHeight: 400)
  42. Text("Baz")
  43. .frame(minHeight: 400)
  44. }
  45. }
  46. .navigationTitle("Disable Rendering")
  47. .navigationBarTitleDisplayMode(.inline)
  48. }
  49. }
  50. #Preview {
  51. DisableRendering()
  52. }