我有一个简单的代码块,可以在点击我的图像时播放声音。但是,当我点击我的图像时,点击甚至都无法识别。
我相信这是真的,因为 handleTap 函数中的 println 在点击图像时不会打印任何内容。
谁能告诉我问题出在哪里?
var coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("wavSampleSound", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
loadSound()
let gesture = UITapGestureRecognizer(target: self, action: "handleTap:")
gesture.delegate = self
myImage.addGestureRecognizer(gesture)
}
func loadSound() {
audioPlayer = AVAudioPlayer(contentsOfURL: coinSound, error: nil)
audioPlayer.prepareToPlay()
}
func handleTap(gesture: UITapGestureRecognizer) {
let playsSound = self.audioPlayer.play()
println("\(playsSound)")
}
@IBOutlet weak var myImage: UIImageView!
最佳答案
请注意,UIImageView
的userInteractionEnabled
默认设置为false
。您希望将其设置为 true
。
关于iOS/Swift UIImageView (.jpg) 无法识别我的点击手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31627597/