gpt4 book ai didi

ios - UIImageview 点击不工作

转载 作者:行者123 更新时间:2023-11-28 12:20:21 25 4
gpt4 key购买 nike

我是 swift 的新手。我正在为 UIIMageview 实现点击事件的功能。我的代码如下。

override func viewDidLoad() {
super.viewDidLoad()
initialize()
let logout = UIImage(named: "logout.png")
let location = UIImage(named:"location.png")
let appIcon = UIImage(named: "logo.png")

imgLogout.image = logout
imgLocation.image = location
appicon.image = appIcon
// Do any additional setup after loading the view, typically from a nib.

imgLogout.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ProductListController.profileImageHasBeenTapped(gesture:)))
//this is where we add the target, since our method to track the taps is in this class
//we can just type "self", and then put our method name in quotes for the action parameter


//finally, this is where we add the gesture recognizer, so it actually functions correctly
imgLogout.addGestureRecognizer(tapGestureRecognizer)

}


func profileImageHasBeenTapped(gesture: UIGestureRecognizer){
print("Triggered")

}

我的点击事件没有触发。任何人都可以帮助我。任何将不胜感激。

XCode 版本为 8.3.3

Swift 版本为 3.1

最佳答案

为什么不使用专为这种用途设计的 UIButton

let logout = UIButton(type: .custom)
logout.setImage(UIImage(named: "logout.png"), forState: .normal)
logout.addTarget(self, action: #selector(self.performLogout), for: .touchUpInside)

@objc
func performLogout() {
// Do something
}

关于ios - UIImageview 点击不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com