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")
}
}