[Mobile Lab] Week 01: Single Button App

Our first assignment for this Mobile Lab class was making a one-button iPhone app. I came up with an idea of Bomb app, which people can use like drawing lots. In this app, a bomb was intended to explode once in seven chances.

I designed the UI on the Sketch app.:

For the explosion sound, I downloaded an mp3 file from freesound.org

I actually wanted to make the ‘normal2’ image only appears when a user touched the screen but not yet released it. However, I couldn’t find out the method for detecting ‘touch begin’ and ‘touch ended’, so I instead made the app to show those two images by turns as long as the bomb doesn’t explode.

The code in ViewController.swift file is like below:

//
import UIKit
import AVFoundation

class ViewController: UIViewController {

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    // EDIT START ////////////////////////////////////////////////////////////////////////////////////////
    
    // Set to 'true' or 'false' to control content sequence.
    // Either flip through content in sequence or randomize.
    let randomize = true
    
    // Set to array size.
    // Make sure all arrays are the same length and matches array size.
    let arraySize = 7
    
    
    // Image name array.
    // Set to image name or set to empty string.
    let imageNameArray = ["img01.png",
                          "img02.png",
                          "",
                          "",
                          "",
                          "",
                          "img03.png"]
    
    // MP3 sound file array.
    // Set mp3 file name or set to empty string.
    let soundArray = ["",
                      "",
                      "",
                      "",
                      "",
                      "",
                      "bomb_explosion"]
    
    // EDIT END //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    
    // Connected to storyboard UI elements.
//    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var imageView: UIImageView!

    var player: AVAudioPlayer?

    var currentIndex = 0
    var normalState = 0
    
    // Initial setup function.
    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.image = UIImage(named: imageNameArray[currentIndex])
        
        //updateContent()
    }

    // Called when screen is tapped.
    @IBAction func handleTap(_ sender: UITapGestureRecognizer) {
        updateContent()
    }

    
    // Update content based on array content and current array index.
    func updateContent() {
        // Update content index.,
        nextIndex()


        
        // Update image if string is not empty
        if imageNameArray[currentIndex].isEmpty {
            imageView.image = nil
        } else {
            imageView.image = UIImage(named: imageNameArray[currentIndex])
        }

        // Play sound.
        if !soundArray[currentIndex].isEmpty {
            playSoundMP3(filename: soundArray[currentIndex])
        }
    }

    
    // Either increment index or randomize.
    func nextIndex() {
        if randomize {
            if (currentIndex == arraySize-1) {
                currentIndex = 0
            }
            else {
                currentIndex = Int(arc4random_uniform(UInt32(arraySize)))
                if(currentIndex < arraySize-1) {
                    if(normalState==0) {
                        currentIndex = 1
                        normalState = 1
                    }
                    else {
                        currentIndex = 0
                        normalState = 0
                    }
                }
            }
        } else {
            currentIndex = (currentIndex + 1 == arraySize) ? 0 : currentIndex + 1
        }
    }

    // Make the device vibrate.
    func generateImpactFeedback() {
        let generator = UIImpactFeedbackGenerator(style: .heavy)
        generator.impactOccurred()
    }

    // Play a mp3 sound file.
    func playSoundMP3(filename: String) {
        guard let url = Bundle.main.url(forResource: filename, withExtension: "mp3") else { return }
        
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            
            player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
            
            guard let player = player else { return }
            player.play()
        } catch let error {
            print(error.localizedDescription)
        }
    }
    

}

The whole project file is available here

 

Leave a Reply

Your email address will not be published. Required fields are marked *