Swift:点按手势无法识别
Swift: tap Gesture not recognized
当我的代码被分成 类 时,我在使用点击手势时遇到了一些问题。我之前将所有这些都包含在一个文件中并且运行顺利,所以我假设我在以下代码中做错了:
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 无法识别点击,并且在点击时 return 不打印任何语句。
有什么想法吗?感谢您的帮助!
查看 placeContainerView 及其父视图框架和 userInteractionEnable。
按照您的建议更改了选择器语法
let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(self.showFullPlaceContainerViewFunction(_:)))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)
你的方法是这样的
func showFullPlaceContainerViewFunction(recognizer: UITapGestureRecognizer) {
let placeContainerView = recognizer.view as! PlaceContainerView
placeContainerView.animateExpandContractContainer()
}
期望函数的参数在哪里?
placeContainerView.userInteractionEnabled = true
let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(YourViewController.yourFunction(_:))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)
您期望的功能应该是这样的:
func yourFunction(tapGestureRecognizer:UITapGestureRecognizer) {
// Do something
}
当我的代码被分成 类 时,我在使用点击手势时遇到了一些问题。我之前将所有这些都包含在一个文件中并且运行顺利,所以我假设我在以下代码中做错了:
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 无法识别点击,并且在点击时 return 不打印任何语句。
有什么想法吗?感谢您的帮助!
查看 placeContainerView 及其父视图框架和 userInteractionEnable。
按照您的建议更改了选择器语法
let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(self.showFullPlaceContainerViewFunction(_:)))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)
你的方法是这样的
func showFullPlaceContainerViewFunction(recognizer: UITapGestureRecognizer) {
let placeContainerView = recognizer.view as! PlaceContainerView
placeContainerView.animateExpandContractContainer()
}
期望函数的参数在哪里?
placeContainerView.userInteractionEnabled = true
let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(YourViewController.yourFunction(_:))
placeContainerView.addGestureRecognizer(showFullPlaceContainerView)
您期望的功能应该是这样的:
func yourFunction(tapGestureRecognizer:UITapGestureRecognizer) {
// Do something
}