UIImageView는 UIButton과 같이 addTarget을 하여서 클릭을 자체적으로 감지를 할 수 없다. 그렇기에 TapGesture를 활용해서 뷰가 클릭된 것을 감지해야한다.
let photoButton = UIImageView().then{
$0.image = UIImage(named: "photoButton")?.withRenderingMode(.alwaysOriginal)
}
@objc func didClickPhoto(sender: UITapGestureRecognizer) {
print("didClickPhoto")
}
let PhotoBtn = UITapGestureRecognizer(target: self, action: #selector(didClickPhoto))
photoButton.isUserInteractionEnabled = true
photoButton.addGestureRecognizer(PhotoBtn)
이렇게 되면
1. 유저가 UIImageView 클릭
2. 클릭 gesture에 반응해서 PhotoBtn를 실행시킴
3. 이는 didClickPhoto 실행시키게 코딩 되어있음
4. didClickPhoto "didClickPhoto"를 print하게끔 코딩되어있음
5. didClickPhoto 출력