播放基于UIImageVIew命名图像的声音不起作用

时间:2019-06-29 12:43:41

标签: swift xcode uiimageview

我开发了一个抽认卡应用程序,我有一个UIImageView以及单击手势来更改图片和一个按钮来播放基于图像的声音。

这是正在发生的事情,我使用数字循环浏览所有图像:

        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 0 {
                imageView.image = UIImage(named: "card2")
                imageView.image = UIImage(named: "card\(number)")
                number = number % 26 + 1
            }
            else if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 1 {
                imageView.image = UIImage(named: "upper2")
                imageView.image = UIImage(named: "upper\(number)")
                number = number % 26 + 1
            }
            else if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 2 {
                imageView.image = UIImage(named: "num2")
                imageView.image = UIImage(named: "num\(number)")
                number = number % 10 + 1
            }
}

然后我有一个按钮,该按钮应根据imageview中显示的图像播放声音:

        if imageView.image ==  UIImage(named: "card1") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: aSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }
        else if imageView.image !=  UIImage(named: "card2") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: bSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }
        else if imageView.image !=  UIImage(named: "card3") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: cSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }
}

我已经设置了声音的所有变量,并将它们添加为对我的xcode项目的引用:

var audioPlayer = AVAudioPlayer() 
let aSound = Bundle.main.path(forResource: "aSound.m4a", ofType:nil)!
let bSound = Bundle.main.path(forResource: "bSound.m4a", ofType:nil)!
let cSound = Bundle.main.path(forResource: "cSound.m4a", ofType:nil)!

问题是当我按下声音按钮时,它每次都播放相同的声音,就像它永远不会通过我的if语句读取一样。

1 个答案:

答案 0 :(得分:1)

问题是,当您尝试以这种方式比较图像

if imageView.image ==  UIImage(named: "card1") {
            ...
        }

您实际上是在尝试比较具有不同引用的两个不同对象。

为了实现所需的行为,可以采用两种不同的解决方案。

第一个正在使用视图的tag属性:

if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 0 {
                imageView.image = UIImage(named: "card2")
                imageView.image = UIImage(named: "card\(number)")
// HERE YOU CAN ADD A TAG
                imageView.tag = 1 //for example, it has to be Int
                number = number % 26 + 1
            }

因此,您可以通过以下方式比较tag

if imageView.tag ==  1 {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: aSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }

以此类推。

第二个是使用isEqual,它应该比较对象的hash值:

if imageView.image.isEqual(UIImage(named: "")){
            ...
        }