tryingtolearn tryingtolearn - 4 months ago 14
Swift Question

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)


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.

Any ideas? Thanks for your help!

Answer

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