UIImageView UITapGestureRecognizer无法正常工作

最后发布: 2016-09-04 16:26:19


问题

点击手势事件未调用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 ! ")
    }
}    
ios swift uiimageview uitapgesturerecognizer
回答

我建议使用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 ! ")
    }


}


回答

Okey,所以我以某种方式工作了。 我要做的就是在我的LoginViewController中添加视图控制器,如下所示:

self.addChildViewController(myVC1)

而不是使用view.addSubview(container)

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