SwiftUI SwiftUI Drawing and Graphics 2 — Questions and Answers
Question 1: How do you apply a linear gradient as a fill in SwiftUI?
- .fill(LinearGradient(colors:startPoint:endPoint:))
- .gradient(.linear)
- .background(LinearGradient(...))
- Both A and C work (Correct answer)
Correct answer: Both A and C work
You can apply a LinearGradient via `.fill()` on a Shape or via `.background()` on any view.
Question 2: What is `TimelineView` used for in SwiftUI?
- Displaying a calendar timeline
- Updating a view on a schedule using a timeline schedule (Correct answer)
- Animating views along a path over time
- Logging view update times
Correct answer: Updating a view on a schedule using a timeline schedule
`TimelineView` updates its content on a given schedule (e.g., every second), enabling clock, animation, or live data displays.
Question 3: Which SwiftUI modifier applies a visual effect blur to a view?
- .shadow(radius:)
- .blur(radius:) (Correct answer)
- .opacity()
- .saturation()
Correct answer: .blur(radius:)
`.blur(radius:)` applies a Gaussian blur effect to the view's rendered output.
Question 4: What is the purpose of the `symbolVariants` environment value in SwiftUI?
- Sets SF Symbol animation style
- Automatically applies a variant (fill, slash, circle) to all SF Symbols in a view hierarchy (Correct answer)
- Controls symbol size
- Selects the symbol color palette
Correct answer: Automatically applies a variant (fill, slash, circle) to all SF Symbols in a view hierarchy
Setting `.symbolVariants()` in the environment propagates a visual variant like `.fill` or `.circle` to all SF Symbols in the hierarchy.
Question 5: How do you render an SF Symbol in SwiftUI?
- SFSymbol("star")
- Image(systemName: "star") (Correct answer)
- Symbol("star")
- Icon(sf: "star")
Correct answer: Image(systemName: "star")
`Image(systemName:)` loads and renders an SF Symbol by its system name.
Question 6: Which modifier controls the rendering mode of an SF Symbol image in SwiftUI?
- .imageScale()
- .symbolRenderingMode() (Correct answer)
- .renderingMode()
- .tint()
Correct answer: .symbolRenderingMode()
`.symbolRenderingMode()` sets how an SF Symbol is rendered: monochrome, hierarchical, palette, or multicolor.
How do you apply a linear gradient as a fill in SwiftUI?