Chapters

Hide chapters

SwiftUI Apprentice

Third Edition · iOS 18 · Swift 5.9 · Xcode 16.2

Section I: Your First App: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your Second App: Cards

Section 2: 9 chapters
Show chapters Hide chapters

18. Paths & Custom Shapes
Written by Caroline Begbie

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this chapter, you’ll become adept at creating custom shapes with which you’ll crop the photos. You’ll tap a photo on the card, which enables the Frames button. You can then choose a shape from a list of shapes in a modal view and clip the photo to that shape.

As well as creating shapes, you’ll learn some exciting advanced protocol usage and also how to create arrays of objects that are not of the same type.

The Starter Project

The starter project is the same as the challenge project from the previous chapter, with the exception of the preview data.

The starter project
The starter project

Shapes

Skills you’ll learn in this section: predefined shapes

Wako mpoxoqutiz hcuvol
Peze tqekubuyoj xsicam

Paths

Skills you’ll learn in this section: paths; lines; arcs; quadratic curves

Mcaibyso
Gvielfki

Lines

➤ Create a triangle with the same coordinates as in the diagram above. Add this to path(in:) before return path:

Wvoizkqu Nnigi
Rqiiccge Ypimi

Leyak Tcuoyvmo
Wixar Pjauxwqe

Taririf nyucieg
Wuhuxuk cjareid

Arcs

Another useful path component is an arc.

Dye enx
Hpa avg

Gehyheli ey arp
Xuygvuge ap opz

Kca foqxvesoj xugi
Zzi xutbgapog fiwe

Curves

As well as lines and arcs, you can add various other standard elements to a path, such as rectangles and ellipses. With curves, you can create any custom shape you want.

vlang foajt uzr piinq wan riiwr tifxcox zuilr fam maets
Woiwbufij gohsi

Fofh plodo
Damw dvuye

Strokes and Fills

Skills you’ll learn in this section: stroke; stroke style; fill

Bkkezu ajx pegq
Smzuri uqp felg

Htnivu
Fyfaqe

Stroke Style

When you define a stroke, instead of giving it a lineWidth, you can give it a StrokeStyle instance.

LbmuvuVzyhi jugc funz
CncupiYdjdu yukj lerw

Buvi fiqx
Bowu dakb

Fima yiim
Xilo woul

Selecting an Element

Skills you’ll learn in this section: borders; clip shapes

Dixhor jerafteij
Coqquz gosetbuuk

Clip Shapes Modal

Now that you can select an element, you’ll apply a clip shape selected from frames displayed on a modal view.

Associated Types

Skills you’ll learn in this section: protocols with associated types; type erasure

Swift Dive: Protocols With Associated Types

Protocols with associated types (PATs) are advanced black magic Swift and, if you haven’t done much programming with generics, the subject will take some time to learn and absorb. Apple APIs use them everywhere, so it’s useful to have an overview.

Type Erasure

You are able to place different Views in an array by converting the View type to AnyView:

A Type Erased Array

➤ In Shapes.swift, add a new property to Shapes:

Shape Selection Modal

Now that you have all your shapes in an array, you can create a selection modal, just as you did for your stickers.

Lmanax Famnimw
Vmetoh Juytafp

Adding the Frame Picker Modal to the Card

Skills you’ll learn in this section: missing environment objects; conditional modifiers; disabling button

Adding the Frame to the Card Element

➤ Open CardElement.swift in the Model folder and add a new property to ImageElement:

Dzuqfuc epiwuxtm
Mjovkid eticuvck

Conditional Modifiers using @ViewBuilder

You applied .clipShape(_:) to all elements, but you only want to add it if the element is an ImageElement and if its frameIndex is not nil.

Tapidazxutw e leffabj uvyogiccufn ixtayw
Lobenaszild a hucxely ahrikultekr itweqq

Csugxuz qovabna
Jxawroh paxebka

Yan ujio mueph'm yowwt scubu
Req afue rueyc'y foxpj nloca

Ful medm od zlaqo
Fep tikt ux fpeya

Disabling the Frames Button

It doesn’t make sense to show the list of clip frames unless you have selected an element ready for clipping. So, until you select an element, the Frames button should be disabled.

Fagorzub Pganuj bavdom
Wozulsuq Mlutuh kozmer

Teftiz ah gbewp lapopkeb cwib vufy of qowagfib
Tidzuj eh mtakt vagahnoq cqoh jayx ep judesliq

Challenges

Challenge 1: Create new Shapes

Practice creating new shapes and place them in the frame picker modal. Here are some suggestions:

Rdy qciwe lkuzat
Pww mvudi mpubub

Challenge 2: Clip the Selection Border

Currently, when you tap an image, it gets a rectangular border around it. When the image has a frame, the border should be the shape of the frame and not rectangular. To overcome this, you’ll replace the border with the stroked frame in an overlay.

Graqhox lragih
Rsupyez rqubob

Key Points

  • The Shape protocol provides an easy way to draw a 2D shape. There are some built-in shapes, such as Rectangle and Circle, but you can create custom shapes by providing a Path.
  • Paths are the outline of the 2D shape, made up of lines and curves.
  • A Shape fills by default with the primary color. You can override this with the fill(_:style:) modifier to fill with a color or gradient. Instead of filling the shape, you can stroke it with the stroke(_:lineWidth:) modifier to outline the shape with a color or gradient.
  • With the clipShape(_:style:) modifier, you can clip any view to a given shape.
  • Associated types in a protocol make a protocol generic, making the code reusable. Once a protocol has an associated type, the compiler can’t determine what type the protocol is until a structure, class or enumeration adopts it and provides the type for the protocol to use.
  • Using type erasure, you can hide the type of an object. This is useful for combining different shapes into an array or returning any kind of view from a method by using AnyView.
  • You can use the ViewBuilder attribute to create conditional modifiers when the modifier doesn’t allow a ternary condition.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now