HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
The Core iOS 6 Developer’s Cookbook brings together reliable, proven solutions for the heart of day-to-day iOS 6 development. World-renowned iOS programming expert Erica Sadun covers all the classes you’ll need to create successful iOS 6 mobile apps with standard APIs and interface elements and take full advantage of iOS 6 graphics, touches, and views.
As in her previous bestselling iOS books, Sadun translates today’s development best practices into working code, distilling key concepts into concise recipes that are easy to understand and transfer into your own projects. This isn’t just cut-and-paste; using her examples, Sadun fully explains both the “how” and “why” of effective iOS 6 development.
All code has been fully revised and extensively tested to reflect the latest iOS 6 features and the newest iPhone, iPad, and iPod touch capabilities. Throughout, every chapter groups related tasks together, so you can jump straight to your solution, without having to identify the right class or framework first. Coverage includes
To download the code in this book, please visit github.com/erica/iOS-6-Cookbook.
Core iOS 6 Developer's Cookbook: Gestures and Touches
Download the sample pages (includes Chapter 1 and Index)
Preface xxii
1 Gestures and Touches 1
Touches 1
Phases 2
Touches and Responder Methods 3
Touching Views 4
Multi-Touch 4
Gesture Recognizers 5
Recipe: Adding a Simple Direct Manipulation Interface 5
Recipe: Adding Pan Gesture Recognizers 7
Recipe: Using Multiple Gesture Recognizers Simultaneously 9
Resolving Gesture Conflicts 12
Recipe: Constraining Movement 13
Recipe: Testing Touches 15
Recipe: Testing Against a Bitmap 17
Recipe: Drawing Touches Onscreen 19
Recipe: Smoothing Drawings 21
Recipe: Using Multi-Touch Interaction 24
Recipe: Detecting Circles 28
Recipe: Creating a Custom Gesture Recognizer 33
Recipe: Dragging from a Scroll View 35
Recipe: Live Touch Feedback 39
Enabling Touch Feedback 39
Intercepting and Forwarding Touch Events 40
Implementing the TOUCHkit Overlay View 41
Recipe: Adding Menus to Views 44
Summary 46
2 Building and Using Controls 49
The UIControl Class 49
Target-Action 50
Kinds of Controls 50
Control Events 51
Buttons 53
Buttons in Interface Builder 54
Connecting Buttons to Actions 55
Recipe: Building Buttons 55
Multiline Button Text 58
Adding Animated Elements to Buttons 58
Adding Extra State to Buttons 59
Recipe: Animating Button Responses 59
Recipe: Adding a Slider with a Custom Thumb 61
Customizing UISlider 62
Adding Efficiency 63
Appearance Proxies 66
Recipe: Creating a Twice-Tappable Segmented Control 69
Second-Tap Feedback 70
Controls and Attributes 70
Working with Switches and Steppers 72
Recipe: Subclassing UIControl 73
Creating Controls 74
Tracking Touches 74
Dispatching Events 74
Recipe: Building a Star Slider 77
Recipe: Building a Touch Wheel 80
Recipe: Creating a Pull Control 83
Discoverability 83
Testing Touches 85
Recipe: Building a Custom Lock Control 87
Adding a Page Indicator Control 90
Recipe: Creating a Customizable Paged Scroller 93
Building Toolbars 98
Summary 100
3 Alerting the User 101
Talking Directly to Your User Through Alerts 101
Building Simple Alerts 102
Alert Delegates 103
Displaying the Alert 104
Kinds of Alerts 104
“Please Wait”: Showing Progress to Your User 105
Using UIActivityIndicatorView 106
Using UIProgressView 107
Recipe: No-Button Alerts 107
Building a Floating Progress Monitor 109
Recipe: Creating Modal Alerts with Run Loops 110
Recipe: Using Variadic Arguments with Alert Views 113
Presenting Lists of Options 114
Scrolling Menus 117
Displaying Text in Action Sheets 117
Recipe: Building Custom Overlays 117
Tappable Overlays 119
Recipe: Basic Popovers 119
Recipe: Local Notifications 121
Best Practices 122
Alert Indicators 123
Badging Applications 124
Recipe: Simple Audio Alerts 124
System Sounds 124
Vibration 125
Alerts 126
Delays 126
Disposing of System Sounds 126
Summary 128
4 Assembling Views and Animations 129
View Hierarchies 129
Recipe: Recovering a View Hierarchy Tree 131
Exploring XIB and Storyboard Views 132
Recipe: Querying Subviews 133
Managing Subviews 134
Adding Subviews 134
Reordering and Removing Subviews 135
View Callbacks 135
Tagging and Retrieving Views 136
Using Tags to Find Views 136
Recipe: Naming Views by Object Association 137
Naming Views in Interface Builder 137
View Geometry 139
Frames 140
Rectangle Utility Functions 140
Points and Sizes 141
Transforms 142
Coordinate Systems 142
Recipe: Working with View Frames 143
Adjusting Sizes 144
CGRects and Centers 146
Other Geometric Elements 147
Recipe: Retrieving Transform Information 151
Retrieving Transform Properties 151
Testing for View Intersection 152
Display and Interaction Traits 157
UIView Animations 158
Building Animations with Blocks 159
Recipe: Fading a View In and Out 159
Recipe: Swapping Views 161
Recipe: Flipping Views 162
Recipe: Using Core Animation Transitions 163
Recipe: Bouncing Views as They Appear 165
Recipe: Image View Animations 166
Summary 167
5 View Constraints 169
What Are Constraints? 169
Alignment Rectangles 170
Declaring Alignment Rectangles 171
Constraint Attributes 171
Constraint Math 172
The Laws of Constraints 173
Creating Constraints 175
Basic Constraint Declarations 175
Visual Format Constraints 176
Variable Bindings 177
Format Strings 177
Orientation 177
View Names 179
Connections 179
Predicates 183
Metrics 183
View-to-View Predicates 184
Priorities 184
Format String Summary 184
Storing and Updating Constraints 186
Recipe: Comparing Constraints 187
Recipe: Describing Constraints 189
Recipe: Creating Fixed-Size Constrained Views 192
Disabling Autosizing Constraints 192
Starting within View Bounds 193
Constraining Size 193
Recipe: Centering Views 196
Recipe: Setting Aspect Ratio 197
Aligning Views and Flexible Sizing 199
Why You Cannot Distribute Views 199
Recipe: Responding to Orientation Changes 200
Constraint Macros 202
Consistent Constraints 202
Sufficient Constraints 203
Macros 203
Debugging Your Constraints 205
Summary 207
6 Text Entry 209
Recipe: Dismissing a UITextField Keyboard 209
Preventing Keyboard Dismissal 210
Text Trait Properties 211
Other Text Field Properties 212
Recipe: Dismissing Text Views with Custom Accessory Views 213
Recipe: Adjusting Views Around Keyboards 216
Recipe: Adjusting Views Around Accessory Views 220
Testing for Hardware Keyboards 221
Recipe: Creating a Custom Input View 223
Recipe: Making Text-Input-Aware Views 227
Recipe: Adding Custom Input Views to Nontext Views 230
Adding Input Clicks 231
Recipe: Building a Better Text Editor (Part I) 233
Recipe: Building a Better Text Editor (Part II) 236
Enabling Attributed Text 236
Controlling Attributes 236
Other Responder Functionality 237
Recipe: Text-Entry Filtering 239
Recipe: Detecting Text Patterns 242
Rolling Your Own Expressions 242
Enumerating Regular Expressions 243
Data Detectors 243
Using Built-In Type Detectors 244
Useful Websites 244
Recipe: Detecting Misspelling in a UITextView 246
Searching for Text Strings 247
Summary 248
7 Working with View Controllers 249
View Controllers 249
The UIViewController Class 250
Navigation Controllers 250
Tab Bar Controllers 251
Split View Controllers 251
Page View Controller 251
Popover Controllers 251
Developing with Navigation Controllers and Split Views 252
Using Navigation Controllers and Stacks 253
Pushing and Popping View Controllers 254
Bar Buttons 254
Recipe: The Navigation Item Class 255
Titles and Back Buttons 255
Macros 256
Recipe: Modal Presentation 257
Presenting a Custom Modal Information View 258
Recipe: Building Split View Controllers 261
Recipe: Creating Universal Split View/Navigation Apps 266
Recipe: Tab Bars 268
Remembering Tab State 272
Recipe: Page View Controllers 275
Book Properties 276
Wrapping the Implemen tation 277
Exploring the Recipe 279
Building a Presentation Index 279
Recipe: Scrubbing Pages in a Page View Controller 285
Recipe: Custom Containers 286
Adding and Removing a Child View Controller 287
Transitioning Between View Controllers 288
Recipe: Segues 292
Segues, Interface Builder, and iOS 6 297
Summary 298
8 Common Controllers 299
Image Picker Controller 299
Image Sources 299
Presenting the Picker on iPhone and iPad 300
Recipe: Selecting Images 300
How To: Adding Photos to the Simulator 301
The Assets Library Framework 301
Presenting a Picker 302
Handling Delegate Callbacks 303
Recipe: Snapping Photos 306
Setting Up the Picker 307
Displaying Images 308
Saving Images to the Photo Album 309
Recipe: Recording Video 310
Creating the Video Recording Picker 311
Saving the Video 311
Recipe: Playing Video with Media Player 313
Recipe: Editing Video 316
AV Foundation and Core Media 316
Recipe: Picking and Editing Video 318
Recipe: E-Mailing Pictures 321
Creating Message Contents 321
Recipe: Sending a Text Message 323
Recipe: Posting Social Updates 325
Recipe: Activity View Controller 328
Creating and Presenting the Controller 328
Adding Services 331
I tems and Services 335
Recipe: The Quick Look Preview Controller 336
Implementing Quick Look 337
Summary 340
9 Accessibility 341
Accessibility 101 341
Accessibility in Interface Builder 342
Enabling Accessibility 343
Traits 344
Labels 345
Hints 346
Testing with the Simulator 347
Broadcasting Updates 348
Testing Accessibility on the iPhone 349
Summary 351
10 Creating and Managing Table Views 353
iOS Tables 353
Delegation 354
Creating Tables 355
Table Styles 355
Laying Out the View 355
Assigning a Data Source 356
Serving Cells 356
Registering Cell Classes 356
Dequeueing Cells 357
Assigning a Delegate 357
Recipe: Implementing a Basic Table 358
Data Source Methods 359
Responding to User Touches 359
Table View Cells 361
Selection Color 362
Adding in Custom Selection Traits 363
Recipe: Creating Checked Table Cells 363
Working with Disclosure Accessories 365
Recipe: Table Edits 367
Adding Undo Support 368
Supporting Undo 369
Displaying Remove Controls 369
Handling Delete Requests 369
Swiping Cells 370
Reordering Cells 370
Adding Cells 370
Recipe: Working with Sections 375
Building Sections 375
Counting Sections and Rows 376
Returning Cells 377
Creating Header Titles 378
Customizing Headers and Footers 379
Creating a Section Index 379
Handling Section Mismatches 380
Delegation with Sections 380
Recipe: Searching Through a Table 382
Creating a Search Display Controller 383
Registering Cells for the Search Display Controller 384
Building the Searchable Data Source Methods 384
Delegate Methods 385
Using a Search-Aware Index 386
Recipe: Adding Pull-to-Refresh to Your Table 388
Recipe: Adding Action Rows 391
Coding a Custom Group Table 394
Creating Grouped Preferences Tables 395
Recipe: Building a Multiwheel Table 396
Creating the UIPickerView 397
Data Source and Delegate Methods 397
Using Views with Pickers 397
Using the UIDatePicker 399
Creating the Date Picker 400
Summary 401
11 Collection Views 403
Collection Views Versus Tables 403
Practical Implementation Differences 405
Establishing Collection Views 405
Controllers 405
Views 406
Data Sources and Delegates 406
Flow Layouts 407
Scroll Direction 407
Item Size and Line Spacing 407
Header and Footer Sizing 409
Insets 410
Recipe: Basic Collection View Flows 411
Recipe: Custom Cells 415
Recipe: Scrolling Horizontal Lists 416
Recipe: Introducing Interactive Layout Effects 420
Recipe: Scroll Snapping 422
Recipe: Creating a Circle Layout 423
Creation and Deletion Animation 424
Powering the Circle Layout 424
The Layout 425
Recipe: Adding Gestures to Layout 429
Recipe: Creating a True Grid Layout 431
Recipe: Custom Item Menus 437
Summary 439
12 A Taste of Core Data 441
Introducing Core Data 441
Entities and Models 442
Building a Model File 442
Attributes and Relationships 443
Building Object Classes 444
Creating Contexts 444
Adding Data 445
Examining the Data File 446
Querying the Database 448
Setting Up the Fetch Request 449
Performing the Fetch 449
Removing Objects 450
Recipe: Using Core Data for a Table Data Source 451
Index Path Access 451
Section Key Path 451
Section Groups 452
Index Titles 452
Table Readiness 452
Recipe: Search Tables and Core Data 455
Recipe: Adding Edits to Core Data Table Views 457
Adding Undo/Redo Support 458
Creating Undo Transactions 459
Rethinking Edits 459
Recipe: A Core Data-Powered Collection View 464
Summary 469
13 Networking Basics 471
Recipe: Checking Your Network Status 471
Scanning for Connectivity Changes 474
Recipe: Synchronous Downloads 476
Recipe: Asynchronous Downloads 480
One-Call No-Feedback Asynchronous Downloads 486
Recipe: Using JSON Serialization 487
Recipe: Converting XML into Trees 489
Trees 489
Building a Parse Tree 490
Summary 492
Appendix Objective-C Literals 495
Numbers 495
Boxing 496
Enums 496
Container Literals 497
Subscripting 498
Feature Tests 499
Index 501