Overview
This chapter draws on everything you learned how to do (and more) in the previous three, and leads you through the process of building a vertical (and later horizontal) image and text carousel.
As you dive in, there are a few things to take note of:
Key Takeaways
ScrollViewwraps any content to make it scrollable — useScrollView(.horizontal)for a side-scrolling carousel.- A
CardViewis a reusable struct defined in its own file; every place you call it — includingPreviewProvider— must supply values for any variables it declares. - “Refactoring” in this chapter means extracting hardcoded content into variables so the same
CardViewstruct can be reused with different data — a pattern you’ll use repeatedly. - The
PreviewProviderstruct calls your view struct to render a canvas preview; when the struct has parameters, the preview must pass matching values. - Try to complete the full tutorial before downloading the finished file — the homework assignment is structurally similar and there is no download available for it.
Read Chapter 5: Understanding ScrollView and Building a Carousel UI in Mastering SwiftUI