Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

programming ios 7, 4th edition
Nội dung xem thử
Mô tả chi tiết
www.it-ebooks.info
www.it-ebooks.info
Matt Neuburg
FOURTH EDITION
Programming iOS 7
www.it-ebooks.info
Programming iOS 7, Fourth Edition
by Matt Neuburg
Copyright © 2014 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://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: (800) 998-9938 or [email protected].
Editor: Rachel Roumeliotis
Production Editor: Kristen Brown
Proofreader: O’Reilly Production Services
Indexer: Matt Neuburg
Cover Designer: Randy Comer
Interior Designer: David Futato
Illustrator: Matt Neuburg
May 2011: First Edition
March 2012: Second Edition
March 2013: Third Edition
December 2013: Fourth Edition
Revision History for the Fourth Edition:
2013-12-09: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449372347 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Programming iOS 7, the 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 every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-37234-7
[LSI]
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Part I. Views
1. Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Window 4
Experimenting With Views 7
Subview and Superview 8
Visibility and Opacity 11
Frame 12
Bounds and Center 13
Transform 16
Layout 22
Autoresizing 23
Autolayout 27
Constraints in the Nib 38
Order of Layout Events 45
Autolayout and View Transforms 46
2. Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
UIImage and UIImageView 49
Resizable Images 53
Image Rendering Mode 56
Graphics Contexts 58
UIImage Drawing 61
CGImage Drawing 63
Snapshots 66
CIFilter and CIImage 68
Drawing a UIView 72
iii
www.it-ebooks.info
Graphics Context Settings 74
Paths and Shapes 75
Clipping 79
Gradients 80
Colors and Patterns 82
Graphics Context Transforms 84
Shadows 86
Erasing 87
Points and Pixels 88
Content Mode 89
3. Layers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
View and Layer 92
Layers and Sublayers 94
Manipulating the Layer Hierarchy 97
Positioning a Sublayer 98
CAScrollLayer 99
Layout of Sublayers 99
Drawing in a Layer 100
Content Resizing and Positioning 104
Layers that Draw Themselves 106
Transforms 108
Depth 111
Shadows, Borders, and Masks 115
Layer Efficiency 116
Layers and Key–Value Coding 118
4. Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Drawing, Animation, and Threading 122
UIImageView and UIImage Animation 125
View Animation 126
Block-Based View Animation 127
View Animation Options 129
Springing View Animation 134
Keyframe View Animation 134
Transitions 136
Implicit Layer Animation 138
Animation Transactions 140
Media Timing Functions 141
Core Animation 143
CABasicAnimation and Its Inheritance 143
Using a CABasicAnimation 145
iv | Table of Contents
www.it-ebooks.info
Keyframe Animation 149
Making a Property Animatable 150
Grouped Animations 151
Transitions 155
Animations List 157
Actions 159
What an Action Is 160
Action Search 160
Hooking Into the Action Search 162
Nonproperty Actions 164
Emitter Layers 166
CIFilter Transitions 171
UIKit Dynamics 174
Motion Effects 180
Animation and Autolayout 181
5. Touches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Touch Events and Views 186
Receiving Touches 187
Restricting Touches 189
Interpreting Touches 190
Gesture Recognizers 195
Gesture Recognizer Classes 195
Gesture Recognizer Conflicts 200
Subclassing Gesture Recognizers 203
Gesture Recognizer Delegate 205
Gesture Recognizers in the Nib 207
Touch Delivery 208
Hit-Testing 208
Initial Touch Event Delivery 214
Gesture Recognizer and View 214
Touch Exclusion Logic 216
Gesture Recognition Logic 216
Touches and the Responder Chain 218
Part II. Interface
6. View Controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
View Controller Responsibilities 224
View Controller Hierarchy 225
View Controller Creation 231
Table of Contents | v
www.it-ebooks.info
How a View Controller Gets Its View 234
Manual View 235
Generic Automatic View 237
View in a Separate Nib 238
Nib-Instantiated View Controller 241
Storyboard-Instantiated View Controller 243
View Resizing and the Status Bar 245
Rotation 251
Rotation and Layout Events 254
Initial Orientation 258
Presented View Controller 261
Presenting a View 262
Communication With a Presented View Controller 264
Presented View Animation 267
Presentation Styles 268
Rotation of a Presented View 271
Presenting a View in Response to Rotation 273
Tab Bar Controllers 274
Tab Bar Items 276
Configuring a Tab Bar Controller 277
Navigation Controllers 279
Bar Button Items 282
Navigation Items and Toolbar Items 284
Configuring a Navigation Controller 287
Custom Transition Animations 290
Noninteractive Custom Transition Animation 291
Interactive Custom Transition Animation 294
Custom Presented View Controller Transition 301
Transition Coordinator 308
Page View Controller 310
Container View Controllers 315
Storyboards 320
Segues 322
Container Views 324
Unwind Segues 325
View Controller Lifetime Events 329
Incoherencies in View Controller Events 332
Appear and Disappear Events 332
Manual Event Forwarding to a Child View Controller 333
View Controller Memory Management 335
State Restoration 337
How to Test State Restoration 338
vi | Table of Contents
www.it-ebooks.info
Participating in State Restoration 339
Restoration ID, Identifier Path, and Restoration Class 341
Restoring View Controller State 348
Restoration Order of Operations 353
Restoration of Other Objects 355
Snapshot Suppression 357
7. Scroll Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Creating a Scroll View in Code 360
Using a Content View 363
Scroll View in a Nib 365
Scrolling 367
Paging 371
Tiling 373
Zooming 375
Zooming Programmatically 376
Zooming with Detail 377
Scroll View Delegate 380
Scroll View Touches 382
Floating Scroll View Subviews 386
Scroll View Performance 387
8. Table Views and Collection Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Table View Cells 392
Built-In Cell Styles 393
Registering a Cell Class 399
Custom Cells 401
Table View Data 408
The Three Big Questions 409
Reusing Cells 411
Table View Sections 413
Section Index 418
Refreshing Table View Data 419
Variable Row Heights 423
Table View Cell Selection 426
Managing Cell Selection 426
Responding to Cell Selection 427
Navigation From a Table View 429
Cell Choice and Static Tables 430
Table View Scrolling and Layout 432
Table View State Restoration 432
Table View Searching 433
Table of Contents | vii
www.it-ebooks.info
Table View Editing 442
Deleting Table Items 445
Editable Content in Table Items 446
Inserting Table Items 448
Rearranging Table Items 450
Dynamic Table Items 451
Table View Menus 453
Collection Views 455
Collection View Classes 459
Using a Collection View 462
Custom Collection View Layouts 465
Switching Layouts 469
Collection Views and UIKit Dynamics 470
9. Popovers and Split Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Preparing a Popover 475
Popover Size 476
Popover Appearance Customization 477
Summoning and Dismissing a Popover 480
Popover Segues 480
Managing a Popover Controller 481
Dismissing a Popover 482
Popovers During Rotation or Backgrounding 485
Popovers and Presented Views 486
Automatic Popovers 487
Split Views 489
Using a Split View 489
Split Views in a Storyboard 491
Other Split View Configurations 492
10. Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Fonts 494
Fonts and Dynamic Type 494
Font Descriptors 497
Attributed Strings 499
Attributed String Attributes 500
Making an Attributed String 503
Modifying and Querying an Attributed String 508
Measuring and Drawing an Attributed String 509
Labels 511
Number of Lines 511
Wrapping and Truncation 512
viii | Table of Contents
www.it-ebooks.info
Label Resizing 513
Customized Label Drawing 514
Text Fields 515
Summoning and Dismissing the Keyboard 517
Keyboard Covers Text Field 519
Configuring the Keyboard 524
Text Field Delegate and Control Event Messages 525
Text Field Menu 528
Text Views 530
Text View as Scroll View 531
Text View and Keyboard 532
Text Kit 534
Text Container 535
Alternative Text Kit Stack Architectures 538
Layout Manager 540
Text Kit Without a Text View 543
11. Web Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Web View Content 550
Loading Web View Content 554
Designing Web View Content 556
Paginated Web Views 558
Web View State Restoration 558
Communicating with a Web View 559
12. Controls and Other Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
UIActivityIndicatorView 563
UIProgressView 565
UIPickerView 567
UISearchBar 569
UIControl 574
UISwitch 578
UIStepper 579
UIPageControl 581
UIDatePicker 581
UISlider 584
UISegmentedControl 587
UIButton 590
Custom Controls 595
Bars 598
Bar Position and Bar Metrics 599
Bar Appearance 601
Table of Contents | ix
www.it-ebooks.info
UIBarButtonItem 602
UINavigationBar 604
UIToolbar 607
UITabBar 607
Tint Color 612
Appearance Proxy 614
13. Modal Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Alert View 620
Action Sheet 622
Dialog Alternatives 627
Local Notifications 628
Activity View 632
Part III. Some Frameworks
14. Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
System Sounds 639
Audio Session 640
Interruptions 643
Routing Changes 645
Audio Player 646
Remote Control of Your Sound 648
Playing Sound in the Background 651
Further Topics in Sound 654
15. Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
MPMoviePlayerController 658
MPMoviePlayerViewController 664
UIVideoEditorController 665
Introduction to AV Foundation 668
Some AV Foundation Building Blocks 668
Displaying a Movie 669
Synchronizing Video With Animation 671
Building Media 672
Further Exploration of AV Foundation 673
16. Music Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Exploring the Music Library 675
Querying the Music Library 676
Persistence and Change in the Music Library 680
x | Table of Contents
www.it-ebooks.info
Music Player 681
MPVolumeView 684
Playing Songs With AV Foundation 686
Music Picker 688
17. Photo Library and Image Capture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Choosing From the Photo Library 693
Assets Library Framework 697
Using the Camera 701
Customizing the Image Capture Interface 703
Image Capture With AV Foundation 705
18. Address Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Address Book Database 709
Address Book Interface 714
ABPeoplePickerNavigationController 715
ABPersonViewController 717
ABNewPersonViewController 718
ABUnknownPersonViewController 719
19. Calendar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Calendar Database 721
Creating Calendars and Events 725
Recurrence 727
Fetching Events 729
Reminders 731
Proximity Alarms 732
Calendar Interface 732
EKEventViewController 733
EKEventEditViewController 735
EKCalendarChooser 736
20. Mail and Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Mail Message 739
Text Message 741
Twitter Post 742
21. Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Displaying a Map 743
Annotations 746
Custom Annotation View 747
Custom Annotation Class 751
Table of Contents | xi
www.it-ebooks.info
Other Annotation Features 752
Overlays 754
Custom Overlay Class 755
Custom Overlay Renderer 758
Other Overlay Features 760
Map Kit and Current Location 761
Communicating With the Maps App 762
Geocoding, Searching, and Directions 764
Geocoding 764
Searching 766
Directions 766
22. Sensors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Core Location 770
Basic Location Determination 771
Heading 775
Background Location 776
Acceleration and Attitude 779
Shake Events 780
Raw Acceleration 781
Gyroscope 785
Part IV. Final Topics
23. Persistent Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
The Sandbox 793
Basic File Operations 794
Saving and Reading Files 796
User Defaults 798
File Sharing 799
Document Types 800
Handing Off a Document 803
Document Architecture 806
iCloud 811
XML 814
SQLite 821
Core Data 821
Image File Formats 827
24. Basic Networking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
HTTP Requests 831
xii | Table of Contents
www.it-ebooks.info
Simple HTTP Request 833
Formal HTTP Request 834
Multiple Tasks 839
Background Downloads 845
Background App Refresh 847
In-App Purchases 848
Bonjour 852
25. Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Main Thread 855
Why Threading Is Hard 858
Three Ways of Threading 860
Manual Threads 861
NSOperation 863
Grand Central Dispatch 867
Threads and App Backgrounding 872
26. Undo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875
Undo Manager 875
Target–Action Undo 876
Undo Grouping 878
Invocation Undo 879
Undo Interface 880
Shake-To-Edit 880
Undo Menu 881
A. Application Lifetime Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
Table of Contents | xiii
www.it-ebooks.info