/hoth-v-ios-workshop

Resources for HOTH V Introduction to iOS Workshop

Primary LanguageSwift

HOTH 5 - iOS Workshop README

Slides: https://docs.google.com/presentation/d/1jdLz1cU41l7fj3WQTB8BHoS2whf8-1vAX3s8D_2MoaQ/edit?usp=sharing

Image: https://imgur.com/a/u1fm6

Table of Contents

Getting Started

  1. Begin by creating a new project in Xcode.
  2. Select "Create a new Xcode project" near the bottom left of the window.

img


  1. Ensure that the "iOS" tab at the top of the page is selected.
  2. Double-click on "Single View App".

img


  1. Name your project "BruinTap".
  2. Pick whatever Team, Organization Name and Organization Identifier that you want.
  3. Ensure that the Language field is set to "Swift".
  4. Ensure that "Use Core Data" is unchecked.
  5. Click "Next".
  6. On the following window, select where you'd like your project's folder to be create (default location is ~/Desktop) and click "Create".

img


Interface Builder (IB)

  1. In the Project Navigator on the left, you should see all of the files associated with our project. The only group (folder) we're interested in for the purposes of this tutorial is the BruinTap group. The BruinTapTests, BruinTapUITests, and Products groups aren't important for now.
  2. Inside the BruinTap group you should see a file named Main.storyboard. Click it and you'll be taken to the Interface Builder.


Setting Up the UI (User Interface)

  1. Locate the button that has a circle with a square in it on the top right of the screen and click it.
  2. The Object Menu will appear and contain UI elements we can drag into our storyboard.


  1. Scroll down in the menu until you see the "Label" and "Button" elements.


  1. Drag two Labels and two Buttons into the View Controller, and arrange them as they are in the following photo (it doesn't have to be exact, just more or less):


Setting Constraints Using Auto Layout

  1. Select the Button we placed in the top left corner.
  2. On the right side of the screen, open the Attributes Inspector by clicking the tab to the left of the ruler icon:


  1. Under the "Title" field (third from the top), change the text from "Button" to "Start" and press Enter.


  1. With the Start button still selected, click the "Add New Constraints" button near the bottom right of the page.


  1. Set both the top and left margins to 30. The red lines should now be solid rather than dashed.
  2. Click "Add 2 Constraints".


  1. Now select the Button in the middle of the page.
  2. Click the "Add New Constaints" button near the bottom right of the page.
  3. Set the Width and Height to 150. The boxes next to Width and Height should now be checked.
  4. Click "Add 2 Constraints".


  1. With the Button in the middle of the View Controller still selected, click the "Align" button directly to the left of the "Add New Constraints" button near the bottom right of the page.
  2. Check both the "Horizontally in Container" and "Vertically in Container" boxes.
  3. Click "Add 2 Constraints".


  1. Select the Label above the Button in the middle of the page.
  2. In the Attributes Inspector on the right side of the screen (the tab to the left of the ruler icon), change the Text field to 0 (then press Enter) and change the Alignment to centered.


  1. With the top label still selected, click the "Add New Constaints" button near the bottom right of the page.
  2. Set the bottom margin to 75. The red line should now be solid rather than dashed.
  3. Click "Add 1 Constraint".


  1. With the top label still selected, click the "Align" button directly to the left of the "Add New Constraints" button near the bottom right of the page.
  2. Only check the "Horizontally in Container" box.
  3. Click "Add 1 Constraint".


  1. Now select the Label below the Button in the middle of the page.
  2. In the Attributes Inspector on the right side of the screen (the tab to the left of the ruler icon), change the Text field to 10.00 (then press Enter) and change the Alignment to centered.


  1. With the bottom label still selected, click the "Add New Constaints" button near the bottom right of the page.
  2. Set the top margin to 75. The red line should now be solid rather than dashed.
  3. Click "Add 1 Constraint".


  1. With the bottom label still selected, click the "Align" button directly to the left of the "Add New Constraints" button near the bottom right of the page.
  2. Only check the "Horizontally in Container" box.
  3. Click "Add 1 Constraint".


  1. All set, verify that your View Controller now looks like this:


Adding Images Using XCAssets

  1. In the Project Navigator on the left, select the folder icon named Assets.xcassets.
  2. In the left pane (the one containing the "AppIcon" entry), right click and select "New Image Set".


  1. Single click our newly created Image and rename it to ButtonBG.


  1. Click here and download the image.
  2. From Finder/your Downloads, drag the downloaded image into the slot in ButtonBG labeled 1x.


  1. Return via the Project Navigator to Main.storyboard.
  2. Select the Button in the center of the View Controller and set its Title field to nothing/empty via the Attributes Inspector.


  1. Five fields down from the "Title", use the drop-down menu to change the "Background" to ButtonBG.


Linking your IB Objects to your Code

  1. Click on the two overlapping circles near the top right of Xcode to open the Assistant Editor.
  2. If it is difficult to see our View Controller, close the left pane if necessary by clicking on the button (square with black rectangle on the left side) to the left of "View as iPhone..." in the bottom left of the window.


  1. While holding down control (not to be confused with cmd(⌘)), click on the "Start" button and drag to the space directly above the viewDidLoad() function.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. Set the "Name" field to startButton.
  4. Click "Connect".


  1. While holding down control, click on the button in the middle of the View Controller and drag to the space directly below the startButton line we just made.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. Set the "Name" field to tapButton.
  4. Click "Connect".
  5. While holding down control, click on the "0" label and drag to the space directly below the tapButton line we just made. (Note: You may have to zoom in via pinch-drag on your touchpad to be able to select the label itself).
  6. In the window that appears, ensure that the "Type" field says UILabel, else press "Cancel" and try again.
  7. Set the "Name" field to scoreLabel.
  8. Click "Connect".
  9. While holding down control, click on the "10.00" label and drag to the space directly below the scoreLabel line we just made. (Note: You may have to zoom in via pinch-drag on your touchpad to be able to select the label itself).
  10. In the window that appears, ensure that the "Type" field says UILabel, else press "Cancel" and try again.
  11. Set the "Name" field to timeLabel.
  12. Click "Connect".
  13. Add a new line under the "timeLabel" declaration we just made if necessary (for neatness).
  14. You should see the following:


Linking IB Actions (Button Taps) to your Code

  1. While holding down control, click on the "Start" button and drag to the space directly above the viewDidLoad() function.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. Use the drop-down menu to change the "Connection" field from Outlet to Action.
  4. Set the "Name" field to tappedStartButton.
  5. Use the drop-down menu to change the "Type" field from Any to UIButton.
  6. Ensure that the "Event" field is set to Touch Up Inside and that the "Arguments" field is set to sender.
  7. Click "Connect".


  1. While holding down control, click on the Button in the middle of the View Controller and drag to the space below the function we just made, but still above the viewDidLoad() function.
  2. In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
  3. Use the drop-down menu to change the "Connection" field from Outlet to Action.
  4. Set the "Name" field to tappedTapButton.
  5. Use the drop-down menu to change the "Type" field from Any to UIButton.
  6. Ensure that the "Event" field is set to Touch Up Inside and that the "Arguments" field is set to sender.
  7. Click "Connect".
  8. Add newlines as necessary for neatness.
  9. You should see the following:


Coding in Swift

Declaring the Properties we Need

  1. Close the Assistant Editor by clicking on the "X" on the top right of the pane with our code in it.


  1. In the Project Navigator on the left side of the screen, select the "ViewController.swift" file.


  1. Above all of the IBOutlet statements, but still within the (class ViewController block), add the following code:
var score = 0
    
var timer: Timer!
var time: Double = 10


Incrementing Score when we Tap the Button

  1. Inside the tappedTapButton() function, add the following code:

    score += 1
    scoreLabel.text = String(score)


  1. Use the "Play" button in the top left of Xcode to build and test our app out. Selecting a smaller phone like the iPhone 5S or 6 will make it easier to see everything.
  2. Try pressing the button. The score should go up each time we tap it!


Implementing a Timer

  1. Inside ViewController.swift, declare a function exposed to Objective-C between tappedTapButton() and viewDidLoad() named timerAction() and fill it with the following code:

    @objc func timerAction() {
            
            time -= 0.01
            timerLabel.text = String(format: "%.2f", time)
            
            if time <= 0 {
                
                timer.invalidate()
                timerLabel.text = "0.00"
                
                startButton.isEnabled = true
                
            }
            
    }
  2. Inside the tappedStartButton() function, add the following code:

    time = 10
    timeLabel.text = String(format: "%.2f", time)
            
    timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
            
    startButton.isEnabled = false


  1. Build and run our app again. Tap the "Start" button and you should see the timer counting down!

Disabling and Enabling the Tap Button

  1. Inside the viewDidLoad() function, add the following code:

    tapButton.isEnabled = false
  2. Change the timerAction() function to the following:

    func timerAction() {
            
            time -= 0.01
            timerLabel.text = String(format: "%.2f", time)
            
            if time <= 0 {
                
                timer.invalidate()
                timerLabel.text = "0.00"
                
                startButton.isEnabled = true
                tapButton.isEnabled = false
                
            }
            
    }
  3. Change the startTap() function to the following:

    @IBAction func startTap(_ sender: UIButton) {
            
            score = 0
            time = 10
            
            scoreLabel.text = String(score)
            timerLabel.text = String(format: "%.2f", time)
            
            timer = Timer.scheduledTimer(timeInterval: 0.01, 
                                         target: self, 
                                         selector: #selector(timerAction), 
                                         userInfo: nil, 
                                         repeats: true)
            
            startButton.isEnabled = false
            tapButton.isEnabled = true
            
    }
  4. Build and run the app again. Congratulations! You have finished your first app!