I am learning Swift, and I am throwing myself in the deep end to force myself to learn the language. I have a nephew who is a baby and thought to make an app to help him learn numbers.
The app is designed to set a set number of buttons on the screen like the one provided below. I have the code to play Directions, which tells the user which number to select. A-N14a, the audio file, says to click the 4. The Done button is set to move to the next screen.
What I am asking is that if I want 4 to be pressed, and they press the 9, I want to know how to implement a feature to give a hint to click the number 4? The idea is to change the background to a button, but I don’t know how to implement the feature. I am also open to other ideas. As a note, I do not know what to do, and I’m trying to learn, so the code provided is probably very simplistic and is at the beginning stages.
Below is an image of the screen and the code for that page.
import UIKit
import AVFoundation
class Intervention_Numerals1: UIViewController {
@IBOutlet weak var Directions: UIButton!
@IBOutlet weak var Done: UIButton!
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
setUpElements()
//Audio Test
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "A-N14a", ofType:"mp3")!))
audioPlayer.prepareToPlay()
} catch {
print(error)
}
}
func setUpElements() {
// Style the elements
Utilities.styleFilledButton(Directions)
Utilities.styleFilledButton(Done)
}
@IBAction func Play(_ sender: Any) {
audioPlayer.play()
}
}
Please let me know any tips or advice or links to similar questions, even though I could not find any on my own.
2
Answers
I would do as follows:
1. Create as many IBOutlets as your numbers (I suppose 0-9 for your example?) and link them to your buttons – E.g.
2. Create an IBAction and link it to all your buttons, with this code
Enjoy!
Here’s what I would do:
Record the sound "Tap the number" and then the sounds for the numbers 0 through 9. Name the number sounds "0.mp3" through "9.mp3"
Create a storyboard with 4 buttons on it (like the picture you posted.)
Set up button IBOutlets
buttonA
–buttonD
. Put those buttons into an array:Fill an array with the numbers 0-9. Shuffle it. Remove 4 values put them into an array "buttonValues" (use the method
removeLast()
.) The code to generate non-repeating values from 0-9 might look like this:Loop through your array of buttonValues and install the string for each number as the title of one of your buttons:
for index = 0…3 {
buttonsArray[index].setTitle("(buttonValues[index])", forSate: .normal)
}
Pick an index 0-3 to be the "correct" number.
Look up that value in buttonValues, and use it to pick a sound file to play:
Load and play the "tap the number" sound, and then Load and play the sound for the selected number (
soundName
).When the user taps a button, have the IBAction method use the sender parameter that is passed to it, and look in the array of buttons,
buttonsArray
, to see which button index was tapped.If it is the correct button, take the success action.
If the tapped button index is not
indexToPick
, do an animation that changes the background color of the button atindexToPick
, or the button’s border width, or something, and then animates it back to normal. (Look at the UIViewanimate(duration:)
family of methods for how to animate the button’s background color. Use the form that takes anoptions:
parameter, and set the.autoreverse
option.)If you’re a newbie to iOS development, figuring out how to animate your correct answer button could be a challenge. I created a sample project that just animates one of 4 random buttons: https://github.com/DuncanMC/ButtonAnimation.git
The code for that project is as follows: