使图像可单击以展开

时间:2019-02-18 03:43:43

标签: xcode imageview uigesturerecognizer uitapgesturerecognizer

我有一个背景图片,我想在单击该图片时对其进行扩展。我有一些设置动画,可以使背景变暗并将图像支撑到中心,但是由于某些原因,图像不可单击。

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

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” 还有其他我想念的东西吗?

1 个答案:

答案 0 :(得分:0)

删除此行:

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

并将其添加到您的viewDidLoad

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