ios – 如何在故事板上单击UIImageView(swift)

前端之家收集整理的这篇文章主要介绍了ios – 如何在故事板上单击UIImageView(swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是新来的(和一般的 Xcode开发),我想知道如何在故事板上单击ImageView.我想要做的是使其点击时,它显示另一个视图控制器.

解决方法

您可以添加tapGesture.这是代码
  1. class ViewController: UIViewController {
  2.  
  3. @IBOutlet weak var imageView: UIImageView!
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. // create tap gesture recognizer
  7. let tapGesture = UITapGestureRecognizer(target: self,action: "imageTapped:")
  8.  
  9. // add it to the image view;
  10. imageView.addGestureRecognizer(tapGesture)
  11. // make sure imageView can be interacted with by user
  12. imageView.userInteractionEnabled = true
  13. }
  14.  
  15. func imageTapped(gesture: UIGestureRecognizer) {
  16. // if the tapped view is a UIImageView then set it to imageview
  17. if let imageView = gesture.view as? UIImageView {
  18. println("Image Tapped")
  19. //Here you can initiate your new ViewController
  20.  
  21. }
  22. }
  23. }
@H_404_8@Swift 3.0

  1. class ViewController: UIViewController {
  2.  
  3. @IBOutlet weak var imageView: UIImageView!
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. // create tap gesture recognizer
  7. let tapGesture = UITapGestureRecognizer(target: self,action: #selector(ViewController.imageTapped(gesture:)))
  8.  
  9. // add it to the image view;
  10. imageView.addGestureRecognizer(tapGesture)
  11. // make sure imageView can be interacted with by user
  12. imageView.isUserInteractionEnabled = true
  13. }
  14.  
  15. func imageTapped(gesture: UIGestureRecognizer) {
  16. // if the tapped view is a UIImageView then set it to imageview
  17. if (gesture.view as? UIImageView) != nil {
  18. print("Image Tapped")
  19. //Here you can initiate your new ViewController
  20.  
  21. }
  22. }
  23. }

猜你在找的iOS相关文章