使图像可点击以展开

Make an image clickable to expand

我有一张背景图片,我想在点击时将其展开。我设置了使背景变暗并将图像支撑到中心的动画,但由于某种原因图像不可点击。

我尝试实现一个简单的打印(点击),当我点击时我也没有看到任何东西。我想知道我是否必须将我的图像视图声明为其他东西?

import Foundation
import UIKit

class JobViewController: UIViewController {


let imageView: UIImageView = {
    let iv = UIImageView(image: #imageLiteral(resourceName: "yo"))
    iv.contentMode = .scaleAspectFill
    iv.isUserInteractionEnabled = true
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomTap)))
    return iv
}()

@objc func handleZoomTap(_ tapGesture: UITapGestureRecognizer) {
  print("tap tap")

}

override func viewDidLoad() {
    super.viewDidLoad()

     view.addSubview(imageView)
    imageView.fillSuperview()        

}
}

当我点击图像时,我希望它至少应该打印 "tap tap" 还有什么我想念的吗?

删除这一行:

iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomTap)))

并将此添加到您的 viewDidLoad:

imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomTap)))