我正在尝试UIImageView在用户点击操作时将其分配给操作。
UIImageView
我知道如何为a创建动作UIButton,但是如何模仿a的相同行为UIButton却使用a UIImageView?
UIButton
您需要一个UITapGestureRecognizer。要进行设置,请使用以下命令:
UITapGestureRecognizer
override func viewDidLoad() { super.viewDidLoad() let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))) imageView.isUserInteractionEnabled = true imageView.addGestureRecognizer(tapGestureRecognizer) } @objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) { let tappedImage = tapGestureRecognizer.view as! UIImageView // Your action }
(您也可以使用,UIButton并为其分配图片,不添加文字,而只需连接即可IBAction)
IBAction