简体   繁体   English

Swift:无法识别点击手势

[英]Swift: tap Gesture not recognized

I am having some trouble getting my tap gesture to work when my code is separated into classes. 当我的代码分为几类时,我无法使点击手势起作用。 I had this all contained in one file earlier and it worked smoothly so I assume I did something wrong in the following code: 我早先将所有这些文件包含在一个文件中,并且工作顺利,因此我认为我在以下代码中做错了什么:

placeContainerView.userInteractionEnabled = true

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

where the function showFullContainerViewFunction(placeContainerView) 函数showFullContainerViewFunction(placeContainerView)

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

and

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)
    }

}

Somehow the placeContainerView is not recognising the taps and does not return any print statements when clicked. 以某种方式,placeContainerView无法识别点击,并且在单击时不返回任何打印语句。

Any ideas? 有任何想法吗? Thanks for your help! 谢谢你的帮助!

查看placeContainerView及其超级视图框架和userInteractionEnable。

Changed Selector syntax as you suggested 根据您的建议更改了选择器语法

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

Your method will be like this 您的方法将是这样

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

Where is the parameter of the expected function? 期望函数的参数在哪里?

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

Your expected function should be like this: 您期望的功能应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM