iOS / Swift UIImageView(.jpg)无法识别我的点按手势?

时间:2015-07-25 14:39:59

标签: ios iphone swift uiimageview uigesturerecognizer

我有一个简单的代码块,可以在我的图像被点击时播放声音。但是,当我点按图像时,水龙头甚至无法识别。

我相信这是真的,因为在点击图像时,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!

}

1 个答案:

答案 0 :(得分:5)

请注意userInteractionEnabled UIImageView的默认设置为false。您想将其设置为true