Book Image

Swift 2 Blueprints

By : Cecil Costa
Book Image

Swift 2 Blueprints

By: Cecil Costa

Overview of this book

In this book, you will work through seven different projects to get you hands-on with developing amazing applications for iOS devices. We start off with a project that teaches you how to build a utility app using Swift. Moving on, we cover the concepts behind developing an entertainment or social networking related application, for example, a small application that helps you to share images, audio, and video files from one device to another. You’ll also be guided through create a city information app with customized table views, a reminder app for the Apple Watch, and a game app using SpriteKit. By the end of this book, you will have the required skillset to develop various types of iOS applications with Swift that can run on different iOS devices. You will also be well versed with complex techniques that can be used to enhance the performance of your applications.
Table of Contents (15 chapters)
Swift 2 Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a view to draw on it


Before we start with the next view controller, we are going to create a custom view to draw the current picture's state and allow the user to add some features to it.

Create a new file called BoardView.swift and start a new class with the same name. This class must inherit from UIView (not UIViewController), therefore it needs to import UIKit:

import UIKit

class BoardView:UIView {

How is it going to work? It's very easy. We have to store the elements that should be drawn. Every time we store a new element, we will have to call setNeedsDisplay to report that there is something new. drawRect should be called as well. These elements are going to be implemented afterward; the only detail we need to know about them right now is that they inherit from a class that will be called Element, which will have a method called draw. Place the following code to add the elements to the array:

    private var elements = [Element]()

    func addElement(element:BoardView.Element...