Programming IOS 12: Dive Deep Into Views, View Controllers, and Frameworks
9781492044635, 1881881911, 1492044636
If you're grounded in the basics of Swift, Xcode, and the Cocoa framework, this book provides a structured explanat
270
6
13MB
English
Pages 1176
[1177]
Year 2018
Report DMCA / Copyright
DOWNLOAD PDF FILE
Table of contents :
Cover......Page 0
Copyright......Page 4
Table of Contents......Page 5
The Scope of This Book......Page 21
Versions......Page 25
From the Programming iOS 4 Preface......Page 26
Using Code Examples......Page 28
How to Contact Us......Page 29
Part I. Views......Page 31
Chapter 1. Views......Page 33
How an App Launches......Page 34
Subclassing UIWindow......Page 36
Experimenting with Views......Page 37
Subview and Superview......Page 38
Visibility and Opacity......Page 41
Frame......Page 42
Bounds and Center......Page 43
Transform......Page 48
The View Environment......Page 51
Window Coordinates and Screen Coordinates......Page 52
Trait Collections and Size Classes......Page 54
Layout......Page 55
Autoresizing......Page 57
Autolayout and Constraints......Page 59
Implicit Autoresizing Constraints......Page 62
Creating Constraints in Code......Page 63
Constraints as Objects......Page 69
Margins and Guides......Page 72
Intrinsic Content Size and Alignment Rects......Page 77
Stack Views......Page 79
Internationalization......Page 82
Mistakes with Constraints......Page 83
Configuring Layout in the Nib......Page 86
Creating a Constraint......Page 87
Viewing and Editing Constraints......Page 89
Problems with Nib Constraints......Page 91
Varying the Screen Size......Page 93
Conditional Interface Design......Page 94
View Debugger......Page 96
Designable Views and Inspectable Properties......Page 98
Layout Events......Page 101
Layout-Driven UI......Page 102
Simulating Autolayout......Page 104
Images and Image Views......Page 107
Image Files......Page 108
Image Views......Page 112
Resizable Images......Page 114
Transparency Masks......Page 118
Reversible Images......Page 119
Graphics Contexts......Page 120
Drawing a UIImage......Page 122
UIImage Drawing......Page 124
CGImage Drawing......Page 126
Snapshots......Page 128
CIFilter and CIImage......Page 129
Blur and Vibrancy Views......Page 134
Drawing a UIView......Page 135
Graphics Context Settings......Page 137
Paths and Shapes......Page 139
Clipping......Page 143
Gradients......Page 144
Colors and Patterns......Page 145
Graphics Context Transforms......Page 148
Shadows......Page 149
Erasing......Page 150
Points and Pixels......Page 151
Content Mode......Page 152
Chapter 3. Layers......Page 155
View and Layer......Page 156
Layers and Sublayers......Page 157
Manipulating the Layer Hierarchy......Page 160
Positioning a Sublayer......Page 161
CAScrollLayer......Page 162
Layer and Delegate......Page 163
Drawing in a Layer......Page 164
Drawing-Related Layer Properties......Page 166
Content Resizing and Positioning......Page 167
Layers that Draw Themselves......Page 170
Affine Transforms......Page 171
3D Transforms......Page 173
Depth......Page 174
Shadows......Page 178
Masks......Page 179
Layer Efficiency......Page 181
Layers and Key–Value Coding......Page 182
Chapter 4. Animation......Page 185
Drawing, Animation, and Threading......Page 186
Image View and Image Animation......Page 188
A Brief History of View Animation......Page 190
Property Animator Basics......Page 191
View Animation Basics......Page 194
View Animation Configuration......Page 196
Timing Curves......Page 201
Canceling a View Animation......Page 205
Frozen View Animation......Page 208
Custom Animatable View Properties......Page 209
Keyframe View Animation......Page 210
Transitions......Page 212
Implicit Layer Animation......Page 215
Animation Transactions......Page 216
Media Timing Functions......Page 217
CABasicAnimation and Its Inheritance......Page 218
Using a CABasicAnimation......Page 221
Keyframe Animation......Page 224
Making a Property Animatable......Page 226
Grouped Animations......Page 227
Freezing an Animation......Page 231
Transitions......Page 232
Animations List......Page 233
What an Action Is......Page 235
Action Search......Page 236
Hooking Into the Action Search......Page 238
Making a Custom Property Implicitly Animatable......Page 241
Nonproperty Actions......Page 242
Emitter Layers......Page 243
CIFilter Transitions......Page 249
The Dynamics Stack......Page 251
Custom Behaviors......Page 255
Animator and Behaviors......Page 257
Animation and Layout......Page 265
Chapter 5. Touches......Page 269
Touch Events and Views......Page 270
Receiving Touches......Page 272
Restricting Touches......Page 274
Interpreting Touches......Page 275
Gesture Recognizer Classes......Page 279
Gesture Recognizer Conflicts......Page 285
Subclassing Gesture Recognizers......Page 287
Gesture Recognizer Delegate......Page 289
Gesture Recognizers in the Nib......Page 291
3D Touch Press Gesture......Page 292
Touch Delivery......Page 294
Hit-Testing......Page 295
Hit-Test Munging......Page 296
Hit-Testing For Layers......Page 297
Hit-Testing For Drawings......Page 298
Hit-Testing During Animation......Page 300
Gesture Recognizer and View......Page 302
Gesture Recognition Logic......Page 304
Part II. Interface......Page 307
Chapter 6. View Controllers......Page 309
View Controller Responsibilities......Page 310
View Controller Hierarchy......Page 311
Manual Child View Placement......Page 314
Ensuring a Coherent Hierarchy......Page 317
View Controller Creation......Page 319
How a View Controller Obtains Its View......Page 321
Manual View......Page 322
Generic Automatic View......Page 324
View in a Separate Nib......Page 325
How Storyboards Work......Page 328
How a View Controller Nib is Loaded......Page 329
How a View Nib is Loaded......Page 330
View Size in the Nib Editor......Page 331
Bars and Underlapping......Page 332
Resizing and Layout Events......Page 335
Rotation......Page 336
Uses of Rotation......Page 337
Permitting Compensatory Rotation......Page 338
Initial Orientation......Page 340
Initial Manual Layout......Page 341
Manual Layout During Rotation......Page 343
Presented View Controller......Page 344
Presentation and Dismissal......Page 346
Configuring a Presentation......Page 349
Communication with a Presented View Controller......Page 353
Adaptive Presentation......Page 356
Presentation, Rotation, and the Status Bar......Page 358
Tab Bar Controller......Page 359
Tab Bar Items......Page 360
Configuring a Tab Bar Controller......Page 361
Navigation Controller......Page 364
Bar Button Items......Page 366
Navigation Items and Toolbar Items......Page 367
Configuring a Navigation Controller......Page 370
Custom Transition......Page 375
Noninteractive Custom Transition Animation......Page 376
Interactive Custom Transition Animation......Page 381
Custom Presented View Controller Transition......Page 385
Transition Coordinator......Page 392
Preparing a Page View Controller......Page 393
Page View Controller Navigation......Page 396
Container View Controllers......Page 399
Adding and Removing Children......Page 400
Status Bar, Traits, and Resizing......Page 405
Peek and Pop......Page 406
Storyboards......Page 410
Triggered Segues......Page 413
Container Views and Embed Segues......Page 417
Storyboard References......Page 418
Unwind Segues......Page 419
View Controller Lifetime Events......Page 425
Order of Events......Page 427
Appear and Disappear Events......Page 428
Event Forwarding to a Child View Controller......Page 429
View Controller Memory Management......Page 430
Lazy Loading......Page 432
NSCache, NSPurgeableData, and Memory-Mapping......Page 433
State Restoration......Page 434
How to Test State Restoration......Page 435
Participating in State Restoration......Page 436
Restoration ID, Identifier Path, and Restoration Class......Page 439
Restoring View Controller State......Page 444
Restoration Order of Operations......Page 449
Restoration of Other Objects......Page 451
Content Size......Page 455
Manual Content Size......Page 456
Automatic Content Size with Autolayout......Page 457
Scroll View Layout Guides......Page 458
Using a Content View......Page 459
No Internal Autolayout......Page 463
Content Inset......Page 465
Scrolling......Page 467
Scrolling in Code......Page 469
Paging......Page 470
Tiling......Page 472
Zooming......Page 474
Zooming with Detail......Page 476
Scroll View Delegate......Page 479
Scroll View Touches......Page 482
Floating Scroll View Subviews......Page 485
Scroll View Performance......Page 486
Chapter 8. Table Views and Collection Views......Page 489
Table View Controller......Page 492
Table View Cells......Page 493
Built-In Cell Styles......Page 494
Registering a Cell Class......Page 500
Custom Cells......Page 504
Table View Data......Page 510
The Three Big Questions......Page 511
Reusing Cells......Page 513
Table View Sections......Page 515
Section Headers and Footers......Page 516
Section Index......Page 521
Refreshing a Table View......Page 523
Direct Access to Cells......Page 524
Refresh Control......Page 525
Variable Row Heights......Page 526
Manual Row Height Measurement......Page 527
Measurement and Layout with Constraints......Page 528
Estimated Height......Page 529
Automatic Row Height......Page 530
Table View Selection......Page 532
Managing Cell Selection......Page 533
Responding to Cell Selection......Page 534
Navigation from a Table View......Page 536
Cell Choice and Static Tables......Page 537
Table View State Restoration......Page 539
Configuring a Search Controller......Page 540
Using a Search Controller......Page 542
Toggling a Table View’s Edit Mode......Page 549
Changing a Table View’s Structure......Page 551
Deleting Cells......Page 552
Custom Action Buttons......Page 554
Editable Content in Cells......Page 555
Inserting Cells......Page 557
Rearranging Cells......Page 559
Collapsing and Expanding Cells......Page 560
Table View Menus......Page 563
Collection Views......Page 565
Collection View Classes......Page 568
Using a Collection View......Page 571
Deleting Cells......Page 573
Rearranging Cells......Page 574
Flow Layout Subclass......Page 575
Collection View Layout Subclass......Page 577
Decoration Views......Page 578
Switching Layouts......Page 582
Collection Views and UIKit Dynamics......Page 583
Popovers......Page 585
Arrow Source and Direction......Page 587
Popover Size......Page 588
Popover Appearance......Page 589
Passthrough Views......Page 592
Popover Presentation, Dismissal, and Delegate......Page 593
Adaptive Popovers......Page 594
Popover Segues......Page 595
Popover Presenting a View Controller......Page 596
Split Views......Page 597
Expanded Split View Controller (iPad)......Page 598
Collapsed Split View Controller (iPhone)......Page 601
Expanding Split View Controller (Big iPhone)......Page 603
Customizing a Split View Controller......Page 605
Split View Controller in a Storyboard......Page 607
Setting the Collapsed State......Page 609
View Controller Message Percolation......Page 611
iPad Multitasking......Page 614
Drag and Drop......Page 616
Drag and Drop Architecture......Page 617
Basic Drag and Drop......Page 618
Item Providers......Page 621
Additional Delegate Methods......Page 626
Table Views and Collection Views......Page 629
Spring Loading......Page 634
iPhone and Local Drag and Drop......Page 635
Chapter 10. Text......Page 637
Fonts......Page 638
Font Descriptors......Page 643
Attributed String Attributes......Page 646
Making an Attributed String......Page 649
Modifying and Querying an Attributed String......Page 654
Custom Attributes......Page 655
Drawing and Measuring an Attributed String......Page 656
Labels......Page 657
Wrapping and Truncation......Page 658
Resizing a Label to Fit Its Text......Page 659
Text Fields......Page 661
Summoning and Dismissing the Keyboard......Page 664
Keyboard Covers Text Field......Page 665
Text Field Delegate and Control Event Messages......Page 669
Text Field Menu......Page 671
Drag and Drop......Page 673
Keyboard and Input Configuration......Page 674
Text Views......Page 681
Links, Text Attachments, and Data......Page 682
Self-Sizing Text View......Page 684
Text View and Keyboard......Page 685
Text Kit......Page 686
Text Container......Page 687
Alternative Text Kit Stack Architectures......Page 690
Layout Manager......Page 692
Text Kit Without a Text View......Page 695
Chapter 11. Web Views......Page 701
WKWebView......Page 702
Web View Content......Page 704
Tracking Changes in a Web View......Page 705
Web View Navigation......Page 706
Communicating with a Web Page......Page 707
Custom Schemes......Page 711
Safari View Controller......Page 713
Developing Web View Content......Page 715
UIActivityIndicatorView......Page 717
UIProgressView......Page 719
Progress View Alternatives......Page 721
The Progress Class......Page 722
UIPickerView......Page 723
UISearchBar......Page 726
UIControl......Page 730
UISwitch......Page 734
UIStepper......Page 735
UIPageControl......Page 736
UIDatePicker......Page 737
UISlider......Page 740
UISegmentedControl......Page 743
UIButton......Page 745
Custom Controls......Page 749
Bars......Page 752
Bar Position and Bar Metrics......Page 753
Bar Appearance......Page 754
UIBarButtonItem......Page 756
UINavigationBar......Page 757
UITabBar......Page 760
Tint Color......Page 764
Appearance Proxy......Page 766
Alerts and Action Sheets......Page 769
Alerts......Page 770
Action Sheets......Page 773
Dialog Alternatives......Page 775
Quick Actions......Page 776
Local Notifications......Page 779
Authorization for Local Notifications......Page 783
Notification Categories......Page 786
Scheduling a Local Notification......Page 789
Hearing About a Local Notification......Page 792
Grouped Notifications......Page 794
Managing Notifications......Page 795
Notification Content Extensions......Page 796
Today Extensions......Page 800
Activity Views......Page 802
Presenting an Activity View......Page 804
Custom Activities......Page 806
Action Extensions......Page 809
Share Extensions......Page 813
Part III. Some Frameworks......Page 817
System Sounds......Page 819
Category......Page 821
Activation and Deactivation......Page 822
Ducking......Page 823
Interruptions......Page 824
Secondary Audio......Page 825
Routing Changes......Page 826
Audio Player......Page 827
Remote Control of Your Sound......Page 830
Playing Sound in the Background......Page 833
AVAudioEngine......Page 834
Text to Speech......Page 840
Speech to Text......Page 841
Further Topics in Sound......Page 844
Chapter 15. Video......Page 847
AVPlayerViewController......Page 848
Other AVPlayerViewController Properties......Page 850
Picture-in-Picture......Page 851
Introducing AV Foundation......Page 852
Some AV Foundation Classes......Page 853
Things Take Time......Page 854
Constructing Media......Page 857
AVPlayerLayer......Page 863
UIVideoEditorController......Page 866
Music Library Authorization......Page 869
Exploring the Music Library......Page 871
Querying the Music Library......Page 872
Music Player......Page 876
MPVolumeView......Page 881
Playing Songs with AV Foundation......Page 882
Media Picker......Page 884
Browsing with UIImagePickerController......Page 887
Image Picker Controller Presentation......Page 888
Image Picker Controller Delegate......Page 889
Dealing with Image Picker Controller Results......Page 891
Photos Framework......Page 892
Querying the Photo Library......Page 893
Modifying the Library......Page 896
Being Notified of Changes......Page 898
Fetching Images......Page 899
Editing Images......Page 903
Photo Editing Extension......Page 908
Using the Camera......Page 909
Capture with UIImagePickerController......Page 910
Capture with AV Foundation......Page 913
Contact Classes......Page 919
Fetching a Contact......Page 921
Repopulating a Contact......Page 922
Contact Formatters......Page 923
Saving Contact Information......Page 924
Contact Sorting, Groups, and Containers......Page 925
Contacts Interface......Page 926
CNContactPickerViewController......Page 927
CNContactViewController......Page 929
Chapter 19. Calendar......Page 933
Calendars......Page 934
Calendar Database Changes......Page 935
Events......Page 936
Alarms......Page 937
Recurrence......Page 938
Reminders......Page 939
Proximity Alarms......Page 940
Fetching Events and Reminders......Page 941
EKEventViewController......Page 942
EKEventEditViewController......Page 943
EKCalendarChooser......Page 944
Displaying a Map......Page 947
Annotations......Page 951
Customizing an MKMarkerAnnotationView......Page 952
Changing the Annotation View Class......Page 954
Custom Annotation View Class......Page 955
Custom Annotation Class......Page 957
Annotation View Hiding and Clustering......Page 958
Other Annotation Features......Page 960
Overlays......Page 961
Custom Overlay Class......Page 963
Custom Overlay Renderer......Page 965
Other Overlay Features......Page 967
Map Kit and Current Location......Page 968
Communicating with the Maps App......Page 970
Geocoding, Searching, and Directions......Page 971
Geocoding......Page 972
Searching......Page 973
Directions......Page 974
Chapter 21. Sensors......Page 977
Location Manager, Delegate, and Authorization......Page 978
Location Tracking......Page 983
Where Am I?......Page 986
Background Location......Page 987
Heading......Page 991
Acceleration, Attitude, and Activity......Page 993
Shake Events......Page 994
Using Core Motion......Page 995
Raw Acceleration......Page 996
Gyroscope......Page 1000
Other Core Motion Data......Page 1005
Part IV. Final Topics......Page 1011
Standard Directories......Page 1013
Inspecting the Sandbox......Page 1014
Basic File Operations......Page 1015
Saving and Reading Files......Page 1016
User Defaults......Page 1023
File Sharing......Page 1025
Document Types and Receiving a Document......Page 1026
Handing Over a Document......Page 1029
Previewing a Document......Page 1030
Quick Look Previews......Page 1031
Document Architecture......Page 1032
A Basic Document Example......Page 1034
iCloud......Page 1038
Document Browser......Page 1041
Custom Thumbnails......Page 1044
Custom Previews......Page 1046
Document Picker......Page 1048
XML......Page 1049
JSON......Page 1051
SQLite......Page 1056
Core Data......Page 1057
PDFs......Page 1064
Image Files......Page 1065
Obtaining a Session......Page 1069
Session Configuration......Page 1071
Session Tasks......Page 1072
Session Delegate......Page 1074
HTTP Request with Task Completion Function......Page 1075
HTTP Request with Session Delegate......Page 1077
One Session, One Delegate......Page 1079
Delegate Memory Management......Page 1080
Session and Delegate Encapsulation......Page 1081
Downloading Table View Data......Page 1085
Background Session......Page 1087
On-Demand Resources......Page 1090
In-App Purchases......Page 1093
Main Thread......Page 1099
Background Threads......Page 1101
Why Threading Is Hard......Page 1103
Blocking the Main Thread......Page 1104
Manual Threading......Page 1106
Operation......Page 1108
Grand Central Dispatch......Page 1113
Synchronous Execution......Page 1116
Dispatch Groups......Page 1117
One-time Execution......Page 1118
Checking the Queue......Page 1119
Threads and App Backgrounding......Page 1120
Target–Action Undo......Page 1123
Undo Grouping......Page 1126
Functional Undo......Page 1127
Shake-To-Edit......Page 1129
Undo Menu......Page 1130
Application States......Page 1133
App Delegate Events......Page 1135
Major State Changes......Page 1137
Paused Inactivity......Page 1138
Transient Inactivity on the iPad......Page 1139
Lifetime Event Timing......Page 1140
Core Graphics Initializers......Page 1143
Adjust a CGSize......Page 1144
Dictionary of Views......Page 1145
Constraint Issues......Page 1146
Configure a Value Class at Point of Use......Page 1147
Finite Repetition of an Animation......Page 1148
Remove Multiple Indexes from an Array......Page 1149
Appendix C. How Asynchronous Works......Page 1151
Index......Page 1155
About the Author......Page 1176