{"id":2953,"date":"2020-08-19T21:25:17","date_gmt":"2020-08-19T21:25:17","guid":{"rendered":"http:\/\/4310.mynmi.net\/?page_id=2953"},"modified":"2026-04-23T21:20:48","modified_gmt":"2026-04-23T21:20:48","slug":"homework-assignment-3","status":"publish","type":"page","link":"https:\/\/nmi.cool\/appdev\/homework-assignment-3\/","title":{"rendered":"Homework Assignment 3"},"content":{"rendered":"\n<p>In this assignment, you&#8217;ll build on the CardView lesson to create a scrollable row of playing cards. You&#8217;ll design a reusable card component and populate a <code>ScrollView<\/code> with multiple instances of it.<\/p>\n\n\n\n<p><strong>Important:<\/strong> <code>VStack<\/code> and <code>HStack<\/code> have a limit of 10 direct child views. Stick to 10 cards, or nest stacks if you want more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get Started<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a new Xcode project named <strong>ScrollViewAssignment<\/strong>.<\/li>\n<li><a href=\"https:\/\/nmi.cool\/appdev\/wp-content\/uploads\/sites\/17\/2022\/08\/cards.zip\">Download the card images<\/a>, unzip the file, and drag the <strong>face cards<\/strong> (those with letter names like <code>AD<\/code>, not number names) into <code>Assets.xcassets<\/code>.<\/li>\n<li>Create a new SwiftUI view file and name it <strong>Cards<\/strong> (or similar).<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Build the Card Component<\/h2>\n\n\n\n<p>Inside <code>Cards<\/code>, design a card that matches the example shown in class, then add variables for the values that differ from card to card.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/nmi.cool\/appdev\/wp-content\/uploads\/sites\/10\/2020\/08\/img_5f3d92e3ac44d.png\" alt=\"Example Ace of Diamonds card to recreate\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a <strong>variable for the image name<\/strong> and a <strong>variable for the title<\/strong>. Set default values for each so your Preview works without extra setup \u2014 use these exact defaults: This differs slightly from the textbook approach \u2014 their way also works, but this is faster.<\/li>\n\n\n<pre class=\"wp-block-code\"><code>var thetitle = \"Ace of Diamonds\"\nvar theimage = \"AD\"<\/code><\/pre>\n\n<li>Use <code>Image(theimage)<\/code> in your view, referencing the variable instead of a literal string.<\/li>\n<li>Add <strong>padding with a number<\/strong> to the image (e.g., <code>.padding(10)<\/code>).<\/li>\n<li>Wrap the image and title in a <code>VStack<\/code>. Apply both a plain <code>.padding()<\/code> and a numbered <code>.padding(.bottom, N)<\/code> to the <code>VStack<\/code>. You can stack multiple padding modifiers on the same view \u2014 that&#8217;s intentional.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Set Up the ScrollView<\/h2>\n\n\n\n<p>Back in <code>ContentView<\/code>, set up a horizontally scrolling row of cards:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Add a <code>ScrollView<\/code> with a <strong>horizontal axis<\/strong>.<\/li>\n<li>Place an <code>HStack<\/code> inside it and add <strong>10 card instances<\/strong>, passing a different face card image name to each.<\/li>\n<li>Test it \u2014 the cards should scroll smoothly from side to side.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/nmi.cool\/appdev\/wp-content\/uploads\/sites\/10\/2020\/08\/img_5f3d9751e1804.png\" alt=\"ScrollView result showing row of scrollable cards\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus: Colored Background<\/h2>\n\n\n\n<p>To add a background color to the card view, open <code>Cards<\/code> and wrap a <code>ZStack<\/code> around the outer <code>VStack<\/code>. Insert this as the first child inside the <code>ZStack<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Color.yellow\n    .opacity(0.2)\n    .edgesIgnoringSafeArea(.all)<\/code><\/pre>\n\n\n\n<p>Swap in any color and opacity that you like.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this assignment, you&#8217;ll build on the CardView lesson to create a scrollable row of playing cards. You&#8217;ll design a reusable card component and populate a ScrollView with multiple instances of it. Important: VStack and HStack have a limit of 10 direct child views. Stick to 10 cards, or nest stacks if you want more. &hellip; <a href=\"https:\/\/nmi.cool\/appdev\/homework-assignment-3\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Homework Assignment 3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2953","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/2953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/comments?post=2953"}],"version-history":[{"count":9,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/2953\/revisions"}],"predecessor-version":[{"id":4270,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/2953\/revisions\/4270"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/media?parent=2953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}