Swift:点击手势无法识别

时间:2016-07-25 23:56:33

标签: ios swift uigesturerecognizer

当我的代码被分成类时,我在使用自来水手势时遇到了一些麻烦。我之前已将这一切都包含在一个文件中并且运行顺利,因此我假设我在以下代码中做错了一些:

placeContainerView.userInteractionEnabled = true

let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: Selector(self.showFullPlaceContainerViewFunction(placeContainerView)))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)

函数showFullContainerViewFunction(placeContainerView)

func showFullPlaceContainerViewFunction(placeContainerView: PlaceContainerView) {
    placeContainerView.animateExpandContractContainer()   
}

func animateExpandContractContainer() {
    print("Tap gesture working")
    if self.displayingPlaceLabel == false {
        print(self.displayingPlaceLabel)
        self.displayingPlaceLabel = true

        UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
            self.center.x += 180
            }, completion: nil)
    } else {
        self.displayingPlaceLabel = false
        UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
            self.center.x -= 180
            }, completion: nil)
    }

}

以某种方式,placeContainerView无法识别点击,并且在点击时不会返回任何打印语句。

有什么想法吗?谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

查看placeContainerView及其superview框架和userInteractionEnable。

答案 1 :(得分:2)

按照建议更改了选择器语法

let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(self.showFullPlaceContainerViewFunction(_:)))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)

你的方法将是这样的

func showFullPlaceContainerViewFunction(recognizer: UITapGestureRecognizer) {
    let placeContainerView = recognizer.view as! PlaceContainerView
    placeContainerView.animateExpandContractContainer()   
}

答案 2 :(得分:0)

预期函数的参数在哪里?

placeContainerView.userInteractionEnabled = true
let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(YourViewController.yourFunction(_:))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)

您的预期功能应该是这样的:

func yourFunction(tapGestureRecognizer:UITapGestureRecognizer) {
   // Do something
}