1. Trang chủ
  2. » Công Nghệ Thông Tin

SwiftUI For Dummies by Wei Meng Lee

419 28 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 419
Dung lượng 26,08 MB

Nội dung

Have a unique app idea but worried you don’t quite have the coding skills to build it? Good news: You can stop fretting about someone beating you to market with the same idea and start work right now using SwiftUI. SwiftUI is a gateway app development framework that has become one of the best ways for fledgling developers to get iOS apps off the ground without having to become a coding expert overnight. SwiftUI For Dummies makes that process even faster, providing a friendly introduction to the SwiftUI and Swift programming language and helping you feel right at home creating and building with playgrounds. The book also covers the frameworks and APIs that make it so easy to create smooth, intuitive interfaces—just dive right in and have fun Combine projects into workspaces Employ Xcode editing tools Use constants and variables Test your code on iOS Simulator Time is of the essence, and with SwiftUI For Dummies, it’s also on your side. Get going with this friendly guide today, and you’ll be celebrating the successful launch of your app way before you thought possible

SwiftUI ™ by Wei-Meng Lee SwiftUI™ For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2020 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and may not be used without written permission SwiftUI is a trademark of Apple, Inc All other trademarks are the property of their respective owners John Wiley & Sons, Inc is not associated with any product or vendor mentioned in this book SwiftUI™ For Dummies® is an independent publication and has not been authorized, sponsored, or otherwise approved by Apple, Inc LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.  NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT.  NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM.  THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ For general information on our other products and services, please contact our Customer Care Department within the U.S at 877-762-2974, outside the U.S at 317-572-3993, or fax 317-572-4002 For technical support, please visit https://hub.wiley.com/community/support/dummies Wiley publishes in a variety of print and electronic formats and by print-on-demand Some material included with standard print versions of this book may not be included in e-books or in print-on-demand If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com For more information about Wiley products, visit www.wiley.com Library of Congress Control Number: 2020939546 ISBN 978-1-119-65268-7 (pbk); ISBN 978-1-119-65272-4 (ebk); ISBN 978-1-119-65270-0 (ebk) Manufactured in the United States of America 10 Contents at a Glance Introduction Part 1: Getting Started with Swift and SwiftUI CHAPTER 1: Introducing SwiftUI CHAPTER 2: Basics of the Swift Programming Language 25 Part 2: Understanding the Basics of SwiftUI 61 CHAPTER 3: CHAPTER 4: CHAPTER 5: CHAPTER 6: CHAPTER 7: Getting Started with the Basics of SwiftUI 63 Handling User Inputs and Custom Views 87 Displaying Lists of Items 113 Creating Navigation and Tabbed Applications 143 Formatting Your User Interface 173 Part 3: Exploring with SwiftUI in More Detail 205 CHAPTER 8: Understanding State Management in SwiftUI 207 CHAPTER 9: Using Legacy UIKit Views and View Controllers in SwiftUI 235 Part 4: Performing Animations and Drawings 263 CHAPTER 10: Drawings and Special Effects 265 Animations in SwiftUI 299 CHAPTER 12: Creating a Complete Project 323 CHAPTER 11: Performing Part 5: The Part of Tens 351 CHAPTER 13: Ten SwiftUI Tips and Tricks 353 CHAPTER 14: Ten Great SwiftUI Resources 375 Appendix: Code Snippets for Common SwiftUI Views 379 Index 387 Table of Contents INTRODUCTION About This Book Foolish Assumptions Icons Used in This Book Beyond the Book Where to Go from Here 2 3 PART 1: GETTING STARTED WITH SWIFT AND SWIFTUI CHAPTER 1: Introducing SwiftUI Understanding What SwiftUI Is Getting the Tools 11 Hello, SwiftUI 12 Automatically previewing your user interface using the canvas 14 Working with Live Preview 18 Generating different previews 19 The Gory Details 21 Info.plist 22 AppDelegate.swift 22 SceneDelegate.swift 23 CHAPTER 2: Basics of the Swift Programming Language 25 Basic Swift Syntax Constants Variables Strings Comments Basic Data Types Integers Floating-point numbers Booleans Tuples Arrays Retrieving elements from an array Inserting elements into an array Modifying elements in an array Appending elements to an array Removing elements from an array Table of Contents 26 26 27 27 27 28 28 29 29 30 30 31 31 31 31 32 v Dictionaries Retrieving elements from a dictionary Modifying an item in a dictionary Removing an item from a dictionary Optional Types Working with implicitly unwrapped optionals Using optional binding Unwrapping optionals using “?” Using the nil coalescing operator Functions Understanding input parameters Returning a value Flow Control If-Else statement Ternary conditional operator Switch statement Looping For-In loop While loop Repeat-While loop Control transfer statements Range operators Structures Memberwise initializers Structures as value types Classes Defining a class Understanding properties Using methods in classes Trying out the self property Closures Understanding closures Using functions as closures Assigning closures to variables Writing closures inline Understanding type inference Using shorthand argument names Working with the operator function Using trailing closures Protocols Defining and using a protocol Conforming to a protocol Using the Codable protocol Using the some keyword vi SwiftUI For Dummies 32 32 33 33 33 35 36 36 37 38 38 39 39 40 40 40 41 42 42 42 42 43 43 44 45 47 47 47 49 50 51 51 52 52 53 53 54 54 55 55 56 56 57 59 PART 2: UNDERSTANDING THE BASICS OF SWIFTUI 61 CHAPTER 3: Getting Started with the Basics of SwiftUI 63 Taking a Look at SwiftUI Views Conforming to the View protocol Using modifiers Stacking modifiers Using the Inspector Displaying an Image Using modifiers on the Image view Resizing images Displaying a Button Customizing the button Adding actions Stacking the Views VStack HStack Putting on the Finishing Touches CHAPTER 4: 63 64 66 66 67 70 72 73 75 76 77 78 79 80 85 Handling User Inputs and Custom Views 87 Looking at Input Views 87 TextField 88 SecureField 92 Toggle 93 Slider 94 Stepper 98 Picker 100 Composing Custom Views 103 Composing the custom view 104 Using the custom view 111 CHAPTER 5: Displaying Lists of Items 113 Using the List View to Display Items Customizing the rows Adding rows programmatically Alternative way to generate rows in a List view Displaying the List within a NavigationView Making the items tappable Adding rows Using the Identifiable protocol Deleting rows Editing rows Moving rows 113 114 116 119 120 121 123 125 126 128 130 Table of Contents vii CHAPTER 6: CHAPTER 7: Displaying Sections Displaying sections from a dictionary Changing the style of the List view Previewing in Light and Dark Modes During runtime During design time 133 134 136 138 138 139 Creating Navigation and Tabbed Applications 143 Creating Navigation Apps Working with the two key views in navigation-style apps Navigating to a page Navigating programmatically Creating a news reader application Creating Tabbed Applications Using the TabView Selecting TabViews programmatically 144 145 148 149 151 167 168 169 Formatting Your User Interface 173 Laying Out Views Using Stacks 173 VStack 174 HStack 186 ZStack 190 Using Container Views 195 Form and Section .198 Group 200 Divider 203 PART 3: EXPLORING WITH SWIFTUI IN MORE DETAIL 205 Understanding State Management in SwiftUI 207 CHAPTER 8: Using a Property Wrapper 207 Maintaining State Using State Variables 209 Binding State Variables 213 Managing State from External Objects .218 Using the ObservableObject protocol and @Published .219 Using the @ObservedObject 221 Sharing Objects 223 Accessing Built-in Environment Variables 228 Defining your own environment keys 230 Using your own environment keys 231 viii SwiftUI For Dummies degrees(), 310 delete(), 127 doSomething, 38 encode(), 58 environmentObject(), 224 fetchData(), 154, 155, 239, 331, 333 font(), 66, 359–360 getProductCode(), 36 greater than (>), 55 insert(), 31 Int(), 33–34 lesser than () function, 55 greetings property, 245, 246, 247 Group view, 200–202, 359–360 H half-open range operator, 43 Haptic Touch, 373–374 Hashable protocol, 119 HorizontalAlignment properties, 188 HStack view, 78, 80–85, 114, 186–190, 196–197, 374, 380–381 Hudson, Paul, 376 I icons, explained, Identifiable protocol, 125–126 If statement, 34, 39–40 If-Else statement, 40 Image view, 70–75, 114–115, 190–191, 192, 194, 292, 374, 379–380 ImagePickerViewController creating, 254–256 using, 260–262 images custom progress indicators, 314–322 displaying, 70–75, 334–335 custom views, 103–112 displaying remotely, 157–160 details pages, 162–167 resizing, 73–75 ImagePickerViewController, 254–256 implementing localization, 354–358 layouts, 325–327 implicit fallthrough, 41 navigation applications, 143–172 implicitly unwrapped optionals, 35–36 news reader applications, 151–167 indeterminate progress indicator, 314–318 preferences view, 339–344 Info.plist file, 22 previews, 19–21 initializers, using in views, 246 projects, 323–349 input parameters, 38–39 Index 391 input views about, 87 Picker, 100–103, 339, 341, 382 SecureField, 92–93, 381 Slider, 94–98, 382 Stepper, 98–100, 382 TextField, 88–91, 381 Toggle, 93–94, 212, 382 insert() function, 31 K keepCapacity argument, 32 L Label view, 8, 10 labelsHidden() modifier, 94, 101–102 lastTextBaseline property, 188–190 layouts, creating, 325–327 inserting elements into arrays, 31 leading property, 79 Inspector, 67–70 lesser than (

Ngày đăng: 17/05/2021, 12:59

TỪ KHÓA LIÊN QUAN