Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中检测UIImageView触摸_Ios_Swift_Uiimageview - Fatal编程技术网

Ios 在Swift中检测UIImageView触摸

Ios 在Swift中检测UIImageView触摸,ios,swift,uiimageview,Ios,Swift,Uiimageview,当触摸UIImageView时,您将如何检测并运行操作? 这是我目前掌握的代码: override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { var touch: UITouch = UITouch() if touch.view == profileImage { println("image touched") } } override f

当触摸
UIImageView
时,您将如何检测并运行操作? 这是我目前掌握的代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    var touch: UITouch = UITouch()
    if touch.view == profileImage {
        println("image touched")
    }
}
override func touchsbegind(touchs:Set,withEvent-event:UIEvent){
变量touch:UITouch=UITouch()
如果touch.view==profileImage{
println(“触摸图像”)
}
}

您可以使用Interface Builder或在代码中(根据需要)在
UIImageView
中放置一个
UIAPTgestureRecognitor
,我更喜欢第一个。然后,您可以在
UIImageView
中放置
@iAction
并处理点击,不要忘记在Interface Builder或code中将
UserInteractionEnabled
设置为
true

@IBAction func imageTapped(sender: AnyObject) {
    println("Image Tapped.")
}

我希望这对您有所帮助。

您可以通过在
UIImageView
中添加一个来检测触摸

需要注意的是,默认情况下,
UIImageView
isUserInteractionEnabled
属性设置为false,因此必须在情节提要中或以编程方式显式设置它


斯威夫特3

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first!
        if touch.view == profileImage {
    println("image touched")
}

}


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first!
        if touch.view == profileImage {
    println("image released")
}

}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
让触摸:UITouch=touch.first!
如果touch.view==profileImage{
println(“触摸图像”)
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
让触摸:UITouch=touch.first!
如果touch.view==profileImage{
println(“图像发布”)
}
}

确保
userInteractionEnabled
设置为
true
。我相信
UIImageView
@AdamPro13默认情况下是
false
,它仍然不能与我上面的代码一起工作。需要在viewDidLoad()方法中添加profileImage.isUserInteractionEnabled=true。如何在“imageTapped”函数中传递额外参数?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first!
        if touch.view == profileImage {
    println("image touched")
}

}


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first!
        if touch.view == profileImage {
    println("image released")
}

}