问题描述
我有这个很大的 UICollectionView
占据了屏幕的大部分,还有一个 UIButton
显示了一个菜单.我想在用户点击屏幕的任何一侧时隐藏菜单,不幸的是,这对我来说是 UICollectionView
的任何部分.在其他视图上试过以下代码,效果很好...但不适用于 UICollectionView
.该函数不会被调用.
覆盖功能func touchesBegan(touches:Set< NSObject>,withEvent事件:UIEvent){hidemenu()self.view.endEditing(true)}
出什么问题了?谢谢您的宝贵时间.
OR 如何在 UICollectionView
所在的 UIViewController
中触发 touchesBegan
在滚动控制器(如UIScrollView,UITableView,UICollection等)上点击时不会调用 touchesBegan
方法.因为它们有自己的 selector
方法.要处理这种情况,您需要在 UICollectionView
上创建 UITapGesture
.点击 UICollectionView
时,其选择器方法将调用并执行您想要的操作.
以下是指导您的链接.如何在 UICollectionView
上创建双击手势.借助此功能,您还可以创建单个Tap手势.
进行以下更改,就可以正常工作.
第1步:在 SwipeMenuViewController
中声明 handleTap
.
func handleTap(发送方:UITapGestureRecognizer){println(称为滑动")}
第2步:创建 SwipeMenuViewController
控制器的全局变量.在 viewDidLoad()
var vc2 = SwipeMenuViewController()
第3步:在 viewDidLoad()
TapGesture
var tap = UITapGestureRecognizer(目标:vc2,操作:"handleTap:")tap.numberOfTapsRequired = 1self.collectionView.addGestureRecognizer(点击)
输出:
称为滑动
希望这对您有所帮助.
I have this big UICollectionView
occupying major part of screen and there is a UIButton
that shows a menu. I want to hide the menu when the user taps on any side of the screen which becomes unfortunately any part of UICollectionView
for me. Tried on other view the below code, it works well...but not for UICollectionView
. The function does not gets called.
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
hidemenu()
self.view.endEditing(true)
}
What is the problem? Thanks for your time.
ORHow to trigger touchesBegan
in the UIViewController
where UICollectionView
resides
While tapping on scrolling controllers (like UIScrollView, UITableView, UICollection etc) does not call touchesBegan
method. because they have their own selector
method. To handle such situation, you need to create UITapGesture
on UICollectionView
. While tapping on UICollectionView
, its selector method called and do what ever you want.
Here are the link that guide you. how to create double Tap Gesture on UICollectionView
. with help of this you can created single Tap gesture as well.
Collection View + Double Tap Gesture
Edit : Do the following changes, it work fine.
Step 1 : Declare handleTap
in SwipeMenuViewController
.
func handleTap(sender: UITapGestureRecognizer) {
println("called swipe")
}
Step 2 : Create global variable of SwipeMenuViewController
controller. that is out side of viewDidLoad()
var vc2 = SwipeMenuViewController()
Step 3 : Declare TapGesture
in viewDidLoad()
var tap = UITapGestureRecognizer(target: vc2, action : "handleTap:")
tap.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tap)
Output :
called swipe
Hope this help you.
这篇关于在UiCollectionView中处理触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!