iOS/Swift UIImageView (.jpg) not recognizing my tap gesture?(iOS/Swift UIImageView (.jpg) 无法识别我的点击手势?)
问题描述
我有一个简单的代码块,可以在我的图像被点击时播放声音.但是,当我点击我的图像时,甚至无法识别点击.
I have a simple block of code to play a sound when my image gets tapped. However, when I tap my image, the tap isn't even recognized.
我相信这是真的,因为当点击图像时,handleTap 函数中的 println 不会打印任何内容.
I believe this to be true because println in the handleTap function doesn't print anything when the image is tapped.
谁能告诉我问题出在哪里?
Can anyone give me some insight as to where the problem might be?
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.
Note that the default setting of the userInteractionEnabled of UIImageView is false. You want to set it to true.
这篇关于iOS/Swift UIImageView (.jpg) 无法识别我的点击手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS/Swift UIImageView (.jpg) 无法识别我的点击手势?
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
