ios application development...simon völker & philipp wacker: ios application development • a...

31
iOS Application Development Lecture 5: Protocols, Extensions,TabBar an Scroll Views Dr. Simon Völker & Philipp Wacker Media Computing Group RWTH Aachen University Winter Semester 2017/2018 http://hci.rwth-aachen.de/ios

Upload: others

Post on 13-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

iOS Application Development Lecture 5: Protocols, Extensions, TabBar an Scroll Views

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

Winter Semester 2017/2018

http://hci.rwth-aachen.de/ios

Page 2: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

Swift Protocols and Extensions

2

Page 3: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• 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

3

Protocols

Page 4: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development4

CustomStringConvertible

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

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

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

Page 5: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development5

CustomStringConvertibleclass 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 6: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development6

CustomStringConvertibleclass 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 7: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development7

Equatablestruct 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 8: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development8

Equatablestruct 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 9: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development9

Comparablestruct 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 10: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development10

Comparablestruct 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 11: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development11

Creating a Protocolprotocol FullyNamed { var fullName: String { get } func sayFullName()}

Page 12: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development12

Creating a Protocolprotocol 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 13: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• Design pattern that delegates functionality an instance of a class or structure

• Mostly implemented using Protocols

• Commonly used in UIKit

• UITableViewDelegate

• UIPickerViewDelegate

• From SpriteKit: SKPhysicsContactDelegate

• In contrast to normal protocols they often provide optional protocol requirements

13

Delegation

Page 14: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development14

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

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

• Works only with with Objective-C

• @objc Protocols can only be adapted by Objective-C classes

Page 15: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development15

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

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

• Works only with with Objective-C

• @objc Protocols can only be adapted by Objective-C classes

Page 16: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development16

Optional Protocol Requirements

public protocol SKPhysicsContactDelegate : NSObjectProtocol {

optional public func didBegin(_ contact: SKPhysicsContact)

optional public func didEnd(_ contact: SKPhysicsContact)}

• The SKPhysicsContactDelegate from SpriteKit

Page 17: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• Extensions add functionality to types that are already defined.

• You can add:

• computed properties, define methods, provide new initializers, or conform an existing type to a protocol

• You cannot add properties!

17

Extensionsextension 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 18: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development18

Extensionsstruct 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 19: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

UIView Controllers

19

Page 20: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

Tab Bar Controller

20

Page 21: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• View Controllers can be in one of these stats:

• View not loaded

• View appearing

• View appeared

• View disappearing

• View disappeared

21

View Controller Life CycleNot Loaded

AppearviewDidAppear(_:)

DisappearedviewDidDisappear(_:)

viewDidLoad()

viewWillAppear(_:)

viewWillDisappear(_:)

Page 22: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

Application Life Cycle

22

Page 23: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

AppDelegate.swift

23

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

func applicationWillResignActive(_ application: UIApplication) { }

func applicationDidEnterBackground(_ application: UIApplication) { }

func applicationWillEnterForeground(_ application: UIApplication) { }

func applicationDidBecomeActive(_ application: UIApplication) { }

func applicationWillTerminate(_ application: UIApplication) { }

Page 24: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

Scroll Views

24

Page 25: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• UIScrollView

• Parent of UITableView

• Allows to 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?

25

ScrollView

Page 26: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development26

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

Page 27: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• Size of a StackView is defined by its content

27

ScrollView with StackView

Page 28: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

• Use insets to provide padding to your content

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

28

Content Insets

Page 29: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

Content Insets

29

let contentInsets = UIEdgeInsetsMake(0.0, 0.0, 300, 0.0)

self.theScrollView.contentInset = contentInsets

self.theScrollView.scrollIndicatorInsets = contentInsets

Page 30: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

UIScrollViewDelegate

30

func scrollViewDidScroll(_ scrollView: UIScrollView) {}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {

} func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {

}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {}

...

Page 31: iOS Application Development...Simon Völker & Philipp Wacker: iOS Application Development • A protocols defines a blueprint of methods, properties and other requirements • Protocols

Simon Völker & Philipp Wacker: iOS Application Development

MVC in iOS

31

Controller

View Model

User action Update

Update Notify

Controller

Storyboard

ViewController

ModelController

Model Classes

Persistent Storage