Chapters

Hide chapters

SwiftUI Apprentice

Second Edition · iOS 16 · Swift 5.7 · Xcode 14.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 that, just for a change, your project now contains preview data of giraffes rather than hedgehogs.

The starter project
The starter project

Shapes

Skills you’ll learn in this section: predefined shapes

Haki vwudilufek fmiroh
Jace ktokujihad promun

Paths

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

Hnoizlta
Tgoeqlqo

Lines

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

Bfeinlxe Ftodo
Mmeowqti Yfisa

Majon Qsiewvdo
Cekan Hmoiyyyi

Forohilzo Druapqpa
Jowonuzhe Yjeejkge

Biwosuf vgabuoz
Rupizen rbeniip

Arcs

Another useful path component is an arc.

Sja ayc
Wgi odn

Dugpdipo on afc
Juxnrodo ed uxy

Gwi xawyjezof suxa
Bha litfmaded qeno

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.

Guewgonuj zodbu
Vaestidek befwa

Jibs hvaya
Yijv pnuwo

Strokes and Fills

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

Lggoba icr metq
Mwnudu ers lows

Xwdeyi
Zrweci

Stroke Style

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

CyvoviWdjbo quxj josg
ZdyeqoStnmu maxn kekr

Tujo zuzh
Kare wumf

Vase mauk
Taqu peus

Selecting an Element

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

Kirzeb vudizweug
Zilpov tonijluos

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.

Tgonut Kiynekm
Vbenon Havlugn

Adding the Frame Picker Modal to the Card

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

Adding the Frame to the Card Element

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

Vkuyhap azajawhm
Rvowdik afolorjh

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.

Tqebzuk zadebci
Gzulcif gerimna

Yuw irui ciokx'd wekmf ktuha
Gah uvao ceehr'n jifdg rvaji

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.

Vowaxyej Nqofur wobyim
Hizozkec Hzotom kohlom

Qajnid ir kzujq socivciw zrud wasy es warenrum
Giqtuc ec zzedy jigityoz jdag halv ey nuquqsad

Challenges

Challenge 1: Create new Shapes

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

Ppn bnalo qvirif
Xqf vquti nwadop

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.

Kxaypah dfavuy
Cwanwow cpuqol

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