iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group...

20
Simon Völker & Philipp Wacker Media Computing Group RWTH Aachen University hci.rwth-aachen.de/ios Lecture 8: Clousures, Improving Complex Input Screens, and Animations iOS Application Development

Transcript of iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group...

Page 1: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

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

hci.rwth-aachen.de/ios

Lecture 8: Clousures, Improving Complex Input Screens, and Animations

iOS Application Development

Page 2: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Seminar Review Meetings• First Seminar meetings are next week:

2 Simon Voelker, Philipp Wacker: iOS Application Development

Topic Group Presentation DiscussionsMapKit, CoreLocation Group 8 18.11.19, first group 11.11.19Debugging in Xcode and Instruments Group 4 18.11.19, second group 11.11.19SiriKit Group 2 19.11.19, second group 12.11.19Extensions & Inter-App communication Group 5 19.11.19, first group 12.11.19WatchOS Group 14 25.11.19, second group 18.11.19Swift UI Group 11 25.11.19, first group 18.11.19Scene Kit Group 7 26.11.19, first group 19.11.19GameplayKit Group 9 26.11.19, second group 19.11.19RealityKit & Reality Composer Group 1 09.12.19, first group 02.12.19Networking on iOS Group 6 2.12.19, first group 25.11.19ClassKit Group 3 2.12.19, second group 25.11.19Bringing People into AR Group 13 09.12.19, second group 02.12.19Core Data Group 10 3.12.19, first group 26.11.19Core ML + Create ML Group 12 3.12.19, second group 26.11.19

Page 3: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Seminar

3 Simon Voelker, Philipp Wacker: iOS Application Development

• 20 minute presentations + 10 min Q&A

• Deliverables: Slides + well documented demo code

Introduction Structure Demo

Page 4: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

4

Closures

Simon Voelker, Philipp Wacker: iOS Application Development

Page 5: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Closures• Similar to Objective-C Blocks and Java Lambdas

• Self-contained blocks of functionality

• „Anonymous functions“

5 Simon Voelker, Philipp Wacker: iOS Application Development

{ (parameters) -> return type in statements }

Page 6: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Closure Syntax• Function:

6 Simon Voelker, Philipp Wacker: iOS Application Development

func sumFunction(numbers: [Int]) -> Int { var total = 0 //Code return total}

let sum = sumFunction(numbers: [5,34,12,42])

• Closure:let sumClosure = { (numbers: [Int]) -> Int in var total = 0 //Code return total}

let sum = sumClosure([8,34,16,42])

Page 7: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Closure Types• No parameters, no return value:

7 Simon Voelker, Philipp Wacker: iOS Application Development

let printClosure1 = { () -> Void in print("This closure does not take any parameters and does not return a value.")}

• With parameters, no return value:let printClosure2 = { (string: String) -> Void in print(string)}

• No parameters, with return value:

• With parameters, with return value:

let randomNumberClosure1 = { () -> Int in return 0}

let randomNumberClosure2 = { (minValue: Int, maxValue: Int) -> Int in return 0}

Page 8: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Trailing Closure

8 Simon Voelker, Philipp Wacker: iOS Application Development

let sortedTracks = tracks.sorted(by: { (firstTrack, secondTrack) -> Bool in return firstTrack.starRating < secondTrack.starRating})

let sortedTracks = tracks.sorted { (firstTrack, secondTrack) -> Bool in return firstTrack.starRating < secondTrack.starRating}

Closure as the only argument:

Closure as the last argument:func performRequest(url: String, response: (Int) -> Void){ }

performRequest(url: "https://www.apple.com") { (data) in print(data)}

Page 9: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Simplifying Closures

9 Simon Voelker, Philipp Wacker: iOS Application Development

let sortedTracks = tracks.sorted { (firstTrack, secondTrack) -> Bool in return firstTrack.starRating < secondTrack.starRating}

Infer the return type:let sortedTracks = tracks.sorted { (firstTrack, secondTrack) in return firstTrack.starRating < secondTrack.starRating}

Use placeholder arguments:let sortedTracks = tracks.sorted { return $0.starRating < $1.starRating}

Automatic returnlet sortedTracks = tracks.sorted {$0.starRating < $1.starRating}

Page 10: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Mapping

10 Simon Voelker, Philipp Wacker: iOS Application Development

let names = ["Johnny", "Nellie", "Aaron", "Rachel"]var fullNames: [String] = [] for name in names { let fullName = name + " Smith" fullNames.append(fullName)}

Use the map() function:let fullNames = names.map { (name) -> String in return name + " Smith"}

Short version:let fullNames = names.map { $0 + " Smith" }

Page 11: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Filter

11 Simon Voelker, Philipp Wacker: iOS Application Development

let numbers = [4, 8, 15, 16, 23, 42]var numbersLessThan20: [Int] = [] for number in numbers { if number < 20 { numbersLessThan20.append(number) }}

Use the filter() function:let numbersLessThan20 = numbers.filter { (number) -> Bool in   return number < 20}

Short version:let numbersLessThan20 = numbers.filter { $0 < 20 }

Page 12: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Reduce

12 Simon Voelker, Philipp Wacker: iOS Application Development

let numbers = [8, 6, 7, 5, 3, 0, 9]var total = 0 for number in numbers { total = total + number}

Use the reduce() function:let total = numbers.reduce(0) { (currentTotal, newValue) -> Int in return currentTotal + newValue}

Short version:let total = numbers.reduce(0, { $0 + $1})

Page 13: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Capturing Values

13 Simon Voelker, Philipp Wacker: iOS Application Development

func makeIncrementer(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementer() -> Int { runningTotal += amount return runningTotal } return incrementer}

let incrementByTen = makeIncrementer(forIncrement: 10)

incrementByTen() //10incrementByTen() //20incrementByTen() //30

let incrementBySeven = makeIncrementer(forIncrement: 7)incrementBySeven() //7incrementByTen() //40let alsoIncrementBy10 = incrementByTenalsoIncrementBy10() //50

Page 14: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

14

Improving Complex Input Screens

Simon Voelker, Philipp Wacker: iOS Application Development

Page 15: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Improving Complex Input Screens

15 Simon Voelker, Philipp Wacker: iOS Application Development

Page 16: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

16

Animation

Simon Voelker, Philipp Wacker: iOS Application Development

Page 17: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

Why Animations?• Direct the User's Attention

• Keep the User Oriented

• Connect User Behaviors

17 Simon Voelker, Philipp Wacker: iOS Application Development

Page 18: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

What Can Be Animated?• UIView:

• frame

• bounds

• center

• transform

• alpha

18 Simon Voelker, Philipp Wacker: iOS Application Development

Page 19: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

UIView Animations

19 Simon Voelker, Philipp Wacker: iOS Application Development

Animate with Duration:

UIView.animate(withDuration: 2.0) { aView.alpha = 0.3}

Animation Closures:animate(withDuration:animations:) animate(withDuration:animations:completion:) animate(withDuration:delay:options:animations:completion:)

Page 20: iOS Application Development...Simon Voelker, Philipp Wacker: iOS Application Development Topic Group Presentation Discussions MapKit, CoreLocation Group 8 18.11.19, first group 11.11.19

20

Animation Playground

Simon Voelker, Philipp Wacker: iOS Application Development