iOS/Swift UIImageView (.jpg) 无法识别我的点击手势?
iOS/Swift UIImageView (.jpg) not recognizing my tap gesture?
我有一个简单的代码块,可以在点击我的图像时播放声音。但是,当我点击我的图像时,点击甚至都无法识别。
我相信这是真的,因为 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
。
我有一个简单的代码块,可以在点击我的图像时播放声音。但是,当我点击我的图像时,点击甚至都无法识别。
我相信这是真的,因为 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
。