0

I have a collectionViewCell that either plays a video or displays and image.Now the elements in the cell are generated programatically. I added tap gesture to toggle the sound when video play. The gesture recognizer wasn't getting called. I tried to place a button in story and get its action, that also didn't recieve a call. Then, I tried to place a view inside the cell, that also didn't display.

Here is my code with tap gesture:

    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
            }
        }

    }
Ishika
  • 2,187
  • 1
  • 17
  • 30

1 Answers1

1

Iam doing the same thing in my application by using the following code :

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

and then call the function:

    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 
}}}

you can do whatever you want to do in this function. In my case i used this to enlarge the image in the collection view

Hakikat Singh
  • 285
  • 2
  • 11