ebook img

Programming IOS 12: Dive Deep Into Views, View Controllers, and Frameworks PDF

1177 Pages·2018·12.56 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming IOS 12: Dive Deep Into Views, View Controllers, and Frameworks

X c o C de ove 1 r 0, s i a O n S d 1 S 2, wi f t 4. 2 Programming iOS 12 DIVE DEEP INTO VIEWS, VIEW CONTROLLERS, AND FRAMEWORKS Matt Neuburg NINTH EDITION Programming iOS 12 Matt Neuburg Boston Programming iOS 12, Ninth Edition by Matt Neuburg Copyright © 2019 Matt Neuburg. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/insti‐ tutional sales department: 800-998-9938 or [email protected]. Editor: Rachel Roumeliotis Cover Designer: Randy Comer Production Editor: Melanie Yarbrough Interior Designer: David Futato Proofreader: O’Reilly Production Services Illustrator: Matt Neuburg Indexer: Matt Neuburg May 2011: First Edition March 2012: Second Edition March 2013: Third Edition December 2013: Fourth Edition December 2014: Fifth Edition November 2015: Sixth Edition November 2016: Seventh Edition December 2017: Eighth Edition October 2018: Ninth Edition Revision History for the Ninth Edition: 2018-10-04: First release See http://oreilly.com/catalog/errata.csp?isbn=9781492044635 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Programming iOS 12, the cover image of a kingbird, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-04463-5 [LSI] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Part I. Views 1. Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Window and Root View 4 How an App Launches 4 Subclassing UIWindow 6 Referring to the Window 7 Experimenting with Views 7 Subview and Superview 8 Visibility and Opacity 11 Frame 12 Bounds and Center 13 Transform 18 The View Environment 21 Window Coordinates and Screen Coordinates 22 Trait Collections and Size Classes 24 Layout 25 Autoresizing 27 Autolayout and Constraints 29 Implicit Autoresizing Constraints 32 Creating Constraints in Code 33 Constraints as Objects 39 Margins and Guides 42 Intrinsic Content Size and Alignment Rects 47 Stack Views 49 Internationalization 52 Mistakes with Constraints 53 iii Configuring Layout in the Nib 56 Autoresizing in the Nib 57 Creating a Constraint 57 Viewing and Editing Constraints 59 Problems with Nib Constraints 61 Varying the Screen Size 63 Conditional Interface Design 64 Xcode View Features 66 View Debugger 66 Previewing Your Interface 68 Designable Views and Inspectable Properties 68 Layout Events 71 Layout-Driven UI 72 Tweaking Autolayout 74 Simulating Autolayout 74 2. Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Images and Image Views 77 Image Files 78 Image Views 82 Resizable Images 84 Transparency Masks 88 Reversible Images 89 Graphics Contexts 90 Drawing On Demand 92 Drawing a UIImage 92 UIImage Drawing 94 CGImage Drawing 96 Snapshots 98 CIFilter and CIImage 99 Blur and Vibrancy Views 104 Drawing a UIView 105 Graphics Context Commands 107 Graphics Context Settings 107 Paths and Shapes 109 Clipping 113 Gradients 114 Colors and Patterns 115 Graphics Context Transforms 118 Shadows 119 Erasing 120 Points and Pixels 121 iv | Table of Contents Content Mode 122 3. Layers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 View and Layer 126 Layers and Sublayers 127 Manipulating the Layer Hierarchy 130 Positioning a Sublayer 131 CAScrollLayer 132 Layer and Delegate 133 Layout of Layers 134 Drawing in a Layer 134 Drawing-Related Layer Properties 136 Content Resizing and Positioning 137 Layers that Draw Themselves 140 Transforms 141 Affine Transforms 141 3D Transforms 143 Depth 144 Further Layer Features 148 Shadows 148 Borders and Rounded Corners 149 Masks 149 Layer Efficiency 151 Layers and Key–Value Coding 152 4. Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Drawing, Animation, and Threading 156 Image View and Image Animation 158 View Animation 160 A Brief History of View Animation 160 Property Animator Basics 161 View Animation Basics 164 View Animation Configuration 166 Timing Curves 171 Canceling a View Animation 175 Frozen View Animation 178 Custom Animatable View Properties 179 Keyframe View Animation 180 Transitions 182 Implicit Layer Animation 185 Animation Transactions 186 Media Timing Functions 187 Table of Contents | v Core Animation 188 CABasicAnimation and Its Inheritance 188 Using a CABasicAnimation 191 Springing Animation 194 Keyframe Animation 194 Making a Property Animatable 196 Grouped Animations 197 Freezing an Animation 201 Transitions 202 Animations List 203 Actions 205 What an Action Is 205 Action Search 206 Hooking Into the Action Search 208 Making a Custom Property Implicitly Animatable 211 Nonproperty Actions 212 Emitter Layers 213 CIFilter Transitions 219 UIKit Dynamics 221 The Dynamics Stack 221 Custom Behaviors 225 Animator and Behaviors 227 Motion Effects 235 Animation and Layout 235 5. Touches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Touch Events and Views 240 Receiving Touches 242 Restricting Touches 244 Interpreting Touches 245 Gesture Recognizers 249 Gesture Recognizer Classes 249 Gesture Recognizer Conflicts 255 Subclassing Gesture Recognizers 257 Gesture Recognizer Delegate 259 Gesture Recognizers in the Nib 261 3D Touch Press Gesture 262 Touch Delivery 264 Hit-Testing 265 Performing Hit-Testing 266 Hit-Test Munging 266 Hit-Testing For Layers 267 vi | Table of Contents Hit-Testing For Drawings 268 Hit-Testing During Animation 270 Initial Touch Event Delivery 272 Gesture Recognizer and View 272 Touch Exclusion Logic 274 Gesture Recognition Logic 274 Part II. Interface 6. View Controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 View Controller Responsibilities 280 View Controller Hierarchy 281 Automatic Child View Placement 284 Manual Child View Placement 284 Presentation View Placement 287 Ensuring a Coherent Hierarchy 287 View Controller Creation 289 How a View Controller Obtains Its View 291 Manual View 292 Generic Automatic View 294 View in a Separate Nib 295 Summary 298 How Storyboards Work 298 How a View Controller Nib is Loaded 299 How a View Nib is Loaded 300 View Resizing 301 View Size in the Nib Editor 301 Bars and Underlapping 302 Resizing and Layout Events 305 Rotation 306 Detecting Rotation 307 Uses of Rotation 307 Permitting Compensatory Rotation 308 Initial Orientation 310 View Controller Manual Layout 311 Initial Manual Layout 311 Manual Layout During Rotation 313 Presented View Controller 314 Presentation and Dismissal 316 Configuring a Presentation 319 Communication with a Presented View Controller 323 Table of Contents | vii Adaptive Presentation 326 Presentation, Rotation, and the Status Bar 328 Tab Bar Controller 329 Tab Bar Items 330 Configuring a Tab Bar Controller 331 Navigation Controller 334 Bar Button Items 336 Navigation Items and Toolbar Items 337 Configuring a Navigation Controller 340 Custom Transition 345 Noninteractive Custom Transition Animation 346 Interactive Custom Transition Animation 351 Custom Presented View Controller Transition 355 Transition Coordinator 362 Page View Controller 363 Preparing a Page View Controller 363 Page View Controller Navigation 366 Other Page View Controller Configurations 369 Container View Controllers 369 Adding and Removing Children 370 Status Bar, Traits, and Resizing 375 Peek and Pop 376 Storyboards 380 Triggered Segues 383 Container Views and Embed Segues 387 Storyboard References 388 Unwind Segues 389 View Controller Lifetime Events 395 Order of Events 397 Appear and Disappear Events 398 Event Forwarding to a Child View Controller 399 View Controller Memory Management 400 Lazy Loading 402 NSCache, NSPurgeableData, and Memory-Mapping 403 Background Memory Usage 404 State Restoration 404 How to Test State Restoration 405 Participating in State Restoration 406 Restoration ID, Identifier Path, and Restoration Class 409 Restoring View Controller State 414 Restoration Order of Operations 419 Restoration of Other Objects 421 viii | Table of Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.