点击手势在collectionView Cell中不起作用,在故事板添加的单元格上都看不到任何UI元素

Posted

技术标签:

【中文标题】点击手势在collectionView Cell中不起作用,在故事板添加的单元格上都看不到任何UI元素【英文标题】:tapgesture not working in collectionViewCell, Neither any UIElements is visible on cell which is added by storyboard 【发布时间】:2017-09-01 06:33:41 【问题描述】:

我有一个 collectionViewCell 可以播放视频或显示和图像。现在单元格中的元素是以编程方式生成的。我添加了点击手势以在视频播放时切换声音。手势识别器没有被调用。我试图在故事中放置一个按钮并获得它的动作,但也没有接到电话。然后,我尝试在单元格内放置一个视图,但也没有显示。

这是我的点击手势代码:

    import UIKit
    import AVKit
    import AVFoundation

    @IBDesignable class CHCollectionImageCell: UICollectionViewCell 


        // MARK: Properties
        var imgView: UIImageView! = UIImageView()
        var screenWidth:CGFloat = 0
        var screenHeight:CGFloat = 0
        var playerLayer: AVPlayerLayer!
        let tapOnCell = UITapGestureRecognizer(target: self, action:  #selector (CHCollectionImageCell.changeMuteRegimeVideo))


        // MARK: Functions
        override func awakeFromNib() 
            super.awakeFromNib()
        

        func configureCell(insight: InsightModel) 

            imgView.removeFromSuperview()
            if playerLayer != nil 
                playerLayer.removeFromSuperlayer()
                playerLayer = nil
            
            self.removeGestureRecognizer(tapOnCell)


            if insight.isVideo 
                guard let unwrappedVideoURLString = insight.videoURL,
                    let unwrappedVideoURL = URL(string: unwrappedVideoURLString) else 
                        return
                


                let playerItem = AVPlayerItem(url: unwrappedVideoURL)
                let player = AVPlayer(playerItem: playerItem)
                playerLayer = AVPlayerLayer(player: player)
                playerLayer.frame = self.bounds
                player.isMuted = false
                layer.addSublayer(playerLayer)

                addGestureRecognizer(self.tapOnCell)
             else 
                imgView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.width)
                imgView.image = UIImage(named: "stone")
                imgView.contentMode = UIViewContentMode.scaleAspectFill
                imgView.clipsToBounds = true
                clipsToBounds = true
                addSubview(self.imgView)
            



        
       /* 
        @IBAction func tapToTurnOfSound(_ sender: Any) 
            if isInsightViedo
                if let unwrappedPlayer = playerLayer.player 
                    unwrappedPlayer.isMuted = !unwrappedPlayer.isMuted
                

            
            //Even Tried adding view as below in the cell

            //let tapView = UIView()
            //tapView.backgroundColor = ColorCodes.appThemeColor
            //self.addSubview(tapView)
            //self.bringSubview(toFront: tapView)
            //tapView.addGestureRecognizer(tapOnCell)

        
*/
        func configureCell() 
            imgView.removeFromSuperview()
            if playerLayer != nil 
                playerLayer.removeFromSuperlayer()
                playerLayer = nil
            
            self.removeGestureRecognizer(tapOnCell)
            imgView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.width)
            imgView.image = UIImage(named: "stone")
            imgView.contentMode = UIViewContentMode.scaleAspectFill
            imgView.clipsToBounds = true
            clipsToBounds = true

            addSubview(self.imgView)
        

        func changeMuteRegimeVideo() 
            if let unwrappedPlayer = playerLayer.player 
                unwrappedPlayer.isMuted = !unwrappedPlayer.isMuted
            
        

    

【问题讨论】:

我认为你没有正确添加 Tapgesture 检查这个:***.com/questions/28675209/… 这些方法我都试过了。 Tap 在 UIView 上工作得很好,但在 collectionViewCell 中却不工作 你能分享任何演示项目吗? 检查tapOnCell是否为nil,然后在contentview中添加手势。 那是因为你在错误的地方调用了点击手势。 UICollectionViewCell 是一个视图。通过视图控制器调用水龙头。 【参考方案1】:

我正在使用以下代码在我的应用程序中做同样的事情:

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(viewController.longPress(_:)))
longPressGesture.minimumPressDuration = 0.8
longPressGesture.delegate = self
collectionView.addGestureRecognizer(longPressGesture)

然后调用函数:

    func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) 

    if longPressGestureRecognizer.state == UIGestureRecognizerState.began 
      let touchPoint = longPressGestureRecognizer.location(in: collectionView)
 if eventsTableView.indexPathForRow(at: touchPoint) != nil 
 let index =  eventsTableView.indexPathForRow(at: touchPoint)//do whatever you want to do with this index 

你可以在这个函数中做任何你想做的事情。就我而言,我用它来放大集合视图中的图像

【讨论】:

请正确检查子图层。查看您是否已从单元格中删除按钮

以上是关于点击手势在collectionView Cell中不起作用,在故事板添加的单元格上都看不到任何UI元素的主要内容,如果未能解决你的问题,请参考以下文章

CollectionView的cell长按事件实现

在collectionview中的uiimage上点击手势

如何在父视图中禁用点击手势?

奇奇怪怪的Bug

iOS 手势获取cell位置点击cell获取indexpath

ios开发之-- tableview/collectionview获取当前点击的cell