Beginning iPhone Development with Swift: Exploring the iOS SDK
Price: $37.58
The team that brought you the bestselling Beginning iPhone Development, the book that taught the world how to program on the iPhone, is back again for Beginning iPhone Development with Swift. This definitive guide to the Swift programming language and the iOS 8 SDK, and the source code has been updated to reflect Xcode 6.3.1 and Swift 1.2.
http://www.amazon.com/Beginning-iPhone-Development-Swift-Exploring/dp/1484204107
http://www.amazon.com/Beginning-iPhone-Development-Swift-Exploring/dp/1484204107
There’s coverage of brand-new technologies, including Swift playgrounds, as well as significant updates to existing material. You'll have everything you need to create your very own apps for the latest iOS devices. Every single sample app in the book has been rebuilt from scratch using the latest Xcode and the latest 64-bit iOS 8-specific project templates, and designed to take advantage of the latest Xcode features.
Assuming little or no working knowledge of the new Swift programming language, and written in a friendly, easy-to-follow style, this book offers a complete soup-to-nuts course in iPhone, iPad, and iPod touch programming. The book starts with the basics, walking through the process of downloading and installing Xcode and the iOS 8 SDK, and then guides you though the creation of your first simple application.
From there, you’ll learn how to integrate all the interface elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders. You’ll master a variety of design patterns, from the simplest single view to complex hierarchical drill-downs. The art of table building will be demystified, and you’ll learn how to save your data using the iPhone file system. You’ll also learn how to save and retrieve your data using a variety of persistence techniques, including Core Data and SQLite. And there’s much more!
What you’ll learn
- Everything you need to know to develop your own bestselling iPhone and iPad apps
- Utilizing Swift playgrounds
- Best practices for optimizing your code and delivering great user experiences
- What data persistence is, and why it’s important
- Get started with building cool, crisp user interfaces
- How to display data in Table Views
- How to draw to the screen using Core Graphics
- How to use iOS sensor capabilities to map your world
- How to get your app to work with iCloud and more
Who this book is for
This book is for aspiring iPhone app developers, new to the Apple Swift programming language and/or the iOS SDK.
Table of Contents
1. Welcome to the Swift Jungle
2. Appeasing the Tiki Gods
3. Handling Basic Interaction
4. More User Interface Fun
5. Rotation and Adaptive Layout
6. Multiview Applications
7. Tab Bars and Pickers
8. Introduction to Table Views
9. Navigation Controllers and Table Views
10. Collection Views
11. iPad Considerations
12. Application Settings and User Defaults
13. Basic Data Persistence
14. Hey! You! Get onto iCloud!
15. Grand Central Dispatch, Background Processing, and You
16. Core Graphics: Drawing with Quartz
17. Getting Started with Sprite Kit
18. Taps, Touches, and Gestures
19. Where Am I? Finding Your Way with Core Location and Map Kit
20. Whee! Gyro and Accelerometer!
21. The Camera and Photo Library
22. Application Localization
23. Appendix: A Swift Introduction to Swift
目錄
Contents
About the Authors .............................................................................................................. xxi
About the Technical Reviewer ......................................................................................... xxiii
■■Chapter 1: Welcome to the Swift Jungle ...........................................................................1
What This Book Is ..........................................................................................................................1
What You Need ..............................................................................................................................1
Developer Options ................................................................................................................................................. 3
What You Need to Know ........................................................................................................................................4
What’s Different About Coding for iOS? .........................................................................................5
Only One Active Application ................................................................................................................................... 5
Only One Window ...................................................................................................................................................5
Limited Access ......................................................................................................................................................5
Limited Response Time .........................................................................................................................................6
Limited Screen Size ............................................................................................................................................... 6
Limited System Resources .................................................................................................................................... 7
Some New Stuff .....................................................................................................................................................8
A Different Approach .............................................................................................................................................8
viii Contents
What’s in This Book .......................................................................................................................8
What’s New in This Update? ........................................................................................................10
Swift and Xcode Versions ............................................................................................................11
Are You Ready? ...........................................................................................................................11
■■Chapter 2: Appeasing the Tiki Gods .................................................................................13
Setting Up Your Project in Xcode .................................................................................................13
The Xcode Project Window .................................................................................................................................. 18
A Closer Look at Our Project ................................................................................................................................ 30
Introducing Xcode’s Interface Builder .........................................................................................31
File Formats .........................................................................................................................................................32
The Storyboard ....................................................................................................................................................33
The Library ..........................................................................................................................................................35
Adding a Label to the View .................................................................................................................................. 37
Changing Attributes .............................................................................................................................................42
Some iPhone Polish: Finishing Touches ......................................................................................44
The Launch Screen ......................................................................................................................48
Bring It on Home .........................................................................................................................50
■■Chapter 3: Handling Basic Interaction .............................................................................51
The Model-View-Controller Paradigm .........................................................................................52
Creating Our Project ....................................................................................................................53
Looking at the View Controller ....................................................................................................54
Understanding Outlets and Actions ..................................................................................................................... 56
Cleaning Up the View Controller .......................................................................................................................... 58
Designing the User Interface ............................................................................................................................... 58
Trying It Out ......................................................................................................................................................... 70
Previewing Layout ...............................................................................................................................................79
Adding Some style ............................................................................................................................................... 82
Looking at the Application Delegate ............................................................................................83
Bring It on Home .........................................................................................................................86
Contents ix
■■Chapter 4: More User Interface Fun ................................................................................87
A Screen Full of Controls .............................................................................................................87
Active, Static, and Passive Controls .............................................................................................91
Creating the Application ..............................................................................................................92
Implementing the Image View and Text Fields ............................................................................93
Adding the Image View ........................................................................................................................................93
Resizing the Image View .....................................................................................................................................96
Setting View Attributes ........................................................................................................................................98
Adding the Text Fields ........................................................................................................................................101
Adding Constraints ............................................................................................................................................108
Creating and Connecting Outlets ....................................................................................................................... 109
Closing the Keyboard ................................................................................................................111
Closing the Keyboard When Done Is Tapped ...................................................................................................... 113
Touching the Background to Close the Keyboard .............................................................................................. 113
Adding the Slider and Label .............................................................................................................................. 116
Adding More Constraints ...................................................................................................................................118
Creating and Connecting the Actions and Outlets ............................................................................................. 119
Implementing the Action Method ....................................................................................................................... 119
Implementing the Switches, Button, and Segmented Control ...................................................120
Adding Two Labeled Switches ........................................................................................................................... 122
Implementing the Switch Actions ...................................................................................................................... 124
Spiffing Up the Button ...............................................................................................................126
Stretchable Images ............................................................................................................................................127
Control States ....................................................................................................................................................128
Connecting and Creating the Button Outlets and Actions .................................................................................. 129
Implementing the Segmented Control Action ............................................................................129
Implementing the Action Sheet and Alert ..................................................................................131
Showing an Action Sheet ...................................................................................................................................131
Showing an Alert ...............................................................................................................................................135
Crossing the Finish Line ............................................................................................................136
x Contents
■■Chapter 5: Rotation and Adaptive Layout ......................................................................137
The Mechanics of Rotation ........................................................................................................138
Points, Pixels, and the Retina Display ................................................................................................................ 139
Handling Rotation ..............................................................................................................................................140
Choosing Your View Orientations ...............................................................................................140
Supported Orientations at the App Level ........................................................................................................... 140
Per-Controller Rotation Support ......................................................................................................................... 142
Designing an Interface Using Constraints .................................................................................144
Overriding Default Constraints .......................................................................................................................... 148
Full-Width Labels ...............................................................................................................................................150
Creating Adaptive Layouts .........................................................................................................153
The Restructure Application .............................................................................................................................. 153
Size Classes .......................................................................................................................................................158
Size Classes and Storyboards ........................................................................................................................... 161
Creating the iPhone Landscape Layout ............................................................................................................. 164
Adding the iPad Layout ......................................................................................................................................170
Rotating Out of Here ..................................................................................................................173
■■Chapter 6: Multiview Applications ................................................................................175
Common Types of Multiview Apps .............................................................................................175
The Architecture of a Multiview Application ..............................................................................180
The Root Controller ............................................................................................................................................183
Anatomy of a Content View ................................................................................................................................ 184
Building View Switcher .............................................................................................................184
Renaming the View Controller ........................................................................................................................... 185
Adding the Content View Controllers ................................................................................................................. 187
Modifying SwitchingViewController.swift .......................................................................................................... 187
Building a View with a Toolbar ........................................................................................................................... 188
Linking the Toolbar Button to the View Controller ............................................................................................. 191
Writing the Root View Controller ........................................................................................................................ 192
Contents xi
Implementing the Content Views ....................................................................................................................... 197
Animating the Transition ....................................................................................................................................200
Switching Off .............................................................................................................................203
■■Chapter 7: Tab Bars and Pickers ...................................................................................205
The Pickers Application .............................................................................................................206
Delegates and Data Sources .....................................................................................................211
Creating the Pickers Application ...............................................................................................212
Creating the View Controllers ............................................................................................................................ 212
Creating the Tab Bar Controller .......................................................................................................................... 213
The Initial Test Run ............................................................................................................................................217
Implementing the Date Picker ...................................................................................................218
Implementing the Single-Component Picker .............................................................................221
Building the View ...............................................................................................................................................221
Implementing the Controller As a Data Source and Delegate ............................................................................ 223
Implementing a Multicomponent Picker ...................................................................................226
Declaring Outlets and Actions ........................................................................................................................... 226
Building the View ............................................................................................................................................... 227
Implementing the Controller .............................................................................................................................. 227
Implementing Dependent Components .....................................................................................230
Creating a Simple Game with a Custom Picker .........................................................................237
Preparing the View Controller ............................................................................................................................ 237
Building the View ...............................................................................................................................................237
Implementing the Controller .............................................................................................................................. 239
Final Details .......................................................................................................................................................242
Final Spin ..................................................................................................................................246
■■Chapter 8: Introduction to Table Views .........................................................................247
Table View Basics ......................................................................................................................248
Table Views and Table View Cells ...................................................................................................................... 248
Grouped and Plain Tables .................................................................................................................................. 250
xii Contents
Implementing a Simple Table ....................................................................................................251
Designing the View ............................................................................................................................................251
Writing the Controller ........................................................................................................................................253
Adding an Image ................................................................................................................................................258
Using Table View Cell Styles .............................................................................................................................. 259
Setting the Indent Level .....................................................................................................................................262
Handling Row Selection .....................................................................................................................................263
Changing the Font Size and Row Height ........................................................................................................... 266
Customizing Table View Cells ....................................................................................................269
Adding Subviews to the Table View Cell ........................................................................................................... 269
Creating a UITableViewCell Subclass ................................................................................................................. 270
Loading a UITableViewCell from a Nib ............................................................................................................... 274
Grouped and Indexed Sections ..................................................................................................280
Building the View ...............................................................................................................................................280
Importing the Data .............................................................................................................................................280
Implementing the Controller .............................................................................................................................. 281
Adding an Index ................................................................................................................................................. 285
Implementing a Search Bar .......................................................................................................287
How Many Tables?: View Debugging .........................................................................................296
Putting It All on the Table ...........................................................................................................299
■■Chapter 9: Navigation Controllers and Table Views .......................................................301
Navigation Controller Basics .....................................................................................................302
Stacky Goodness ............................................................................................................................................... 302
A Stack of Controllers ........................................................................................................................................302
Fonts: A Simple Font Browser ...................................................................................................304
Meet the Subcontrollers ....................................................................................................................................305
The Fonts Application’s Skeleton ....................................................................................................................... 307
Creating the Root View Controller .............................................................................................311
Initial Storyboard Setup .............................................................................................................314
Contents xiii
First Subcontroller: The Font List View ......................................................................................316
Storyboarding the Font List ............................................................................................................................... 318
Making the Root View Controller Prepare for Segues ........................................................................................ 319
Creating the Font Sizes View Controller ....................................................................................320
Storyboarding the Font Sizes View Controller ................................................................................................... 322
Making the Font List View Controller Prepare for Segues ................................................................................. 322
Creating the Font Info View Controller .......................................................................................323
Storyboarding the Font Info View Controller ...................................................................................................... 324
Setting Up Constraints .......................................................................................................................................326
Adapting the Font List View Controller for Multiple Segues .............................................................................. 326
My Favorite Fonts ..............................................................................................................................................327
Table View Niceties ...................................................................................................................328
Implementing Swipe-to-Delete ......................................................................................................................... 329
Implementing Drag-to-Reorder ......................................................................................................................... 331
Breaking the Tape ......................................................................................................................333
■■Chapter 10: Collection View ..........................................................................................335
Creating the DialogViewer Project .............................................................................................335
Fixing the View Controller’s Class .............................................................................................337
Defining Custom Cells ...............................................................................................................337
Configuring the View Controller .................................................................................................341
Providing Content Cells .............................................................................................................342
Making the Layout Flow ............................................................................................................343
Providing Header Views .............................................................................................................346
■■Chapter 11: Using Split Views and Popovers .................................................................349
Building Master-Detail Applications with UISplitViewController ................................................352
The Storyboard Defines the Structure ............................................................................................................... 354
The Code Defines the Functionality ................................................................................................................... 356
How the Master-Detail Template Application Works .......................................................................................... 361
Here Come the Presidents .........................................................................................................364
Creating Your Own Popover .......................................................................................................371
xiv Contents
Split Views on the iPhone ..........................................................................................................377
Split Views on the iPhone 6 Plus ....................................................................................................................... 380
Getting the iPhone 6 Plus Behavior on All iPhones ............................................................................................ 381
Customizing the Split View ........................................................................................................384
Time to Wrap Up and Split .........................................................................................................386
■■Chapter 12: Application Settings and User Defaults ......................................................387
Getting to Know Your Settings Bundle .......................................................................................387
The Bridge Control Application ..................................................................................................389
Creating the Project ...........................................................................................................................................394
Working with the Settings Bundle ..................................................................................................................... 395
Reading Settings in Our Application .................................................................................................................. 413
Changing Defaults from Our Application ........................................................................................................... 417
Registering Default Values ................................................................................................................................. 420
Keeping It Real ..................................................................................................................................................421
Switching to the Settings Application .......................................................................................424
Beam Me Up, Scotty ..................................................................................................................424
■■Chapter 13: Basic Data Persistence ..............................................................................425
Your Application’s Sandbox .......................................................................................................426
Getting the Documents and Library Directories ................................................................................................. 428
Getting the tmp Directory .................................................................................................................................. 429
File-Saving Strategies ...............................................................................................................429
Single-File Persistence ......................................................................................................................................430
Multiple-File Persistence ...................................................................................................................................430
Using Property Lists ..................................................................................................................430
Property List Serialization .................................................................................................................................. 431
The First Version of the Persistence Application ............................................................................................... 432
Archiving Model Objects ............................................................................................................439
Conforming to NSCoding ...................................................................................................................................439
Implementing NSCopying .................................................................................................................................. 441
Archiving and Unarchiving Data Objects ........................................................................................................... 442
The Archiving Application .................................................................................................................................. 443
Contents xv
Using iOS’s Embedded SQLite3 .................................................................................................446
Creating or Opening the Database ..................................................................................................................... 447
Using Bind Variables ..........................................................................................................................................448
The SQLite3 Application .....................................................................................................................................450
Using Core Data .........................................................................................................................456
Entities and Managed Objects ........................................................................................................................... 458
The Core Data Application ................................................................................................................................. 462
Persistence Rewarded ..............................................................................................................472
■■Chapter 14: Documents and iCloud ...............................................................................473
Managing Document Storage with UIDocument .......................................................................474
Building TinyPix .................................................................................................................................................474
Creating TinyPixDocument ................................................................................................................................. 475
Code Master ......................................................................................................................................................478
Initial Storyboarding ..........................................................................................................................................485
Creating TinyPixView .........................................................................................................................................489
Storyboard Detailing ..........................................................................................................................................493
Adding iCloud Support ...............................................................................................................497
Creating a Provisioning Profile .......................................................................................................................... 498
How to Query .....................................................................................................................................................500
Save Where? ...................................................................................................................................................... 502
Storing Preferences on iCloud ........................................................................................................................... 503
What We Didn’t Cover ................................................................................................................505
■■Chapter 15: Grand Central Dispatch, Background Processing, and You ........................507
Grand Central Dispatch .............................................................................................................507
Introducing SlowWorker ...........................................................................................................508
Threading Basics .......................................................................................................................511
Units of Work .............................................................................................................................512
GCD: Low-Level Queuing ...........................................................................................................512
Improving SlowWorker ......................................................................................................................................514
xvi Contents
Background Processing ............................................................................................................520
Application Life Cycle ........................................................................................................................................521
State-Change Notifications ................................................................................................................................ 522
Creating State Lab .............................................................................................................................................523
Exploring Execution States ................................................................................................................................ 524
Using Execution State Changes ......................................................................................................................... 526
Handling the Inactive State ............................................................................................................................... 527
Handling the Background State ......................................................................................................................... 531
Grand Central Dispatch, Over and Out .......................................................................................539
■■Chapter 16: Drawing with Core Graphics ......................................................................541
Paint the World ..........................................................................................................................541
The Quartz 2D Approach to Drawing .........................................................................................542
Quartz 2D’s Graphics Contexts .......................................................................................................................... 542
The Coordinate System .....................................................................................................................................543
Specifying Colors ...............................................................................................................................................545
Drawing Images in Context ................................................................................................................................ 547
Drawing Shapes: Polygons, Lines, and Curves .................................................................................................. 547
Quartz 2D Tool Sampler: Patterns, Gradients, and Dash Patterns ...................................................................... 548
The QuartzFun Application ........................................................................................................549
Setting Up the QuartzFun Application ................................................................................................................ 549
Adding Quartz 2D Drawing Code ....................................................................................................................... 559
Optimizing the QuartzFun Application ............................................................................................................... 564
Drawing to a Close ....................................................................................................................567
■■Chapter 17: Getting Started with Sprite Kit ...................................................................569
Simple Beginnings ....................................................................................................................570
Initial Scene Customization ............................................................................................................................... 576
Player Movement ......................................................................................................................580
Adding the Player to the Scene ......................................................................................................................... 582
Handling Touches: Player Movement ................................................................................................................. 583
Geometry Calculations .......................................................................................................................................584
Wobbly Bits ........................................................................................................................................................585
Contents xvii
Creating Your Enemies ..............................................................................................................586
Putting Enemies in the Scene ...................................................................................................587
Start Shooting ...........................................................................................................................588
Defining Your Physics Categories ...................................................................................................................... 589
Creating the BulletNode Class ........................................................................................................................... 589
Applying Physics ................................................................................................................................................ 590
Adding Bullets to the Scene .............................................................................................................................. 591
Attacking Enemies with Physics ...............................................................................................593
Finishing Levels .........................................................................................................................594
Keeping Tabs on the Enemies ............................................................................................................................ 595
Transitioning to the Next Levels ........................................................................................................................ 596
Customizing Collisions ..............................................................................................................597
Adding a Class Extension to SKNode ................................................................................................................. 599
Adding Custom Collision Behavior to Enemies .................................................................................................. 600
Showing Accurate Player Lives ......................................................................................................................... 601
Spicing Things Up with Particles ...............................................................................................602
Your First Particle ..............................................................................................................................................603
Putting Particles into the Scene ........................................................................................................................ 605
The End Game ...........................................................................................................................607
At Last, a Beginning: Create a StartScene .................................................................................609
A Sound Is Worth a Thousand Pictures ......................................................................................612
Making the Game a Little Harder: Force Fields .........................................................................613
Game On ....................................................................................................................................616
■■Chapter 18: Taps, Touches, and Gestures ......................................................................617
Multitouch Terminology .............................................................................................................617
The Responder Chain ................................................................................................................618
Responding to Events ........................................................................................................................................618
Forwarding an Event: Keeping the Responder Chain Alive ................................................................................ 620
xviii Contents
The Multitouch Architecture ......................................................................................................620
The Four Touch Notification Methods ........................................................................................621
The TouchExplorer Application ..................................................................................................622
The Swipes Application .............................................................................................................626
Using Touch Events to Detect Swipes ................................................................................................................ 627
Automatic Gesture Recognition ......................................................................................................................... 630
Implementing Multiple Swipes .......................................................................................................................... 631
Detecting Multiple Taps .............................................................................................................633
Detecting Pinch and Rotation ....................................................................................................638
Defining Custom Gestures .........................................................................................................642
The CheckPlease Application ............................................................................................................................. 643
The CheckPlease Touch Methods ...................................................................................................................... 645
Garçon? Check, Please! .............................................................................................................647
■■Chapter 19: Where Am I? Finding Your Way with Core Location and Map Kit ...............649
The Location Manager ...............................................................................................................650
Setting the Desired Accuracy ............................................................................................................................ 650
Setting the Distance Filter ................................................................................................................................. 651
Getting Permission to Use Location Services .................................................................................................... 651
Starting the Location Manager .......................................................................................................................... 651
Using the Location Manager Wisely ................................................................................................................... 652
The Location Manager Delegate ................................................................................................652
Getting Location Updates ..................................................................................................................................652
Getting Latitude and Longitude Using CLLocation ............................................................................................. 652
Error Notifications ..............................................................................................................................................654
Trying Out Core Location ...........................................................................................................655
Using Location Manager Updates ...................................................................................................................... 661
Visualizing Your Movement on a Map ........................................................................................663
Changing Location Service Permissions ........................................................................................................... 667
Wherever You Go, There You Are ................................................................................................668
Contents xix
■■Chapter 20: Whee! Gyro and Accelerometer! ...............................................................671
Accelerometer Physics ..............................................................................................................671
Don’t Forget Rotation ................................................................................................................673
Core Motion and the Motion Manager .......................................................................................673
Event-Based Motion ..........................................................................................................................................674
Proactive Motion Access ...................................................................................................................................678
Gyroscope and Attitude Results ......................................................................................................................... 681
Accelerometer Results ......................................................................................................................................681
Detecting Shakes ......................................................................................................................683
Baked-In Shaking ..............................................................................................................................................683
Shake and Break ...............................................................................................................................................684
Accelerometer As Directional Controller ...................................................................................687
Rolling Marbles ..................................................................................................................................................687
Writing the Ball View .........................................................................................................................................690
Calculating Ball Movement ................................................................................................................................ 692
Rolling On ..................................................................................................................................695
■■Chapter 21: The Camera and Photo Library ...................................................................697
Using the Image Picker and UIImagePickerController ...............................................................697
Using the Image Picker Controller .............................................................................................697
Implementing the Image Picker Controller Delegate ......................................................................................... 700
Road Testing the Camera and Library .......................................................................................701
Designing the Interface .....................................................................................................................................702
Implementing the Camera View Controller ........................................................................................................ 705
It’s a Snap! ................................................................................................................................709
■■Chapter 22: Application Localization .............................................................................711
Localization Architecture ...........................................................................................................711
Strings Files ..............................................................................................................................713
What’s in a Strings File? ....................................................................................................................................713
The Localized String Function ........................................................................................................................... 714
xx Contents
Real-World iOS: Localizing Your Application ..............................................................................715
Setting Up LocalizeMe .......................................................................................................................................716
Trying Out LocalizeMe .......................................................................................................................................719
Localizing the Project ........................................................................................................................................720
Localizing the Storyboard .................................................................................................................................. 724
Generating and Localizing a Strings File ........................................................................................................... 726
Previewing Localizations in Xcode ............................................................................................731
Localizing the App Display Name ...................................................................................................................... 733
Adding Another Localization ......................................................................................................734
Auf Wiedersehen .......................................................................................................................734
■■Appendix: A Swift Introduction to Swift ........................................................................737
Index .................................................................................................................................789
沒有留言:
張貼留言
歡迎網友的交流與分享,謝謝。