ios application development - rwth aachen university€¦ · simon völker & philipp wacker ....

42
Simon Völker & Philipp Wacker Media Computing Group RWTH Aachen University hci.rwth-aachen.de/ios Lecture 5: Swift Protocols and Extensions iOS Application Development

Upload: others

Post on 20-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Simon Völker & Philipp Wacker Media Computing Group RWTH Aachen University

hci.rwth-aachen.de/ios

Lecture 5: Swift Protocols and Extensions

iOS Application Development

Page 2: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Simon Voelker, Philipp Wacker: iOS Application Development2

SpriteKit

Page 3: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

What is Sprite Kit?

Simon Voelker, Philipp Wacker: iOS Application Development3

Page 4: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Simon Voelker, Philipp Wacker: iOS Application Development4

GPU

Scene Graphs

SpriteKitSceneKit

2D Graphics and Imaging

Core AnimationCore ImageCore Graphics

Standards-Based 3D Graphics

Open GL ES

High EfficiencyGPU Access

Metal

Your App

Page 5: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Sprite Kit

Simon Voelker, Philipp Wacker: iOS Application Development5

PhysicsActionsObjects

Page 6: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Root Object: SKScene

Simon Voelker, Philipp Wacker: iOS Application Development6

ApplicationSKView

ViewController//Setup SKView//Create Scene

SKScene/* Your Code here */

Page 7: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Scene Graph

Simon Voelker, Philipp Wacker: iOS Application Development7

background

crater

crater

crater

spaceship

projectileasteroid

asteroid

asteroid

asteroid

propellant

SKScene

crater

NodesActions Physics

Page 8: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Sprite Kit Nodes

Simon Voelker, Philipp Wacker: iOS Application Development8

SKNode SKSpriteNode

SKShapeNode

SKLabelNode

SKEmitterNode

SKEffectNode

SKCropNode

SKScene

SKVideoNode

Page 9: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Simon Voelker, Philipp Wacker: iOS Application Development9

Sprite Kit Demo

Page 10: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

10

Swift Protocols and Extensions

Simon Voelker, Philipp Wacker: iOS Application Development

Page 11: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Protocols• A protocols defines a blueprint of methods, properties and other requirements

• Protocols can be adopted by another type that implements these requirements

• Swift utilizes many protocols such as:

• CustomStringConvertible

• Equatable

• Comparable

11 Simon Voelker, Philipp Wacker: iOS Application Development

Page 12: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

CustomStringConvertible

12 Simon Voelker, Philipp Wacker: iOS Application Development

let string = "Hello, world!"print(string) // Output: Hello, world!

let number = 42print(number) // Output: 42

let boolean = falseprint(boolean) // Output: false

Page 13: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

CustomStringConvertible

13 Simon Voelker, Philipp Wacker: iOS Application Development

class Shoe { let color: String let size: Int let hasLaces: Bool init(color: String, size: Int, hasLaces: Bool) { self.color = color self.size = size self.hasLaces = hasLaces }}

let myShoe = Shoe(color: "Black", size: 12, hasLaces: true)let yourShoe = Shoe(color: "Red", size: 8, hasLaces: false)

print(myShoe) // Output: Shoeprint(yourShoe) // Output: Shoe

Page 14: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

CustomStringConvertible

14 Simon Voelker, Philipp Wacker: iOS Application Development

class Shoe : CustomStringConvertible { let color: String let size: Int let hasLaces: Bool init(color: String, size: Int, hasLaces: Bool) { self.color = color self.size = size self.hasLaces = hasLaces } var description: String { return "Shoe(color: \(color), size: \(size), hasLaces: \(hasLaces))" }}

let myShoe = Shoe(color: "Black", size: 12, hasLaces: true)let yourShoe = Shoe(color: "Red", size: 8, hasLaces: false)

print(myShoe) // Output: Shoe(color: Black, size: 12, hasLaces: true)print(yourShoe) // Output: Shoe(color: Red, size: 8, hasLaces: false)

Page 15: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Equatable

15 Simon Voelker, Philipp Wacker: iOS Application Development

struct Employee { var firstName: String var lastName: String var jobTitle: String var phoneNumber: String}

let employeeA = Employee(...)

let employeeB = Employee(...)

if employeeB == employeeA { // Do Something} else { // Do Something else }

Page 16: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Equatable

16 Simon Voelker, Philipp Wacker: iOS Application Development

struct Employee : Equatable { var firstName: String var lastName: String var jobTitle: String var phoneNumber: String

static func ==(lhs: Employee, rhs: Employee) -> Bool { return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName && lhs.companyID == rhs.companyID }}

let employeeA = Employee(...)

let employeeB = Employee(...)

if employeeB == employeeA { // Do Something} else { // Do Something else }

Page 17: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Comparable

17 Simon Voelker, Philipp Wacker: iOS Application Development

struct Employee { var firstName: String var lastName: String var jobTitle: String var phoneNumber: String

static func ==(lhs: Employee, rhs: Employee) -> Bool { return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName && lhs.companyID == rhs.companyID }}

let employees = [employee1, employee2, employee3, employee4, employee5]

let sortedEmployees = employees.sorted(by: <)

Page 18: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Comparable

18 Simon Voelker, Philipp Wacker: iOS Application Development

struct Employee : Comparable { var firstName: String var lastName: String var jobTitle: String var phoneNumber: String

static func ==(lhs: Employee, rhs: Employee) -> Bool { return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName && lhs.companyID == rhs.companyID }

static func < (lhs: Employee, rhs: Employee) -> Bool { return lhs.lastName < rhs.lastName

}

let employees = [employee1, employee2, employee3, employee4, employee5]

let sortedEmployees = employees.sorted(by: <)

Page 19: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Creating a Protocol

19 Simon Voelker, Philipp Wacker: iOS Application Development

protocol FullyNamed { var fullName: String { get } func sayFullName()}

Page 20: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Creating a Protocol

20 Simon Voelker, Philipp Wacker: iOS Application Development

protocol FullyNamed { var fullName: String { get } func sayFullName()}

struct Person: FullyNamed { var firstName: String var lastName: String   var fullName: String { return "\(firstName) \(lastName)" }   func sayFullName() { print(fullName) }}

Page 21: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

• Functions or parameters with optional modifier don’t have to be implemented

• @objc Protocols can only be adapted by Objective-C classes or subclasses of E. g. UIKit classes

21

Optional Protocol Requirements

@objc protocol CounterDataSource { @objc optional func increment(forCount count: Int) -> Int @objc optional var fixedIncrement: Int { get } }

Simon Voelker, Philipp Wacker: iOS Application Development

Page 22: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Optional Protocol Requirements

22 Simon Voelker, Philipp Wacker: iOS Application Development

public protocol SKPhysicsContactDelegate : NSObjectProtocol {

optional public func didBegin(_ contact: SKPhysicsContact)

optional public func didEnd(_ contact: SKPhysicsContact)}

• The SKPhysicsContactDelegate from SpriteKit

Page 23: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

23

Delegation

Simon Voelker, Philipp Wacker: iOS Application Development

Page 24: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Delegation• Delegation is a design pattern to hand of responsibilities to an instance of

another type

• It is extensively used in UIKit and other iOS Frameworks

24 Simon Voelker, Philipp Wacker: iOS Application Development

class GameScene: SKScene, SKPhysicsContactDelegate { … self.physicsWorld.contactDelegate = self

}

• SpriteKit example:

class MyViewController: UITableViewDataSource, UITableViewDelegate { … self.myTableView.dataSource = self self.myTableView.delegate = self }

• TableView example:

Page 25: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

25

Extensions

Simon Voelker, Philipp Wacker: iOS Application Development

Page 26: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Extensions• Extensions add functionality to types

that are already defined.

• You can add:

• computed properties

• define methods

• provide new initializers

• conform an existing type to a protocol

• You cannot add properties!

26 Simon Voelker, Philipp Wacker: iOS Application Development

extension SomeType { // new functionality to add to // SomeType goes here}

extension SomeType { // new functionality to add to // SomeType goes here}

extension UIColor { static var favoriteColor: UIColor {

return UIColor(red: 0.5, green: 0.1, blue: 0.5, alpha: 1.0)

}}

Page 27: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Extensions

27 Simon Voelker, Philipp Wacker: iOS Application Development

struct Employee { var firstName: String var lastName: String var jobTitle: String var phoneNumber: String

static func ==(lhs: Employee, rhs: Employee) -> Bool { return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName && lhs.companyID == rhs.companyID }}

struct Employee { var firstName: String var lastName: String var jobTitle: String var phoneNumber: String}

extension Employee : Equatable { static func ==(lhs: Employee, rhs: Employee) -> Bool { return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName && lhs.companyID == rhs.companyID }}

Page 28: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

28

CHAPTER 4

UIView Controllers

Simon Voelker, Philipp Wacker: iOS Application Development

Page 29: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Tab Bar Controller

29 Simon Voelker, Philipp Wacker: iOS Application Development

Page 30: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

View Controller Life-Cycle• View Controllers can be in one of these states:

• View not loaded

• View appearing

• View appeared

• View disappearing

• View disappeared

30 Simon Voelker, Philipp Wacker: iOS Application Development

Not Loaded

Appear viewDidAppear(_:)

Disappeared viewDidDisappear(_:)

viewDidLoad()

viewWillAppear(_:)

viewWillDisappear(_:)

Page 31: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Application Life-Cycle

31 Simon Voelker, Philipp Wacker: iOS Application Development

AppDelegate.swift

SceneDelegate.swift

Page 32: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

AppDelegate.swift

32 Simon Voelker, Philipp Wacker: iOS Application Development

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { return true }

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { }

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { }

Page 33: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Scene Life-Cycle

33 Simon Voelker, Philipp Wacker: iOS Application Development

Source: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle

Page 34: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

SceneDelegate.swift

34 Simon Voelker, Philipp Wacker: iOS Application Development

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { }

func sceneDidDisconnect(_ scene: UIScene) { }

func sceneDidBecomeActive(_ scene: UIScene) { }

func sceneWillResignActive(_ scene: UIScene) { }

func sceneWillEnterForeground(_ scene: UIScene) { }

func sceneDidEnterBackground(_ scene: UIScene) { }

Page 35: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

35

CHAPTER 4

Scroll Views

Simon Voelker, Philipp Wacker: iOS Application Development

Page 36: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

ScrollView• UIScrollView

• Parent of UITableView

• Show content that does not fit on one screen

• .frame property

• Where on the screen is the ScrollView?

• .contentSize property

• How large is the scrollable area?

36 Simon Voelker, Philipp Wacker: iOS Application Development

Page 37: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

37

let aView = UIView.init(frame: CGRect.init( x: 0, y: 0, width: 2000, height: 2000))

self.theScrollView.addSubview(aView)self.theScrollView.contentSize = aView.frame.size

Setting the contentSize

Simon Voelker, Philipp Wacker: iOS Application Development

Page 38: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

ScrollView with StackView• Size of a StackView is defined by its content

38 Simon Voelker, Philipp Wacker: iOS Application Development

Page 39: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Content Insets

• Use insets to provide padding to your content

• e.g., on the bottom when displaying the keyboard

39 Simon Voelker, Philipp Wacker: iOS Application Development

Page 40: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

Content Insets

40 Simon Voelker, Philipp Wacker: iOS Application Development

let contentInsets = UIEdgeInsetsMake(0.0, 0.0, 300, 0.0)self.theScrollView.contentInset = contentInsets

self.theScrollView.scrollIndicatorInsets = contentInsets

Page 41: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

UIScrollViewDelegate

41 Simon Voelker, Philipp Wacker: iOS Application Development

func scrollViewDidScroll(_ scrollView: UIScrollView) {} func viewForZooming(in scrollView: UIScrollView) -> UIView? {} func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {}

...

Page 42: iOS Application Development - RWTH Aachen University€¦ · Simon Völker & Philipp Wacker . Media Computing Group . RWTH Aachen University . hci.rwth-aachen.de/ios. Lecture 5: Swift

MVC in iOS

42 Simon Voelker, Philipp Wacker: iOS Application Development

View Model

User action Update

Update Notify

Controller

Storyboard

ViewController

ModelController

Model Classes

Persistent Storage