vendredi 23 juin 2017

how to link an image to a sound randomly

I have this code and it's working, but not as really want to :). I need your help to make me understand (as I'm new to Xcode/Swift/developing apps) how to do some things .. I've made it watching tutorials and using some snippets as examples..but so far is working :)

  1. I'd like to make it work in this way: when I click on playSound and hear it, then I have to choose between leftImage and rightImage..the image that matches with the playSound. I've tried into many ways to get it work.. but nothing...I cannot make the sound index a string to compare as "if a == b .."

  2. when I open the app, it shows me only two bottom buttons.. but not any image. How can I make it to show the first image?

  3. I also like to make it a bit random think... When I click on "nextImage" button, I'd like to shows to different images but only one linked to the sound..so when I play the sound..had to check only the photo who matches with the sound.

  4. At this moment, I have only 8 photos/sounds into the array, but when I click more than 9 times on nextImage.. the images are going on and on.. but the sound starts from beginning and it's not linked anymore. for example, at the 10th image..it playSound says it's at 1. How can I make It to follow the image index?

  5. How to convert the image index into text? for example, if its shows me the image "foto1" .. I'd like to show me under the image a text in the label.

thank you for your time and I hope someone will help me solve this :)

import UIKit

import AVFoundation

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

var soundFiles: [String] = [
    "s0",
    "s1",
    "s2",
    "s3",
    "s4",
    "s5",
    "s6",
    "s7",
    "s8",
    "s9"
]

var images1: [UIImage] = [

    UIImage(named: "foto1.png")!,
    UIImage(named: "foto2.png")!,
    UIImage(named: "foto3.png")!,
    UIImage(named: "foto4.png")!,
    UIImage(named: "foto5.png")!,
    UIImage(named: "foto6.png")!,
    UIImage(named: "foto7.png")!,
    UIImage(named: "foto8.png")!
]

var images2: [UIImage] = [

    UIImage(named: "foto1.png")!,
    UIImage(named: "foto2.png")!,
    UIImage(named: "foto3.png")!,
    UIImage(named: "foto4.png")!,
    UIImage(named: "foto5.png")!,
    UIImage(named: "foto6.png")!,
    UIImage(named: "foto7.png")!,
    UIImage(named: "foto8.png")!
]

var happySad: [UIImage] = [
    UIImage(named: "sad.png")!,
    UIImage(named: "happy.png")!
    ]

var currentImageIndex = 0
var currentImage2Index = 0


@IBOutlet weak var leftImage: UIImageView!

@IBOutlet weak var rightImage: UIImageView!

@IBOutlet weak var sh: UIImageView!





@IBAction func nextImages(_ sender: Any) {
 currentImageIndex += 1
    let numberOfImages = images1.count
    let nextImage1Index = currentImageIndex % numberOfImages
    leftImage.image = images1[nextImage1Index]
    leftImage.isUserInteractionEnabled = true
    self.view.addSubview(leftImage)
    let gesture1 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap1))
    leftImage.addGestureRecognizer(gesture1)


 currentImage2Index += 1
    let numberOfImages2 = images2.count
    let nextImage2Index = currentImage2Index % numberOfImages2
    rightImage.image = images2[nextImage2Index]
    sh.image = UIImage(named: "question")
    rightImage.isUserInteractionEnabled = true
    self.view.addSubview(rightImage)
    let gesture2 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap2))
    rightImage.addGestureRecognizer(gesture2)

}

func singleTap1() {
    if currentImageIndex == currentImage2Index {
        sh.image = UIImage(named: "happy.png")
        print("ok")
    } else {
        sh.image = UIImage(named: "sad.png")
        print("not ok")
    }
}
func singleTap2() {
    if currentImageIndex == currentImage2Index {
        sh.image = UIImage(named: "happy.png")
        print("ok2")
    } else {
        sh.image = UIImage(named: "sad.png")
        print("not ok2")
    }
}


var player: AVAudioPlayer!

@IBAction func playSound(_ sender: Any) {
    let numberOfImages = images1.count
    let nextImage5Index = currentImageIndex % numberOfImages
    let soundFilePath = Bundle.main.url(forResource: soundFiles[nextImage5Index], withExtension: ".m4a")!
    player = try! AVAudioPlayer(contentsOf: soundFilePath)
    player.prepareToPlay()
    player.play()

}

}




Aucun commentaire:

Enregistrer un commentaire