Book Image

SwiftUI Cookbook

By : Giordano Scalzo, Edgar Nzokwe
Book Image

SwiftUI Cookbook

By: Giordano Scalzo, Edgar Nzokwe

Overview of this book

SwiftUI is an innovative and simple way to build beautiful user interfaces (UIs) for all Apple platforms, right from iOS and macOS through to watchOS and tvOS, using the Swift programming language. In this recipe-based book, you’ll work with SwiftUI and explore a range of essential techniques and concepts that will help you through the development process. The recipes cover the foundations of SwiftUI as well as the new SwiftUI 2.0 features introduced in iOS 14. Other recipes will help you to make some of the new SwiftUI 2.0 components backward-compatible with iOS 13, such as the Map View or the Sign in with Apple View. The cookbook begins by explaining how to use basic SwiftUI components. Then, you’ll learn the core concepts of UI development such as Views, Controls, Lists, and ScrollViews using practical implementation in Swift. By learning drawings, built-in shapes, and adding animations and transitions, you’ll discover how to add useful features to the SwiftUI. When you’re ready, 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 while 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 found in building SwiftUI apps.
Table of Contents (15 chapters)

Chapter 6: Drawing with SwiftUI

One of the strongest points of SwiftUI is that all the components are uniform, and 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 see 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 see how simple and natural it is to deal with, extend, and use them with standard components such as text and sliders.

At the end of the chapter, you'll be able to create a view from a custom path, add a gradient to fill it or to act as a border, 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
  • Creating a dashed border in SwiftUI
  • Drawing a custom shape
  • Drawing a curved custom shape
  • Using UIBezierPath with SwiftUI
  • Implementing a progress...