0

USING iOS SWIFT 3

I have developed a custom keyboard using a collectionView. Each collectionViewCell has a button inside from an array of buttons created in my code - When user taps this button I would like the appropriate sound from the array of sounds also created in my code to play....the arrays are set up so that the array of buttons corresponds directly to the array of sounds for instance

var buttons = ["1", "2", "3", "4"] var sounds = ["sound1", "sound2", "sound3", "sound 4"]

I have managed to get the sounds playing from the array in random order, but I want the sounds to play in order of the array - so when user taps button 1, sound1 will play....when user taps button 2, sound2 will play etc

At the moment on the iOS simulator when user taps button 1, any one of the above sounds is picked at random to play

here is the code I have used when user taps button in collectionViewCell sounds play randomly rather than matching to the order of the "sounds" array. FYI in the below code "Sounds" is the array of sounds I have created

 @IBAction func cellButton(_ sender: AnyObject) {
   let range: UInt32 = UInt32(Sounds.count)
    let number = Int(arc4random_uniform(range))

    self.setupAudioPlayer(file: Sounds[number] as NSString, type: ".m4a")
    self.soundPlayer.play()
}
  • use the tag variable on your buttons to distinguish different buttons, then in your array it is as simpe as Sounds[(sender as! UIButton).tag] – Knight0fDragon Sep 26 '16 at 17:15
  • thanks @Knight0fDragon for responding - so how do I tag each individual button? I have set up a prototype collectionViewCell with just one button in the cell and set the title of the button to correspond to an array button titles which works perfectly - the collectionView shows every button in perfect order as per my array of buttons. I have already tagged the cell button with number 2 to set the title. I think Im getting overly confused now!! Could you type out the code to replace the above code Ive shown - thank you – Guy de Bromhead Sep 26 '16 at 18:10
  • I am not sure, it would all depend on your design, just whereever you create the button, be sure to give it a tag id – Knight0fDragon Sep 26 '16 at 18:12
  • @Knight0fDragon I added this Sounds[(sender as! UIButton).tag] above self.audioplayer(file: Sounds[number] as NSString, type: ".m4a") to see if it would accept this code but its saying something about unused I-value now!! – Guy de Bromhead Sep 26 '16 at 18:20
  • I am not positive on what the tag type is, it may need a cast, also did you assign this to a variable, or did you just try accessing the array? – Knight0fDragon Sep 26 '16 at 18:23
  • im not sure either - the above code does access the array fine and does play all the sounds from the "Sounds" array when I tap the buttons in the collectionView. However it plays the sounds randomly rather than in the order of the "Sounds" array. I must be very close to getting the sounds to play in the order of the array just can't quite figure it out - been at it for hours!!! @Knight0fDragon – Guy de Bromhead Sep 26 '16 at 18:28
  • wait, you want to play the sounds one after another? I thought it was a sound per button – Knight0fDragon Sep 26 '16 at 18:32
  • Like button 1 is always quack, button 2 is always moo – Knight0fDragon Sep 26 '16 at 18:33
  • @Knight0fDragon yes I want to play sounds as button 1 is always quack, button 2 is always moo - right so now I have change the code for the button action to this as you said and it now every button plays sound three in the "Sounds" array probably because I have already used tag 2 in the tag field of the button IBAction func cellButton(_ sender: AnyObject) { self.setupAudioPlayer(file: Sounds[(sender as! UIButton).tag] as NSString, type: ".m4a") self.soundPlayer.play() } – Guy de Bromhead Sep 26 '16 at 18:36
  • Ok so let's try let number = (sender as! UIButton) tag – Knight0fDragon Sep 26 '16 at 18:38
  • @Knight0fDragon here is what i have now for the button action and every button in the collection view now plays sound 3 from the "Sounds" array 'IBAction func cellButton(_ sender: AnyObject) { let number = (sender as! UIButton).tag self.setupAudioPlayer(file: Sounds[number] as NSString, type: ".m4a") self.soundPlayer.play() }' – Guy de Bromhead Sep 26 '16 at 18:45
  • ok if every button is playing sound 3 than that means they all have a tag of 3, so now it is a matter of properly giving it a tag – Knight0fDragon Sep 26 '16 at 18:47
  • @Knight0fDragon must be getting close now!!! problem is i have used a tag on the button to set the button title of each cell to correspond with another array of images - so basically user taps the image in the cell that plays a sound - so I guess the question is how do I use the tagging idea to also tag the corresponding sounds in the "Sounds" array - so right now I have tagged the prototype cell button as the number "2" and therefore with the code we have established when we press any or all of the buttons in the collectionView it plays sound 3 from the array "Sounds" – Guy de Bromhead Sep 26 '16 at 19:00
  • func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell var Button = cell.viewWithTag(2) as! UIButton Button.setTitle(appleProducts[indexPath.row], for: UIControlState.normal) return cell } – Guy de Bromhead Sep 26 '16 at 19:01
  • where appleProducts is my array of button titles – Guy de Bromhead Sep 26 '16 at 19:02
  • ...you wouldn't prototype the tag, prototyping is abstract, the number 2 is very concrete – Knight0fDragon Sep 26 '16 at 19:02
  • @Knight0fDragon how would I set the title of each button to equal the exact order of my "appleProducts" array? And then also set the sound of each of the buttons to play in the exact order of my "Sounds" array – Guy de Bromhead Sep 26 '16 at 19:10
  • you know, it sounds to me like you are working backwards, instead, you should be having apple product and sounds as properties of UIButton – Knight0fDragon Sep 26 '16 at 19:12
  • so the end result would be button.sound and button.appleproduct – Knight0fDragon Sep 26 '16 at 19:13
  • now with extensions, it is a real pain to allocate variables, so I would recommend subclassing instead. UIButton could be AppleSoundButton or something – Knight0fDragon Sep 26 '16 at 19:20
  • @Knight0fDragon ok thank you for your help. I have set the tag of the button to zero now, so when I tap every cell button the number 1 sound in my Sounds array plays on every cell. this is because of this code in the action code of the button let number = (sender as! UIButton).tag - what code would I use instead of a tag to play the sounds in the order of the "Sounds" array here is the code of the button in every cell – Guy de Bromhead Sep 26 '16 at 19:52
  • @IBAction func cellButton(_ sender: AnyObject) { let number = (sender as! UIButton).tag self.setupAudioPlayer(file: Sounds[number] as NSString, type: ".m4a") self.soundPlayer.play() } – Guy de Bromhead Sep 26 '16 at 19:52
  • @Knight0fDragon essentially what could I let number = instead of what it is currently equalling which is a tag - is there a way to let number = Sounds[indexPath.row] or something like that so it follows the order of the "Sounds" array or let number = Button.Sounds – Guy de Bromhead Sep 26 '16 at 19:58

0 Answers0