diff --git a/content/notes/wwdc23/10252.md b/content/notes/wwdc23/10252.md index 5d3448f9..8dddcd78 100644 --- a/content/notes/wwdc23/10252.md +++ b/content/notes/wwdc23/10252.md @@ -23,7 +23,7 @@ A preview is a snippet of code that makes and configures a view. They are writte ```swift #Preview { - MyView() + MyView() } ``` @@ -60,7 +60,7 @@ Every preview has the same basic shape. // Anatomy of a preview initializer #Preview { - Content() + Content() } ``` @@ -69,7 +69,7 @@ Start with the Preview macro initializer at the top level of a source file. Then ```swift // Anatomy of a preview initializer #Preview("Name", configuration) { - Content() + Content() } ``` There are two main kinds of previews: views and widgets. Views can come from SwiftUI, UIKit, or AppKit. @@ -83,9 +83,9 @@ For SwiftUI, we can just return any view that we are working on. or we can place ```swift // SwiftUI #Preview { - List { - CollageView(layout: .twoByTwoGrid) - } + List { + CollageView(layout: .twoByTwoGrid) + } } ``` @@ -94,10 +94,10 @@ This is also the place to attach modifiers that provide data through the environ ```swift // SwiftUI #Preview { - List { - CollageView(layout: .twoByTwoGrid) - } - .environment(CollageLayoutStore.sample) + List { + CollageView(layout: .twoByTwoGrid) + } + .environment(CollageLayoutStore.sample) } ``` @@ -105,20 +105,20 @@ Previews are kind of like the scenes that we define at the top level of the app. ```swift #Preview(“2x2 Grid”, traits: .landscapeLeft) { - List { - CollageView(layout: .twoByTwoGrid) - } - .environment(CollageLayoutStore.sample) + List { + CollageView(layout: .twoByTwoGrid) + } + .environment(CollageLayoutStore.sample) } ``` For example, Instead of a SwiftUI view, just make a view controller, and configure it as needed. ```swift #Preview { - var controller = SavedCollagesController() - controller.dataSource = CollagesDataStore.sample - controller.layoutMode = .grid - return controller + var controller = SavedCollagesController() + controller.dataSource = CollagesDataStore.sample + controller.layoutMode = .grid + return controller } ``` @@ -126,10 +126,10 @@ Beyond view controllers, we can also preview a UIView or NSView directly. ```swift #Preview(“Filter View”) { - var view = CollageFilterDisplayView() - view.filter = .bloom(amount: 15.0) - view.imageData = … - return view + var view = CollageFilterDisplayView() + view.filter = .bloom(amount: 15.0) + view.imageData = … + return view } ``` @@ -185,27 +185,27 @@ We add a header for each Section, which is provided in a second trailing closure We’ll switch tab to the view controller that renders a filter using CoreImage, and switch the canvas back to live mode. In the preview macro, we created a view controller and passed it a sample image. ```swift #Preview { - let viewController = FilterRenderingViewController () - if let image = UIImage(named: "sample-001")?.cgImage { - viewController.imageData = image - } - return viewController + let viewController = FilterRenderingViewController () + if let image = UIImage(named: "sample-001")?.cgImage { + viewController.imageData = image + } + return viewController } ``` But now we add the code to pass a filter to the view controller. ```swift #Preview { - let viewController = FilterRenderingViewController () - if let image = UIImage(named: "sample-001")?.cgImage { - viewController.imageData = image - } - viewController.filter = Filter ( - bloomAmount: 1.0, - vignetteAmount: 1.0, - saturationAmount: 0.5 - } - - return viewController + let viewController = FilterRenderingViewController () + if let image = UIImage(named: "sample-001")?.cgImage { + viewController.imageData = image + } + viewController.filter = Filter ( + bloomAmount: 1.0, + vignetteAmount: 1.0, + saturationAmount: 0.5 + } + + return viewController } ``` @@ -238,15 +238,15 @@ This is where a timeline of specific entries comes in handy. We can craft the ex ```swift #Preview(as: systemSmall) { - FrameWidget() -} timeline: { - let first = CollageLayout - .preset_2x3_left3.map { _, _ in Color.gray } - .fillSlice(at: 6, with: [.green, .orange, .cyan]) - let second = first.fillslice(at: 1, with: [.blue]) - -ImageGridEntry(layout: first) -ImageGridEntry(layout: second) + FrameWidget() +} timeline: { + let first = CollageLayout + .preset_2x3_left3.map { _, _ in Color.gray } + .fillSlice(at: 6, with: [.green, .orange, .cyan]) + let second = first.fillslice(at: 1, with: [.blue]) + + ImageGridEntry(layout: first) + ImageGridEntry(layout: second) } ``` @@ -276,11 +276,11 @@ Another example. First, we pass in the attributes we want to use in the initiali // Previewing widgets // Live activities #Preview(as: .dynamicIsland(.compact), PizzaDeliveryAttributes()) { - FoodOrderWidget() + FoodOrderWidget() } contentStates: { - PizzaState.preparing - PizzaState.baking - PizzaState.outForDelivery + PizzaState.preparing + PizzaState.baking + PizzaState.outForDelivery } ```