gpt4 book ai didi

ios - 无法让 UITapGestureRecognizer 识别 Controller 中的特定 View

转载 作者:行者123 更新时间:2023-11-28 13:20:34 29 4
gpt4 key购买 nike

我有一个菜单 Controller ,当它弹出时有一个 Collection View 。它的工作方式是,当用户在 Collection View 外点击时,它应该关闭菜单。当您在 Collection View 外部点击时,菜单会消失,但当您在 Collection View 内部点击时,它也会消失。我似乎无法让手势识别器仅在不在 Collection View 内时才关闭。我在这里和谷歌上四处张望,但还没有找到任何可行的解决方案。下面是处理点击的函数的代码:

    @IBAction func handleTap(sender: MenuTapGestureRecognizer)
{
if (sender.state == .Ended && sender.view == menuCollectionView)
{
NSLog("menuCollectionView")
}
else if (sender.state == .Ended && sender.view == mainView)
{
self.dismissViewControllerAnimated(true, completion: nil)
}

}

mainView 是连接到最顶层 View 的引用,menuCollectionView 是对 UICollectionView 的引用。

最佳答案

我怀疑您只将手势识别器附加到您的 mainView。这很好,但您需要使用 sender.locationInView(menuCollectionView) 并检查该点是否在您的 Collection View 范围内。这是通过以下方式完成的:

if (CGRectContainsPoint(menuCollectionView.bounds, sender.locationInView(menuCollectionView))) {
// do stuff.
}

关于ios - 无法让 UITapGestureRecognizer 识别 Controller 中的特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282231/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com