Book Image

SwiftUI Cookbook - Second Edition

By : Giordano Scalzo, Edgar Nzokwe
Book Image

SwiftUI Cookbook - Second Edition

By: Giordano Scalzo, Edgar Nzokwe

Overview of this book

SwiftUI provides an innovative and simple way to build beautiful user interfaces (UIs) for all Apple platforms, from iOS and macOS through to watchOS and tvOS, using the Swift programming language. In this recipe-based cookbook, you’ll cover the foundations of SwiftUI as well as the new SwiftUI 3 features introduced in iOS 15 and explore a range of essential techniques and concepts that will help you through the development process. The cookbook begins by explaining how to use basic SwiftUI components. Once you’ve learned the core concepts of UI development, such as Views, Controls, Lists, and ScrollViews, using practical implementations in Swift, you'll advance to adding useful features to SwiftUI using drawings, built-in shapes, animations, and transitions. You’ll understand how to integrate SwiftUI with exciting new components in the Apple development ecosystem, such as Combine for managing events and Core Data for managing app data. Finally, you’ll write iOS, macOS, and watchOS apps by sharing the same SwiftUI codebase. By the end of this SwiftUI book, you'll have discovered a range of simple, direct solutions to common problems encountered when building SwiftUI apps.
Table of Contents (17 chapters)

Chapter 7: Drawing with SwiftUI

One of the strongest points of SwiftUI is that all the components are uniform and that they can be used in an interchangeable and mixed way, whereas in UIKit, intermixing labels, buttons, and custom shapes was a bit cumbersome. In this chapter, we'll learn how to use the basic shapes offered out of the box by SwiftUI and how to create new shapes using the Path class. We'll learn how simple and natural it is to deal with, extend, and use custom shapes with standard components such as text and sliders.

By the end of the chapter, you'll be able to create a view from a custom path, add a gradient to fill a custom view, and will know how to write a Tic-Tac-Toe game using basic shapes.

In this chapter, we will cover the following recipes:

  • Using SwiftUI's built-in shapes
  • Drawing a custom shape
  • Drawing a curved custom shape
  • Drawing using the Canvas API
  • Implementing a progress ring
  • Implementing a Tic-Tac-Toe...