Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Swift 2 Blueprints

You're reading from   Swift 2 Blueprints Swift Blueprints

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher Packt
ISBN-13 9781783980765
Length 276 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Costa Costa
Author Profile Icon Costa
Costa
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Swift 2 Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Exploring Xcode FREE CHAPTER 2. Creating a City Information App with Customized Table Views 3. Creating a Photo Sharing App 4. Simulating Home Automation with HomeKit 5. Health Analyzing App Using HealthKit 6. Creating a Game App Using SpriteKit 7. Creating an Apple Watch App 8. AVFoundation 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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images