【发布时间】:2018-08-31 04:46:11
【问题描述】:
我正在尝试制作我的应用程序,以便当用户触摸 UIImageView 时会播放某种声音。但是,在播放该声音时,我希望将 UIImageView.isUserInteractionEnabled 分配为 false。声音播放完毕后,我希望将 UIImageView.isUserInteractionEnabled 分配为 true。每当我运行以下代码时,即使我强制展开,我也会在卡类中遇到错误。下面是包含我要禁用的图像视图的类。
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
@IBOutlet weak var imgPhoto: UIImageView!
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
imgPhoto.isUserInteractionEnabled = false
itemList[imageIndex].playSound()
}
}
这是 playSound 函数所在的类。
import Foundation; import UIKit; import AVFoundation
class Card: NSObject
{
var player: AVAudioPlayer?
var svc = SecondViewController()
var image: UIImage
var soundUrl: String
init(image: UIImage, soundUrl: String, isActive:Bool = true) {
self.image = image
self.soundUrl = soundUrl
}
func playSound()
{
guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
player?.delegate = self
guard let player = player else { return }
player.prepareToPlay()
player.play()
audioPlayerDidFinishPlaying(player)
print("play")
} catch let error {
print(error.localizedDescription)
}
}
}
extension Card: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer){
svc.imgPhoto.isUserInteractionEnabled = true
}
}
【问题讨论】:
标签: ios swift xcode avaudioplayer fatal-error