即使将cancelsTouchesInView设置为false,UICollectionViewCell也不会接收tap事件

时间:2014-10-01 18:05:21

标签: ios swift uicollectionviewcell uitapgesturerecognizer

我对此做了一些研究,因为我注意到我的自定义单元格没有收到tap事件。我有一个NSLog,可以在点击单元格时进行调试。我的问题是,单元格只能长时间接收事件。当我做更多的研究时,我发现创建了对UITapGestureRecognizer的引用并将cancelsTouchesInView设置为false。这样做之后,除了长按之外,它仍然没有收到该事件。视图中唯一的UITapGestureRecognizer是一组,因此当用户点击菜单外部时,它会关闭菜单。我做错了什么吗?我的意思是一个好的用户体验用户不应该按住菜单选项4-5秒。这是代码:

class MenuViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
    @IBOutlet var menuCollectionView: MenuCollectionView!
    @IBOutlet var profileViewController: ProfileViewController!
    @IBOutlet var menuTapGestureRecognizer: MenuTapGestureRecognizer!

    @IBAction func handleTap(sender: UITapGestureRecognizer)
    {
        if (sender.state == .Ended)
        {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }

    override func viewDidLoad()
    {
         super.viewDidLoad()
         // Do any additional setup after loading the view
        self.menuCollectionView.dataSource = self
        self.menuCollectionView.delegate = self
        menuTapGestureRecognizer.cancelsTouchesInView = false
    }

    // Variables
    var menuImagesArray = ["MyProfileIcon.png"]

    // Data Source Protocol
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
    {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return menuImagesArray.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        var menuCell: MenuCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MenuCell", forIndexPath: indexPath) as MenuCollectionViewCell

        var image: UIImage! = UIImage(named: menuImagesArray[indexPath.row])

        menuCell.imageView.image = image



        return menuCell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {
       NSLog("Pressed Cell")
    }
}

0 个答案:

没有答案