在 UiCollectionView 中处理触摸?
Handle Touch in UiCollectionView?
我有这么大的 UICollectionView
占据了屏幕的主要部分,还有一个显示菜单的 UIButton
。当用户点击屏幕的任何一侧时,我想隐藏菜单,不幸的是,这对我来说成为 UICollectionView
的任何部分。在其他视图上尝试了以下代码,它运行良好...但不适用于 UICollectionView
。该函数未被调用。
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
hidemenu()
self.view.endEditing(true)
}
问题是什么?感谢您的宝贵时间。
或
如何在 UICollectionView
所在的 UIViewController
中触发 touchesBegan
您可以直接使用此方法,每当用户点击 collection 视图的任何部分时,您的菜单将自动隐藏:-
func collectionView(_ collectionView: UICollectionView,
didSelectItemAtIndexPath indexPath: NSIndexPath)
{
hidemenu()
self.view.endEditing(true)
}
要检测点击单元格之间的空间,这里是代码
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:self.collectionView.frame];
self.collectionView.backgroundView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
self.collectionView.backgroundView.gestureRecognizers = @[tapRecognizer];
虽然点击滚动控制器(如 UIScrollView、UITableView、UICollection 等)不会调用 touchesBegan
方法。因为他们有自己的 selector
方法。要处理这种情况,您需要在 UICollectionView
上创建 UITapGesture
。在点击 UICollectionView
时,它的选择器方法会调用并执行您想要的操作。
这里有 link 指导。如何在 UICollectionView
上创建双击手势。借助于此,您还可以创建单个点击手势。
Collection View + Double Tap Gesture
编辑:
进行以下更改,效果很好。
步骤 1:在 SwipeMenuViewController
.
中声明 handleTap
func handleTap(sender: UITapGestureRecognizer) {
println("called swipe")
}
步骤 2 : 创建 SwipeMenuViewController
控制器的全局变量。在 viewDidLoad()
之外
var vc2 = SwipeMenuViewController()
步骤 3:在 viewDidLoad()
中声明 TapGesture
var tap = UITapGestureRecognizer(target: vc2, action : "handleTap:")
tap.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tap)
输出 :
调用了刷卡
希望对您有所帮助。
您可以将 UITapGestureRecognizer 添加到 UICollectionView 并创建一个操作以在用户触摸您的集合之外或任何地方时关闭视图。
步骤 1 创建用于关闭视图的点击手势
override func viewDidLoad() {
super.viewDidLoad()
...
let tap = UITapGestureRecognizer(target: self, action: #selector(didTapOutsideCollectionView(recognizer:)))
tap.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tap)
}
第 2 步 点击位置执行您想要的操作
@objc func didTapOutsideCollectionView(recognizer: UITapGestureRecognizer){
let tapLocation = recognizer.location(in: self.view)
//The point is outside of collection cell
if collectionView.indexPathForItem(at: tapLocation) == nil {
dismiss(animated: true, completion: nil)
}
}
我有这么大的 UICollectionView
占据了屏幕的主要部分,还有一个显示菜单的 UIButton
。当用户点击屏幕的任何一侧时,我想隐藏菜单,不幸的是,这对我来说成为 UICollectionView
的任何部分。在其他视图上尝试了以下代码,它运行良好...但不适用于 UICollectionView
。该函数未被调用。
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
hidemenu()
self.view.endEditing(true)
}
问题是什么?感谢您的宝贵时间。
或
如何在 UICollectionView
所在的 UIViewController
中触发 touchesBegan
您可以直接使用此方法,每当用户点击 collection 视图的任何部分时,您的菜单将自动隐藏:-
func collectionView(_ collectionView: UICollectionView,
didSelectItemAtIndexPath indexPath: NSIndexPath)
{
hidemenu()
self.view.endEditing(true)
}
要检测点击单元格之间的空间,这里是代码
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:self.collectionView.frame];
self.collectionView.backgroundView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
self.collectionView.backgroundView.gestureRecognizers = @[tapRecognizer];
虽然点击滚动控制器(如 UIScrollView、UITableView、UICollection 等)不会调用 touchesBegan
方法。因为他们有自己的 selector
方法。要处理这种情况,您需要在 UICollectionView
上创建 UITapGesture
。在点击 UICollectionView
时,它的选择器方法会调用并执行您想要的操作。
这里有 link 指导。如何在 UICollectionView
上创建双击手势。借助于此,您还可以创建单个点击手势。
Collection View + Double Tap Gesture
编辑: 进行以下更改,效果很好。
步骤 1:在 SwipeMenuViewController
.
handleTap
func handleTap(sender: UITapGestureRecognizer) {
println("called swipe")
}
步骤 2 : 创建 SwipeMenuViewController
控制器的全局变量。在 viewDidLoad()
var vc2 = SwipeMenuViewController()
步骤 3:在 viewDidLoad()
TapGesture
var tap = UITapGestureRecognizer(target: vc2, action : "handleTap:")
tap.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tap)
输出 :
调用了刷卡
希望对您有所帮助。
您可以将 UITapGestureRecognizer 添加到 UICollectionView 并创建一个操作以在用户触摸您的集合之外或任何地方时关闭视图。
步骤 1 创建用于关闭视图的点击手势
override func viewDidLoad() {
super.viewDidLoad()
...
let tap = UITapGestureRecognizer(target: self, action: #selector(didTapOutsideCollectionView(recognizer:)))
tap.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tap)
}
第 2 步 点击位置执行您想要的操作
@objc func didTapOutsideCollectionView(recognizer: UITapGestureRecognizer){
let tapLocation = recognizer.location(in: self.view)
//The point is outside of collection cell
if collectionView.indexPathForItem(at: tapLocation) == nil {
dismiss(animated: true, completion: nil)
}
}