Hi all, I’m trying to write an app that if you swipe an image you change to a randomly generated image and that works fine. I’ve tried to return a value from that function so if I either tap the icon or press a button it plays a sound file specific to that image. However the I use tap gesture recognizer or the button pressed function it seems to loop through selecting a random number again, presenting the new image and the sound that applies to that issue. I’ve put the code below, can anyone suggest where I’m going wrong
import UIKit
import AVFoundation
class ViewController: UIViewController
{
let wordList = ["1": "a", "2": "b", "3": "c", "4": "d", "5": "e", "6": "f"]
var audioPlayer:AVAudioPlayer?
@IBOutlet weak var frontImage: UIImageView!
@IBOutlet weak var ranNumLabel: UILabel!
@IBOutlet weak var messageLabel: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
frontImage.isUserInteractionEnabled = true
let swipeLeftRecogniser = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.imageSwipedLeft))
frontImage.addGestureRecognizer(swipeLeftRecogniser)
// let tapRecogniser = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped))
// frontImage.addGestureRecognizer(tapRecogniser)
}
@objc func imageSwipedLeft() -> String
{
var sLetter = ""
let ranNumber = arc4random_uniform(6) + 1
print("The random number was: \(ranNumber)")
frontImage.image = UIImage(named: "\(wordList[String(ranNumber)]!).jpeg")
sLetter = ""
if ranNumber == 1
{
sLetter = "a"
}
if ranNumber == 2
{
sLetter = "b"
}
// and continues for other values
return sLetter
}
@IBAction func soundBtnClicked(_ sender: Any)
{
let letter = imageSwipedLeft()
print(letter)
ranNumLabel.text = "The button is tapped"
}
// I know how to write the code to play a sound file, just written the print and message code to see what's being returned, I also know it doesn't swipe left, only works on default swipe right at the moment
Any help greatly appreciated