-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslides.html
More file actions
1721 lines (1686 loc) · 62.1 KB
/
Copy pathslides.html
File metadata and controls
1721 lines (1686 loc) · 62.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>iOS Development</title>
<link rel="stylesheet" href="./myriad/css/slides.css">
<link rel="stylesheet" href="./myriad/css/myriad.css">
<link rel="stylesheet" href="highlight.css">
<style>
html { --text-size: calc(11 * var(--pt)); }
pre code { font-size: calc(6 * var(--pt)); }
</style>
</head>
<body>
<section class="title">
<h1>An Introduction to<br>iOS Development</h1>
<div class="context">Embedded Systems</div>
<div class="author">Victor Poupet</div>
<time datetime="2019-07-24">July 24, 2019</time>
</section>
<section class="no-page-counter"><img src="figures/Montpellier.jpg" style="height: 100%; width: 100%"></section>
<section class="section">
<h1>A Word About iOS</h1>
</section>
<section class="split">
<h1>iOS</h1>
<div class="side">
<div class="center"><img src="figures/screenshot.jpg" style="width: 80%"></div>
</div>
<div class="side">
<ul>
<li>released in 2007 (with the first generation iPhone)</li>
<li>Multi-touch interface (almost button-less)</li>
<li>Gesture recognition (swipe, pinch, etc.)</li>
<li>Somewhat based on Mac OSX</li>
<li>Core Foundation and Foundation frameworks</li>
<li>Cocoa touch instead of Cocoa</li>
<li>Darwin (Unix-like system)</li>
</ul>
</div>
</section>
<section class="split">
<h1>Version History</h1>
<div class="side"><img src="figures/iOS_versions.jpg" style="width: 100%"></div>
<div class="side">
<ul>
<li>2007 - iPhone OS 1</li>
<li class="only" data-step="0">2008 - iPhone OS 2 (iPhone SDK)</li>
<li class="only highlight">2008 - iPhone OS 2 (iPhone SDK)</li>
<li>2009 - iPhone OS 3 (iPad)</li>
<li>2010 - iOS 4 (limited multitasking)</li>
<li>2011 - iOS 5 (iCloud)</li>
<li>2012 - iOS 6</li>
<li>2013 - iOS 7 (extended multitasking)</li>
<li class="only" data-step="0">2014 - iOS 8 (Swift, Metal)</li>
<li class="only highlight">2014 - iOS 8 (Swift, Metal)</li>
<li>2015 - iOS 9 (split view)</li>
<li>2016 - iOS 10 (Siri, 3D touch)</li>
<li>2017 - iOS 11 (ARKit, Files)</li>
<li>2018 - iOS 12 (ARKit2, CarPlay)</li>
<li>2019 - iOS 13 (iPadOS)</li>
</ul>
</div>
</section>
<section class="split">
<h1>Fragmentation</h1>
<div class="side">
<ul>
<li>iOS runs on a limited number of devices (similar architecture, similar iterface)</li>
<li>Most devices run the latest version of iOS</li>
<li>OS, hardware and SDK are developped by the same company</li>
</ul>
</div>
<div class="side">
<ul>
<li>Devices have varying screen size and resolution</li>
<li>Some features are not available to all devices (multitasking, Siri, camera, etc.)</li>
<li>Two main device families :
<ul>
<li>iPhone</li>
<li>iPad</li>
</ul>
</li>
</ul>
</div>
</section>
<section class="single">
<h1>Distribution</h1>
<p>The only officially accepted way to add apps to an iOS terminal is to use Apple's <em>App Store</em></p>
<ul>
<li>Apple has total control over which apps are accepted on the store
<ul>
<li>delay between submission and availability</li>
<li>Apple guidelines are strict and sometimes difficult to match</li>
<li>guarantees a minimal quality of apps</li>
</ul>
</li>
<li>developper license (99$ / year) necessary to submit an app</li>
<li>free developper account sufficient to develop and install apps on personal devices</li>
<li>developpers receive 70% of the price of the sold app</li>
</ul>
</section>
<section class="single">
<h1>About this Lecture</h1>
<p>iOS development is a vast topic</p>
<ul>
<li>many elements cannot be addressed in this lecture (including important ones)</li>
<li>even with more time, one never fully knows all about iOS programming
<ul>
<li>hundreds of existing classes</li>
<li>thousands of methods and attributes</li>
<li>best way to learn is to look for examples, ask questions and read the documentation</li>
</ul>
</li>
<li>the API, SDK and language evolve quickly
<ul>
<li>examples and discussions on the Internet are already outdated</li>
<li>this lecture is also already outdated</li>
</ul>
</li>
<li>most elements are interconnected
<ul>
<li>no natural order to present things</li>
<li>examples require to use some elements before explaining them</li>
</ul>
</li>
</ul>
</section>
<section class="section">
<h1>Mobile Development</h1>
</section>
<section class="split">
<h1>Mobile Terminals</h1>
<div class="side"><img src="figures/iphone_ipad.jpg" style="width: 100%"></div>
<div class="side">
<ul>
<li>Miniature computer</li>
<li>Embedded system
<ul>
<li>limited resources</li>
</ul>
</li>
<li>Reactive system
<ul>
<li>based on user interface</li>
</ul>
</li>
<li>Smartphone vs.Tablet
<ul>
<li>screen size is different</li>
<li>phone calls</li>
<li>most of the rest is identical</li>
</ul>
</li>
</ul>
</div>
</section>
<section class="split">
<h1>Architecture</h1>
<div class="side"><img src="figures/appleA10.png" style="width: 100%"></div>
<div class="side">
<p>Processor</p>
<ul>
<li>ARM (designed by Apple)</li>
</ul><br>
<p>Storage</p>
<ul>
<li>Flash memory</li>
</ul><br>
<p>Other hardware</p>
<ul>
<li>Network</li>
<li>Accelerometer, gyroscope</li>
<li>GPS location</li>
<li>Camera</li>
<li>etc.</li>
</ul>
</div>
</section>
<section class="split">
<h1>iOS vs. Android</h1>
<div class="side"><img src="figures/iOS.png" style="position: absolute;top: calc(12 * var(--mm));left: calc(32 * var(--mm));width: 20%">
<p><strong>Developper</strong></p>
<ul>
<li>Apple</li>
</ul><br>
<p><strong>Programming language</strong></p>
<ul>
<li>Swift and/or Objective C</li>
<li>some C and C++</li>
</ul><br>
<p><strong>Development environment</strong></p>
<ul>
<li>XCode</li>
</ul><br>
<p><strong>Development kit</strong></p>
<ul>
<li>iOS SDK</li>
</ul>
</div>
<div class="side"><img src="figures/android.jpg" style="position: absolute;top: calc(8 * var(--mm));right: calc(5 * var(--mm));width: 15%">
<p><strong>Developper</strong></p>
<ul>
<li>Google</li>
</ul><br>
<p><strong>Programming language</strong></p>
<ul>
<li>Java</li>
<li>some C++</li>
<li>(Kotlin, possibly Swift in the future)</li>
</ul><br>
<p><strong>Development environment</strong></p>
<ul>
<li>Eclipse</li>
</ul><br>
<p><strong>Development kit</strong></p>
<ul>
<li>Android SDK</li>
</ul>
</div>
</section>
<section class="single">
<h1>Cross-Compilation</h1><img src="figures/cross_compilation.svg" style="width:100%">
</section>
<section class="single">
<h1>Constraints</h1>
<p>Limited resources</p>
<ul>
<li>Memory
<ul>
<li>Beware of memory leaks</li>
<li>Java (Android): garbage collector</li>
<li>Swift / Objective C (iOS): Automatic Reference Counting (ARC)</li>
</ul>
</li>
<li>Energy
<ul>
<li>Some activities are very demanding (GPS, camera, etc.)</li>
<li>Avoid inefficient algorithms (CPU drains battery)</li>
</ul>
</li>
</ul><br>
<p>Handle urgent events</p>
<ul>
<li>Low memory alert</li>
<li>Low battery alert</li>
<li>Phone call interruption</li>
</ul>
</section>
<section class="single">
<h1>Other Constraints</h1><strong>User interface</strong>
<ul>
<li>Simple and convenient to use</li>
<li>Should be reactive</li>
<li>Should fit with the OS and other apps ("look and feel")</li>
</ul><strong>Fiability</strong>
<ul>
<li>Avoid errors and crashing (no error handling when app crashes)</li>
</ul><strong>Security</strong>
<ul>
<li>Device holds sensitive personal data</li>
<li>Apps each have their own files directory and run in a sandbox (iOS only)</li>
</ul><strong>Speed</strong>
<ul>
<li>App should work on slower devices, with other apps using resources</li>
</ul>
</section>
<section class="single">
<h1>Workflow</h1>
<p>Mobile development is built around the user interface (screen):</p><br>
<ul>
<li>Design <em>Graphical User Interface</em> (GUI)</li>
<li>Define and handle <em>events</em></li>
</ul><br><br>
<div class="center">
<p>→ Writing the first app in XCode : <em>Greeting</em></p>
</div>
</section>
<section class="section">
<h1>A Short Introduction<br>to Swift</h1>
</section>
<section class="split">
<h1>Swift</h1>
<div class="side">
<ul>
<li>iOS applications should be programmed using objective C or Swift (since iOS 8)</li>
<li>Swift is highly recommended (simpler, faster and better supported from now on)</li>
<li>Modern syntax inspired by modern languages</li>
<li>Heavily object-oriented</li>
</ul>
</div>
<div class="side">
<ul>
<li>Developed by Apple and published in 2014</li>
<li>Lighter and more elegant syntax</li>
<li>Better performance with LLVM compiler</li>
<li>Can be tested with playgrounds or read-eval-print loop (REPL)</li>
</ul>
</div>
</section>
<section class="split">
<h1>General Syntax</h1>
<div class="side">
<pre><code class="cpp">let a = 10
var b: Int = 12
b += a
let primes = [2, 3, 5, 7, 11, 13]
func isPrime(n: Int) -> Bool {
for x in 2 ..< n {
if n % x == 0 {
return false
}
}
return true
}
var x: Int
x = 12
if isPrime(x) {
println("\(x) is prime.")
} else {
println("\(x) is not prime.")
}</code></pre>
</div>
<div class="side">
<ul>
<li>Variables are declared with <code>let</code> (immutable) or <code>var</code> (mutable)</li>
<li>Variables are typed but type can be inferred by the compiler</li>
<li>Functions are declared with <code>func</code>, arguments and return values are typed</li>
</ul>
</div>
</section>
<section class="split">
<h1>Optionals</h1>
<div class="side">
<pre><code class="swift">var a: Int? = 12
var b = 10
b += a // error, a could be nil
if a != nil {
b += a! // a is unwrapped
}
a = nil
b = nil // error
if let aValue = a {
// conditional unwrapping
b += aValue
}</code></pre>
</div>
<div class="side">
<p>Swift introduces “optional” versions of all types</p>
<ul>
<li>An optional type <code>T?</code> represents a value that can be either <code>nil</code> or a value of type <code>T</code></li>
<li>Always make sure an optional is not nil before using its value</li>
<li>Optionals can be unwrapped to work with the non <code>nil</code> value</li>
</ul>
</div>
</section>
<section class="split">
<h1>Object Programming</h1>
<div class="side">
<p>In a “classical” program</p>
<ul>
<li>program has variables</li>
<li>program calls functions that affect variables</li>
</ul>
</div>
<div class="side">
<p>In object-oriented programming</p>
<ul>
<li>variables contain <em>objects</em></li>
<li>objects contain their own “sub-variables” called <em>properties</em></li>
<li class="skip">objects carry their own functions called <em>methods</em></li>
<li>possible to access and modify the properies of each object</li>
<li>possible to call a specific object method (each objects does its own work)</li>
</ul>
</div>
</section>
<section class="split">
<h1>Classes and Instances</h1>
<div class="side">
<pre><code class="swift">class Person {
var name: String // a property
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func greet() { // a method
print("Hello, \(name)!")
}
func increaseAge(years: Int) {
age += years
}
func ageDifferenceWith(other: Person) -> Int {
return age - other.age
}
}</code></pre>
</div>
<div class="side">
<ul>
<li>A class is a general description of how objects should be and behave</li>
<li>Classes have properties and methods</li>
<li>An instance is a specific object, that matches the description of the class</li>
<li>Different instances of the same class can have different values of their properties</li>
</ul>
</div>
</section>
<section class="split">
<h1>Inheritance</h1>
<div class="side">
<pre><code class="swift">class Student: Person {
var school: String?
var year: Int?
init(name: String, age: Int, school: String, year: Int){
self.school = school
self.year = year
super.init(name: name, age: age)
}
override init(name: String, age: Int) {
super.init(name: name, age: age)
}
override func greet() {
print("Hi, student \(name)")
}
func classDifferenceWith(other: Student) -> Int? {
if let year = year, oYear = other.year {
return year - oYear
}
return nil
}
}</code></pre>
</div>
<div class="side">
<ul>
<li>Classes can be defined as a refinement of an existing one</li>
<ul>
<li>the existing class is the parent of the new class (child)</li>
<li>inherits properties and methods from its parent</li>
</ul>
<li>Methods can be redefined in the child (override)</li>
</ul>
</div>
</section>
<section class="single">
<h1>Creating Objects</h1>
<p>To create an object</p>
<ul>
<li>allocate the necessary memory</li>
<li>initialize the object according to the specifications of the class</li>
</ul>
<p>These steps are performed by the <code>init</code> function</p><br>
<p>Objects are removed from memory automatically when nothing points to them (see later section)</p>
</section>
<section class="single">
<h1>Illustration</h1>
<p>Back to the <em>Greeting</em> app.</p>
</section>
<section class="section">
<h1>Graphical User Interface</h1>
</section>
<section class="split">
<h1>Model View Controller</h1>
<div class="side center"><img src="figures/mvc_sketch.jpg" style="width: 100%"></div>
<div class="side">
<p>Design pattern, for structuring an application in 3 parts</p>
<ul>
<li><em>model</em> : data of the application</li>
<li><em>view</em> : user interface, representation of data from the model</li>
<li><em>controller</em> : management logic (program)</li>
</ul>
</div>
</section>
<section class="split">
<h1>Model View Controller</h1>
<div class="side center"><img src="figures/mvc.png" style="width: 100%"></div>
<div class="side">
<ul>
<li>Communication protocol between separate parts</li>
<li>Internal implementation of parts is independent</li>
<li>Better structure, easier to maintain</li>
</ul>
</div>
</section>
<section class="split">
<h1>Model</h1>
<div class="side">
<pre><code class="swift">class Player {
var cards: [Card]
var game: Game
var points: Int
var isTurnPlayer: Bool
init(...) {
...
}
func playCard(card: Card) {
...
}
func endTurn() {
...
}
func draw(numberOfCards: Int) {
...
}
...
}</code></pre>
</div>
<div class="side">
<p>In iOS, the model is represented by the data object classes</p>
<ul>
<li>the model can be stored in the terminal memory in different forms (<em>Core Data</em>, archiving, <em>NSUserDefaults</em>)</li>
<li>organization of the data structures with classes and attributes</li>
</ul>
</div>
</section>
<section class="split">
<h1>View</h1>
<div class="side center"><img src="figures/UIView.jpg" style="width: 100%"></div>
<div class="side">
<p><em>Cocoa Touch</em> (iOS UI Framework) defines a class <code>UIView</code></p>
<ul>
<li>all views should inherit from <code>UIView</code></li>
<li>many subclasses are already defined</li>
<li>developers can subclass any existing class to add specific behavior</li>
</ul><br>
<p><strong>Note :</strong> the elements added to the Storyboard are subclasses of <code>UIView</code></p>
</div>
</section>
<section class="split">
<h1>UIView</h1>
<div class="side">
<p>To create a user interface</p>
<ul>
<li>create a view hierarchy</li>
<li>associate specific code (in the controller) to the possible events that the view elements can react to</li>
<ul>
<li>using the existing subclasses ensures a uniform experience for the users across apps</li>
</ul>
</ul>
</div>
<div class="side">
<div class="only" data-step="0">
<p>What is a view ?</p>
<ul>
<li>A rectangle</li>
<ul>
<li>in which it is possible to draw</li>
<li>that reacts to events</li>
<li>contained in a window (UIWindow)</li>
</ul>
</ul>
</div>
<div class="only">
<p>View hierarchy</p>
<ul>
<li>each view is placed in a superview</li>
<li>a view can have any number of subviews</li>
<li>lower level views are drawn on top of higher level</li>
<ul>
<li>subviews of a same view are drawn one after the other</li>
<li>views can have transparent elements</li>
</ul>
</ul>
</div>
</div>
</section>
<section class="split">
<h1>Position</h1>
<div class="side">
<pre><code class="swift">let labelFrame = CGRect(x: 20, y: 50, width: 300, height: 50)
let label = UILabel(frame: labelFrame)
label.text = "Hello"
view.addSubview(label)
label.center = CGPoint(x: 170, y: 200)</code></pre>
</div>
<div class="side">
<A>view is positioned relatively to its parent view :</A>
<ul>
<li>the <code>frame</code> is a <code>CGRect</code> that defines the position and size of a view relative to its parent</li>
<li>the <code>center</code> is a <code>CGPoint</code> that positions the view without changing its size</li>
<li>the <code>bounds</code> is a <code>CGRect</code> describing the frame of the view in its own coordinates. Usually origin is <em>(0, 0)</em></li>
</ul>
</div>
</section>
<section class="split">
<h1>UIView Hierarchy</h1>
<div class="side">
<pre><code class="swift">var superview: UIView?
var subviews: [UIView]
var window: UIWindow?
func addSubview(UIView)
func bringSubviewToFront(UIView)
func sendSubviewToBack(UIView)
func removeFromSuperview()
func insertSubview(UIView, at: Int)
func insertSubview(UIView, aboveSubview: UIView)
func insertSubview(UIView, belowSubview: UIView)
func exchangeSubview(at: Int, withSubviewAt: Int)
func isDescendant(of: UIView) -> Bool</code></pre>
</div>
<div class="side">
<ul>
<li>The UIView class has methods to manipulate the hierarchy</li>
<li>The superview owns its subviews</li>
</ul>
</div>
</section>
<section class="single">
<h1>Example : <em>Dominion</em></h1>
<pre><code style="font-size: calc(5 * var(--pt))">GameView
LogView
UITextView
StacksView
KingdomCardsView
StackView (x10)
UIImage (background)
UILabel (cost)
UILabel (remaining)
TreasureCardsView
StackView (x3)
UIImage (background)
UILabel (cost)
UILabel (remaining)
VictoryCardsView
StackView (x4)
UIImage (background)
UILabel (cost)
UILabel (remaining)
PlayerView
ChatView
UITextInput
UITextView
ButtonsView
UILabel (instruction)
UIButton (x3)
HandView
CardView (x3)
UIImage</code></pre><img src="figures/dominion.png" style="position: absolute; width: 65%; right: var(--mm)">
</section>
<section class="split">
<h1>About the Views</h1>
<div class="side center"><img src="figures/coordinates.jpg" style="width: 60%"></div>
<div class="side">
<p>Cartesian coordinates system</p>
<ul>
<li><em>(0, 0)</em> at the top-left corner</li>
<li>coordinates are in points (not pixels)</li>
<li>handles screen density differences seamlessly</li>
<ul>
<li>iPhone/iPod 3.5" (1 to 4) : 320 × 480</li>
<li>iPhone/iPod 4" (5) : 320 × 568</li>
<li>iPhone 4.7" (6 to 8) : 375 × 667</li>
<li>iPhone 5.5" (6+ to 8+) : 414 × 736</li>
<li>iPhone X : 375 × 812</li>
<li>iPad/iPad mini : 768 × 1024</li>
<li>iPad Pro 10.5" : 834 × 1112</li>
<li>iPad Pro 12.9" : 1024 × 1366</li>
</ul>
</ul>
</div>
</section>
<section class="single">
<h1>Create a View</h1>
<p>Create a class that inherits from <code>UIView</code></p>
<ul>
<li>If necessary, define the method <code>drawRect</code></li>
<li>If the view must be refreshed, use <code>setNeedsDisplay</code></li>
</ul><br>
<p>See example : <em>Multiplication Tables</em></p>
</section>
<section class="single">
<h1>UIViewController</h1><img src="figures/viewcontroller.png" style="width: calc(100 * var(--mm)); position: absolute; top: calc(15 * var(--mm)); right: 0">
<ul style="z-index: 1; position: absolute; top: calc(43 * var(--mm)); width: 70%">
<li><em>View controllers</em> are objects that manage a view hierarchy</li>
<li>The class <code>UIViewController</code> implements view controllers</li>
<li>Each controller has a single main view</li>
<li>View controllers are responsible for loading, displaying and releasing the view when needed</li>
<li>View controllers also (should) manage the events that take place in the view</li>
</ul>
</section>
<section class="single">
<h1>Classes Hierarchy</h1>
<div class="center"><img src="figures/viewcontrollerhierarchy.png" style="width: 65%"></div>
</section>
<section class="single">
<h1>View Controller Cycle</h1>
<div class="only" data-step="0">
<p>Important methods :</p>
<ul>
<li><code>init</code> : initialization of the view controller</li>
<li><code>loadView</code>, <code>viewDidLoad</code> : creation of the view</li>
<li><code>viewWillAppear</code>, <code>viewDidAppear</code> : the view is displayed on screen</li>
<li><code>viewWillDisappear</code>, <code>viewDidDisappear</code> : the view is removed from screen</li>
</ul>
</div>
<div class="only center"><img src="figures/viewcontrollercycle.png" style="height: calc(70 * var(--mm))"></div>
</section>
<section class="split">
<h1>Kinds of View Controllers</h1>
<div class="side">
<p>Content view controllers display content</p>
<ul>
<li>simplest view controller</li>
<li>manages one view hierarchy</li>
<li>a view hierarchy is usually managed by a single content VC</li>
</ul><br>
<p>examples : table view controller, most user-made view controllers</p>
</div>
<div class="side">
<p>Container view controllers arrange content of other view controllers</p>
<ul>
<li>contains content owned by other view controllers</li>
<li>manages a list of other view controllers (children)</li>
</ul><br>
<p>examples : navigation controller, tab bar controller, split view controller</p>
</div>
</section>
<section class="split">
<h1>Nested Content VC</h1>
<div class="side">
<pre><code class="swift">let subVC = SomeViewController()
addChild(subVC)
subVC.didMove(toParent: self)
view.addSubview(subVC.view)</code></pre>
</div>
<div class="side">
<p>Most of the time one content view controller is sufficient</p>
<p>It is possible to nest content view controllers when necessary</p>
<ul>
<li>create the sub controller</li>
<li>add it as a child to the main controller</li>
<li>notify the child that it has been added to the parent</li>
<li>add the child's view to the parent's view</li>
</ul>
</div>
</section>
<section class="single">
<h1>Navigation Controller</h1>
<div class="center"><img src="figures/navigation.png" style="width: 65%"></div>
</section>
<section class="single">
<h1>Tab Bar Controller</h1>
<div class="center"><img src="figures/tabbar.png" style="width: 65%"></div>
</section>
<section class="single">
<h1>Split View Controller</h1>
<div class="center"><img src="figures/splitview.png" style="width: 65%"></div>
</section>
<section class="split">
<h1>Create a Controller</h1>
<div class="side">
<pre><code class="swift">var view: UIView!
var isViewLoaded: Bool
func loadView()
func viewDidLoad()
func viewWillAppear(Bool)
func viewDidAppear(Bool)
func viewWillDisappear(Bool)
func viewDidDisappear(Bool)
func viewWillLayoutSubviews()
func viewDidLayoutSubviews()
func updateViewConstraints()
...</code></pre>
</div>
<div class="side">
<ul>
<li>Create new class that inherits from <code>UIViewController</code></li>
<li>Override relevant methods to define management of view</li>
<li>Define when the controller should be loaded</li>
</ul>
</div>
</section>
<section class="single">
<h1>Initialization</h1>
<p>The UIViewController class defines two possible initializers</p>
<ul>
<li><code>init(nibName: String?, bundle: Bundle?)</code> is used when creating a new view controller programmatically</li>
<li><code>init?(coder: NSCoder)</code> is used when the view controller is automatically created by iOS (from a storyboard for instance)</li>
</ul><br>
<p><strong>Warning :</strong> The view is not loaded at the initialization of the controller</p>
<ul>
<li>Accessing view elements will crash the application</li>
<li>Initialization of the view should be done in <code>viewDidLoad</code> and <code>viewDidAppear</code></li>
</ul>
</section>
<section class="single">
<h1>Initialization</h1>
<ul>
<li>Simplest way (with empty view) :
<pre><code class="swift"> let viewController = UIViewController(nibName: nil, bundle: nil)</code></pre>
</li>
<li>When subclassing (with empty view) :
<pre><code class="swift">class MyViewController: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
}
}</code></pre>
</li>
<li>If using a <code>Nib</code> file (view is created with Nib contents) :
<pre><code class="swift">class MyViewController: UIViewController {
init() {
super.init(nibName: "nameOfNibFile", bundle: nil)
}
}</code></pre>
</li>
</ul>
</section>
<section class="single">
<h1>Example</h1>
<p>A simple example : <em>Quiz</em></p>
<p>Slightly more complex example : <em>Tabs</em> (<code>UITabBarController</code>)</p>
</section>
<section class="split">
<h1>Switching Controllers</h1>
<div class="side">
<div class="only" data-step="0">
<pre><code class="swift">let vc = MyViewController()
UIApplication.shared.keyWindow!.
rootViewController = vc</code></pre>
</div>
<div class="only">
<pre><code class="swift">let vc = MyViewController()
present(vc, animated: true, completion: nil)
dismiss(animated: true, completion: nil)</code></pre><br>
<p>Example : <em>SwitchController</em></p>
</div>
<div class="only" data-start="2">
<pre><code class="swift">// create a navigation controller and its
// root controller
let vc = ViewController()
let nc = UINavigationController(rootViewController: vc)
window.rootViewController = nc
// create a new controller an push it on top
let vc2 = ViewController()
navigationController?.pushViewController(vc2, animated: true)
// pop the top controller (thus recovering
// the previous one)
navigationController?.
popViewController(animated: true)</code></pre><br>
<p>Example : <em>Navigation Controller</em></p>
</div>
</div>
<div class="side">
<p>There are many ways to switch from a view controller to another one (and change the displayed view)</p>
<ul>
<li class="uncover" data-start="0">changing the root controller of the application window (should be avoided)</li>
<li class="uncover">presenting a new controller</li>
<li class="uncover">using a navigation controller</li>
</ul><br>
<p class="uncover">Controller transitions can be defined in the Interface Builder (storyboards)</p>
</div>
</section>
<section class="section">
<h1>Responding to Events</h1>
</section>
<section class="single">
<h1>Touch and Motion</h1><img src="figures/events.png" style="width: 45%; position:absolute; top: 40%; right: 1%">
<p>There are 4 kinds of basic events to interact with the UI :</p>
<ul>
<li>touch events</li>
<ul>
<li>single or multiple touches on the screen tap, double-tap, move, etc.</li>
</ul>
<li>motion events (iOS 3)</li>
<ul>
<li>detected by the accelerometer<br>and gyroscope</li>
<li>shake, rotation, acceleration...</li>
</ul>
<li>Remote control events (iOS 4)</li>
<li>Presses events (iOS 9)</li>
</ul>
<p>The class of objects that are prepared to handle such events is <code>UIResponder</code></p>
<ul>
<li>Subclasses include <code>UIApplication</code>, <code>UIWindow</code>, <code>UIView</code> and <code>UIViewController</code></li>
</ul>
</section>
<section class="split">
<h1>The Responder Chain</h1>
<div class="side"><img src="figures/responder_chain.png" style="width: 100%"></div>
<div class="side">
<div class="only" data-step="0">
<p>When an event is detected it is sent to a specific responder</p>
<ul>
<li>for a touch, the view where it happened</li>
<li>for another event, the <em>first responder</em></li>
</ul>
</div>
<div class="only">
<p>The event goes through the responder chain until a responder handles it</p>
<ul>
<li>from a view to its super view</li>
<li>from a top view to its controller</li>
<li>from a controller to the super view of its view</li>
<li>from the main controller to the window, and the application</li>
</ul>
</div>
</div>
</section>
<section class="split">
<h1>Hit Test</h1>
<div class="side"><img src="figures/hit_test.png" style="width: 100%"></div>
<div class="side">
<p>Returns where a touch occurred</p>
<ul>
<li>recursive calls to <code>hitTest:withEvent:</code></li>
<li>if the touch is in a view, all its sub views are tested</li>
<li>if a touch is out of the bounds of a given view, its subviews are not tested</li>
<li>return the lowest view in the hierarchy that contains the point of touch</li>
</ul><br>
<p>Only the point where the touch started counts</p>
</div>
</section>
<section class="split">
<h1>UIEvent</h1>
<div class="side">
<pre><code class="swift">var timestamp: NSTimeInterval
var type: UIEventType
var subtype: UIEventSubtype
func allTouches() -> Set<UITouch>?
func touches(for: UIView) -> Set<UITouch>?
func touches(for: UIWindow) -> Set<UITouch>?</code></pre>
</div>
<div class="side">
<p><code>UIEvent</code> is the class of objects created when an event occurs</p>
<ul>
<li>an UIEvent object has a sub-type (none, motion or possible remote control events)</li>
<li>touch events contains a set of <code>UITouch</code> objects</li>
<li>all events have a time stamps that corresponds to the time when the event was created</li>
</ul>
</div>
</section>
<section class="single">
<h1>UIEventSubtype</h1>
<pre><code class="swift">enum UIEventSubtype : Int {
case none
case motionShake
case remoteControlPlay
case remoteControlPause
case remoteControlStop
case remoteControlTogglePlayPause
case remoteControlNextTrack
case remoteControlPreviousTrack
case remoteControlBeginSeekingBackward
case remoteControlEndSeekingBackward
case remoteControlBeginSeekingForward
case remoteControlEndSeekingForward
}</code></pre>
</section>
<section class="single">
<h1>Shake</h1>
<p><em>UIKit</em> detects 3 events associated with a shake</p>
<ul>
<li><code>motionBegan(_:with:)</code> when a shake motion begins</li>
<li><code>motionEnded(_:with:)</code> when a shake motion ends</li>
<li><code>motionCancelled(_:with:)</code> when a shake motion is cancelled by another event (app becomes inactive, view disappears, etc.)</li>
<ul>
<li>clean state after <code>motionBegan</code></li>
</ul>
</ul><br>
<p>The motion for a shake is <code>UIEventSubtypeMotionShake</code></p>
</section>
<section class="single">
<h1>Touch</h1>
<p>There are 4 methods to handle touch events</p>
<ul>
<li><code>touchesBegan(_:with:)</code></li>
<li><code>touchesMoved(_:with:)</code></li>
<li><code>touchesEnded(_:with:)</code></li>
<li><code>touchesCancelled(_:with:)</code></li>
</ul><br>
<p>The first parameter of each method contains the set of UITouch objects that are relevant to the method (began, moved, ended or cancelled)</p>
</section>
<section class="single">
<h1>Example</h1>
<p>Touch events in <em>Bezier</em></p>
</section>
<section class="single">
<h1>Gesture Recognizers</h1>
<p>Some specific gestures can be recognized automatically :</p>