Chapter 11 Exercises — Styling, Animation, and Accessibility
These exercises extend Chapter 11. Do the Independent Challenges on a separate Git branch so your guided project stays identical to the reference checkpoint:
First commit and tag the guided work using the workbook's Development Checkpoint. Confirm git status --short prints nothing, then create the branch:
git switch -c challenge-11
Commit your challenge work on that branch as you like. Before you start Chapter 12, return to the guided project:
Uncommitted edits can follow you across branches. Save them before switching; if nothing changed, skip add and commit. The status command should print nothing:
git add -A
git commit -m "Challenge 11: Save practice work"
git status --short
git switch main
Hints get progressively more specific. Try each challenge for a while before opening Hint 1, and try again after each hint. Full solutions are in the Chapter 11 Answer Key.
Challenge 1 — A penalty color for negative modifiers
Goal: make a negative modifier visually distinct in the result card.
Requirements:
- Add a new adaptive color set named
Penalty(a muted red) with an Any (light) and a Dark appearance. - Expose it in
ThemeasTheme.penalty. - In
ResultView, draw the "Dice 12, modifier -3" line inTheme.penaltywhen the modifier is negative, and in.secondaryotherwise (the line is not shown at all when the modifier is 0, as before).
Acceptance criteria:
- The color set has two different appearances, and the project uses the generated symbol, not a string name.
- Penalty-colored text has at least 4.5:1 contrast against
CardBackgroundin both modes (#FFFFFFlight,#292F36dark). Write down your hex values and the measured ratios. - The minus sign remains in the text, so the meaning does not depend on color alone.
- Positive modifiers still appear in the secondary color.
- The project builds with 0 errors and 0 warnings.
Hint 1
This challenge repeats Part A and Part B of the chapter with one new color. Which three places did each existing color touch: the asset catalog, Theme, and a view? Remember to build after creating the color set so the symbol exists.
Hint 2
For the light value, start from a deep, slightly brownish red so it sits well next to brass; for the dark value, use a lighter, softer red (pastel-ish) so it stands out on dark slate. Check each against its card color with a contrast tool or the Accessibility Inspector's color contrast calculator, and adjust lightness until both pass 4.5:1. The text line you need to change is inside if roll.modifier != 0 { ... } in ResultView.
Hint 3
The color choice is a ternary inside .foregroundStyle(...), with the condition roll.modifier < 0. Both branches must be the same type: Theme.penalty is a Color, so the other branch should be written as Color.secondary, not just .secondary. The new Theme line follows exactly the pattern of static let onBrass = Color(.onBrass).
Challenge 2 — Feedback when choosing a die
Goal: make choosing a die feel responsive, while respecting user preferences.
Requirements:
- Play a light selection haptic whenever the selected die changes.
- Use the same
hapticsEnabledpreference that controls the roll haptic. - Animate a die button's change between selected and unselected with a short animation, but use no animation when Reduce Motion is on.
Acceptance criteria:
- The selection haptic does not play at launch or when rolling. The trigger is
selectedDie, so check this by reading the code; to feel the haptic, see Optional Device Lesson D2. - Turning the Haptic feedback toggle off silences both the roll and the selection haptics.
- When
accessibilityReduceMotionistrue, the die buttons change instantly. - Rolls, history, and the roll haptic behave exactly as before.
- The project builds with 0 errors and 0 warnings.
Hint 1
You already have every ingredient in the chapter: a .sensoryFeedback modifier with a trigger and a condition closure in ContentView, and an implicit .animation(_:value:) that checks reduceMotion in ResultView. What value changes exactly when the user picks a different die, and is it something .sensoryFeedback can compare?
Hint 2
selectedDie is a DieType, and enums with raw values are automatically comparable with ==, so it works as a trigger. The feedback kind for "a choice changed" is .selection. For the animation, the value that changes inside DieButton is isSelected. DieButton will need its own @Environment(\.accessibilityReduceMotion) property, because environment values are read by the view that uses them.
Hint 3
Add a second .sensoryFeedback(...) right below the existing one in ContentView.body, with trigger: selectedDie and the same { _, _ in hapticsEnabled } closure. In DieButton, add the environment property below let action: () -> Void, then attach .animation(reduceMotion ? nil : .snappy, value: isSelected) to the styled label, directly after the .overlay(...) that draws the gold outline (inside the Button's label closure).
Challenge 3 — Audit and improve one element
Goal: practice finding and fixing a real accessibility or layout issue yourself.
Requirements: run the Accessibility Inspector's Audit on the running app and view ContentView with Dynamic Type Variants in the canvas. Pick one specific issue you or the tool found, fix it, and write two or three sentences: the problem, the change, and how you verified it.
Acceptance criteria: a concrete before/after description; a verification method you actually used; no regression in Light or Dark Mode; the project still builds without warnings.
Hint 1
Look at three kinds of things: what VoiceOver would say (labels and hints in the inspector), what happens at the largest accessibility text sizes (wrapping, truncation, overlapping), and contrast of any text you consider small.
Hint 2
Candidates worth examining: whether the history row label should include the time; whether the stepper labels read well ("Modifier: +2" may be spoken as "Modifier: plus 2"); whether the Haptic feedback toggle should have a hint; whether the header's studio name in .caption is readable at small sizes.
Hint 3
Make one small, testable change (for example, adding the time to HistoryRow's accessibility label using the same date.formatted(date: .omitted, time: .shortened) call the row already uses), then confirm it in the Accessibility Inspector by pointing at the element before and after.
Extra Practice
These have answers in the Answer Key. Try them without running the code first.
Exercise 11.1 — Predict: setting instead of adding
A teammate changes spin() to this:
private func spin() {
if reduceMotion {
return
}
withAnimation(.spring(duration: 0.6, bounce: 0.3)) {
rotation = 360
}
}
Reduce Motion is off. The user rolls three times. How many times does the circle visibly spin, and what is rotation after each roll?
Exercise 11.2 — Spot the bug: the app buzzes at launch
A tester reports that the app "vibrates every time I open it, before I touch anything". The relevant code is:
.sensoryFeedback(.impact(weight: .medium), trigger: history.count) { _, _ in
hapticsEnabled
}
.onAppear {
loadHistory()
}
Explain why it buzzes at launch, and what the trigger should be. Are there other situations where this version would buzz when it should not?
Exercise 11.3 — Spot the bug: AI-generated "realistic" roll
An AI assistant produced this to make rolls "feel more real":
private func spin() {
withAnimation(.spring(duration: 0.6, bounce: 0.3)) {
rotation += 360
} completion: {
latestRoll = RollResult(dieType: selectedDie,
values: [Int.random(in: 1...selectedDie.sides)],
modifier: modifier)
}
}
Ignore whether it compiles in ResultView (it would not, since ResultView cannot change latestRoll). List at least three design problems with deciding the result in an animation's completion.
Exercise 11.4 — Modify and predict: corner radius
You change Theme.cornerRadius from 16 to 8. Without running the app, list which elements on screen change shape and which do not. Then run it and check.
Exercise 11.5 — Predict: generated symbol names
For color sets named CardBackground, OnBrass, AccentColor, and HighlightColor, what generated symbol would you write inside Color(...)?