UIImageView UITapGestureRecognizer无法正常工作

时间:2016-09-04 16:26:20

标签: ios swift uiimageview uitapgesturerecognizer

点击手势事件而不是gettint被调用。我可能做错了什么,但请看看(我尝试将识别器添加到self.view但仍然没有运气):

LoginViewController

class LoginViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let container = UIView() 

        container.heightAnchor.constraintEqualToConstant(50).active = true

        let myVC1 = MyViewController()
        let myVC2 = MyViewController()
        let myVC3 = MyViewController()
        let myVC4 = MyViewController()

        let myStackView = UIStackView(arrangedSubviews: [myVC1.view, myVC2.view, myVC3.view, myVC4.view])
        myStackView.spacing = 10
        myStackView.alignment = .Fill
        myStackView.distribution = .EqualSpacing

        container.addSubview(myStackView)
        view.addSubview(container)
    }
}

MyViewController

class MyViewController: UIViewController, UIGestureRecognizerDelegate {

    let ImageView = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()
        ImageView.backgroundColor = UIColor.blueColor()
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(pressedSocialMediaItem(_:)))
        tapGestureRecognizer.delegate = self
        tapGestureRecognizer.numberOfTapsRequired = 1

        ImageView.userInteractionEnabled = true
        ImageView.addGestureRecognizer(tapGestureRecognizer)

        view.addSubview(ImageView)
    }

    func pressedSocialMediaItem(sender : UITapGestureRecognizer) { 
        print("PRESSED ! ")
    }
}    

2 个答案:

答案 0 :(得分:1)

我建议使用MyViewController.pressedSocialMediaItem,因此您的代码应该像这样工作。

 class MyViewController: UIViewController, UIGestureRecognizerDelegate {

        let ImageView = UIImageView()

        override func viewDidLoad() {
            super.viewDidLoad()
            ImageView.backgroundColor = UIColor.blueColor()
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyViewController.pressedSocialMediaItem(_:)))
            tapGestureRecognizer.delegate = self
            tapGestureRecognizer.numberOfTapsRequired = 1

            ImageView.userInteractionEnabled = true
            ImageView.addGestureRecognizer(tapGestureRecognizer)

            view.addSubview(ImageView)
        }

        func pressedSocialMediaItem(sender : UITapGestureRecognizer) { 
            print("PRESSED ! ")
        }
    } 

我在我的机器上测试过以下内容并且可以正常运行:

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {


    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.backgroundColor = UIColor.blueColor()
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.pressedSocialMediaItem(_:)))
        tapGestureRecognizer.delegate = self
        tapGestureRecognizer.numberOfTapsRequired = 1

        imageView.userInteractionEnabled = true
        imageView.addGestureRecognizer(tapGestureRecognizer)

        view.addSubview(imageView)
    }

    func pressedSocialMediaItem(sender : UITapGestureRecognizer) {
        print("PRESSED ! ")
    }


}

答案 1 :(得分:0)

哦,好吧,我让它以某种方式工作。我需要做的就是在我的LoginViewController中添加视图控制器,如下所示:

self.addChildViewController(myVC1)

而不是使用view.addSubview(container)

注意:我不确定这是否是正确的做法,如果不是,请发表评论。同时它解决了我的问题。