ebook img

iOS Recipes: Tips and Tricks for Awesome iPhone and iPad Apps PDF

193 Pages·2011·2.64 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 iOS Recipes: Tips and Tricks for Awesome iPhone and iPad Apps

Prepared exclusively for Mary Smithson Beta Book Agile publishing for agile developers Under Construction The book you’re reading is still under develop- ment. As part of our Beta book program, we’re releasing this copy well before a normal book would be released. That way you’re able to get this content a couple of months before it’s available in finished form, and we’ll get feedback to make the book even better. The idea is that everyone wins! Be warned. The book has not had a full technical edit, so it will con- tain errors. It has not been copyedited, so it will be full of typos and other weirdness. And there’s been no effort spent doing layout, so you’ll find bad page breaks, over-long lines with little black rectan- gles, incorrect hyphenations, and all the other ugly things that you wouldn’t expect to see in a finished book. We can’t be held liable if you use this book to try to create a spiffy application and you somehow end up with a strangely shaped farm implement instead. Despite all this, we think you’ll enjoy it! Download Updates Throughout this process you’ll be able to down- load updated ebooks from your account on http://pragprog.com. When the book is finally ready, you’ll get the final version (and subsequent updates) from the same address. Send us your feedback In the meantime, we’d appreciate you sending us your feedback on this book at http://pragprog.com/titles/cdirec/errata, or by using the links at the bottom of each page. Thank you for being part of the Pragmatic community! Dave & Andy Prepared exclusively for Mary Smithson iOS Recipes Tips and Tricks for Awesome iPhone and iPad Apps Matt Drance Paul Warren The Pragmatic Bookshelf Raleigh,NorthCarolina Dallas,Texas Prepared exclusively for Mary Smithson Many of the designations used by manufacturers and sellers to distinguish their prod- uctsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andThe Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The PragmaticProgrammer,PragmaticProgramming,PragmaticBookshelfandthelinkingg devicearetrademarksofThePragmaticProgrammers,LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from theuseofinformation(includingprogramlistings)containedherein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatictitles,pleasevisitusathttp://www.pragprog.com. Copyright©2011PragmaticProgrammers,LLC. Allrightsreserved. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,withoutthepriorconsentofthepublisher. PrintedintheUnitedStatesofAmerica. ISBN-10:1-934356-74-3 ISBN-13:978-1-934356-74-6 Printedonacid-freepaper. B1.0printing,March9,2011 Version:2011-3-10 Prepared exclusively for Mary Smithson Contents 1 Introduction 7 Part I—Basic UI Recipes 11 1. Add a Basic Splash Screen Transition . . . . . . . . 13 2. Stylize Your Splash Screen Transition . . . . . . . . 18 3. Animate a Custom Notification View . . . . . . . . . 24 4. Create Reusable Toggle Buttons . . . . . . . . . . . 29 5. Form Rounded Views With Textured Colors . . . . . 33 6. Put Together a Reusable Web View . . . . . . . . . . 37 7. Customize Sliders and Progress Views . . . . . . . . 41 8. Shape a Custom Gesture Recognizer . . . . . . . . . 44 9. Create Self-Contained Alert Views . . . . . . . . . . 49 10. Make a Label for Attributed Strings. . . . . . . . . . 55 11. Scroll an Infinite Wall of Album Art . . . . . . . . . . 60 12. Play Tracks from a Wall of Album Art . . . . . . . . 66 13. Have Fun with Auto-Scrolling Text Views . . . . . . 73 Part II—Table and Scroll View Recipes 77 14. Locate Table Cell Subviews . . . . . . . . . . . . . . 79 15. Simplify Table Cell Production . . . . . . . . . . . . 82 16. Use Smart Table Cells in a Nib . . . . . . . . . . . . 86 17. Organize Complex Table Views . . . . . . . . . . . . 91 18. Produce Two-Tone Table Views . . . . . . . . . . . . 97 19. Add Border Shadows for Table Views . . . . . . . . . 98 20. Stationary Content in a Zoomable Scroll View . . . 99 21. Build a Carousel Paging Scroll View . . . . . . . . . 104 Part III—Graphics Recipes 109 Prepared exclusively for Mary Smithson CONTENTS 6 22 Graphics Recipes 110 22. Draw Gradient Filled Bezier Paths . . . . . . . . . . 112 23. Create Dynamic Images With Multiple Animations . 118 24. Composite and Transform Views . . . . . . . . . . . 121 25. Animate a Gradient Layer . . . . . . . . . . . . . . . 125 26. Reshape Shadows . . . . . . . . . . . . . . . . . . . . 129 27. Display Animated Views . . . . . . . . . . . . . . . . 132 28. Construct a Simple Emitter . . . . . . . . . . . . . . 136 29. Curl the Page to a New View . . . . . . . . . . . . . . 141 Part IV—Networking Recipes 148 30. Tame the Network Activity Indicator . . . . . . . . . 150 31. Simplify Web Service Connections . . . . . . . . . . 153 32. Format a Simple HTTP POST . . . . . . . . . . . . . 157 33. Upload Files Over HTTP . . . . . . . . . . . . . . . . 162 Part V—Runtime and Debugging Recipes 170 34. Leverage Modern Objective-C Class Design . . . . . 172 35. Produce Intelligent Debug Output . . . . . . . . . . 176 36. Design Smarter User Defaults Access . . . . . . . . 180 37. Scan and Traverse View Hierarchies . . . . . . . . . 184 38. Initialize a Basic Data Model. . . . . . . . . . . . . . 185 39. Store Data in a Category . . . . . . . . . . . . . . . . 186 Index 190 Reporterratum Prepared exclusively for Mary Smithson thiscopyis(B1.0printing,March9,2011) Chapter 1 Introduction Our goal as programmers is to solve problems. Sometimes the prob- lems are hard, sometimes they’re easy, and sometimes they’re even fun. Maybe they’re not even “problems” in the colloquial sense of the word, but we are there to solve them. Ourgoal asauthors is tohelpyousolveyour problemsbetterandmore quickly than before—–preferably in that order. A recipe book is unique in that the authors choose a specific set of tasks and problems and attack them explicitly, rather than educate at a high, conceptual level. That’s not to say a recipe book shouldn’t educate. The blessing of a recipebookcomeswithtrust:youarelookingfortrustworthysolutions to problems that you don’t feel like discovering on your own. The curse ofarecipebook,ifyouallowit,isthatyoucopyandpastethesolutions without taking the time to understand them. It’s always great to save timebywritinglesscode.Butit’sjustasgreattothinkandlearnabout how you saved that time, and how you can save more of it moving forward. We developed the recipes in this book with these ideas in mind. The recipesshowyouhowtogetfromstarttofinish,ofcourse,butwehope they also encourage you to think about when and why to choose a cer- tainpath:thereareoftenmultipleoptions,especiallyinanenvironment like Cocoa. Many of these recipes were also written for maximum reusability. We weren’t just satisfied demonstrating a technique, or a snippet of code that got the job done; we wanted to build solutions that were ready for integration into projects of any kind. Some recipes were born from existing projects, and others will find their way into new ones with zero Prepared exclusively for Mary Smithson CHAPTER 1. INTRODUCTION 8 changes. We’ve learned a lot during the process of writing this book, as we do during any project. We certainly hope you’ll learn something by reading it. Cocoaisarather“opinionated”framework,andsoopinionsonapproach are both common and passionate in the Mac and iOS communities. In theinterestofconsistency,wemadesomedecisionsearlyonaboutcer- tain patterns and approaches to use in this book. The techniques you see may be familiar to you; they may be employed in a way you hadn’t considered; or you may simply have never seen them before. Regard- less, we’d like to explain some of them up front so that there are no surprises. Formatting and Syntax A number of code snippets in this book are specially formatted to fit thepage.AverboselanguagelikeObjective-Cdoesn’talwaysplaynicely withcharacterlimits,sosomeofthecodemaysometimeslookunusual. Youmayencountertersemethodorvariablenames,aseeminglyexces- sive number of temporary variables, and odd carriage returns. We’ve tried to preserve the “spirit” of Cocoa convention as much as possible, butinafewplacestheprintedpagewon.Don’tbealarmedifthecoding style suddenly changes from time to time. Categories A fair number of recipes make use of categories on standard Apple classes to accomplish tasks. Categories are an incredibly powerful fea- ture of the Objective-C programming language, and they tend to alien- ate new Cocoa programmers. They aren’t to be feared, but they are to berespected.Categoriescanquicklypollutenamespacesandcreate(or mask)unexpectedbehaviorincomplexclasshierarchies.Whenconsid- ering a category: 1. Ask yourself if a subclass, or a brand-new class, would be more appropriate.AsTheObjective-CProgrammingLanguagefromApple states: “A category is not a substitute for a subclass.” 2. Always prefix category methods when extending a class you don’t control (for example, UIApplication), to avoid symbol collisions with future APIs. Reporterratum Prepared exclusively for Mary Smithson thiscopyis(B1.0printing,March9,2011) CHAPTER 1. INTRODUCTION 9 3. Never override defined methods such as -drawRect: in a category. Doing so breaks the inheritance tree by masking the source class implementation. Synthesized Instance Variables You’llfindfew,ifany,instancevariable(“ivar”)declarationsintheheader files and examples that accompany this book. We’ve chosen to exclu- sively use Objective-C 2.0 properties, with the modern runtime’s ivar synthesis feature, for declaring class storage. The result is less typing and less reading, so we can concentrate on the recipe itself. Private Class Extensions PrivateclassextensionsareanotherrelativelynewfeatureofObjective- C,andthey’refrequentlyusedinthisbook.Privateextensionsincrease readability by minimizing header noise, and they also paint a much clearer picture for adopters or maintainers of your code. The “Lever- aging Modern Objective-C Class Design” recipe introduces both private class extensions and synthesized instance variables for those unfamil- iar with either technique. Cleanup in -dealloc In addition to releasing all relevant instance variables in the -dealloc, our examples set them to nil. This practice is one of the most hotly debated topics among Cocoa programmers, and both sides of the argu- mentholdweight.Thisbookisnotmeanttoparticipateinthedebateat all: we set them to nil, but that doesn’t mean you have to. If you don’t like nil-in-dealloc, feel free to leave it out of your own code. Blocks vs. Delegation Blocks are a new feature added to C and Objective-C in Mac OS X Snow Leopard and iOS 4.0. Due to the relative youth of this feature, the debate on when to use blocks or delegates remains heated. This book uses both at what we felt were appropriate times. You’re more than welcome to add blocks to a recipe that uses delegates, or vice versa. Our goal is ultimately to help you find the simplest and most natural solutions you can. Reporterratum Prepared exclusively for Mary Smithson thiscopyis(B1.0printing,March9,2011) CHAPTER 1. INTRODUCTION 10 Aboveall,thisbookisaboutreducingcomplexityandrepetitioninyour code. We shouldn’t be satisfied with just solving a problem as soon as possible: we want the solution readily available for the long run. We hope you agree, and that the ideas in these pages aid you in your journey as an iOS developer. Reporterratum Prepared exclusively for Mary Smithson thiscopyis(B1.0printing,March9,2011)

Description:
Take your iPhone and iPad apps to the next level. You've seen cool features and tricks in other apps, but haven't had the time to really look into how they're done. We've got the answers for you. This book walks you through clean, reusable solutions to a wide variety of problems and patterns common
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.