Tap a UIView to full screen and tap again back to small size

Viewed 140

I have a view which contains 6 UIView vPreviews and each of them plays real-time stream video.

The target is:

  1. When tap one of each UIView, it should become full screen.
  2. Tap again, back to origin small screen layout

Code as below:

  1. custom tap gesture with value
    class FullScreenTapGesture: UITapGestureRecognizer{
        var isFullScreen: Bool = false
        var index: Int?
    }
  1. Add tap gesture recognizer in viewDidLoad()
let gesture = FullScreenTapGesture(target: self, action: #selector(fullScreenTap))
vPreviews[0].addGestureRecognizer(gesture)
gesture.index = 0
  1. fullScreenTap implementation
@objc func fullScreenTap(sender: FullScreenTapGesture){
            if sender.isFullScreen{
                print("setsmall")
                setSmallScreenLayoutConstrains()
        //                sender.isFullScreen = false
            }else{
                print("setfull")
                setFullScreenLayoutConstrains(index: sender.index!)
        //                sender.isFullScreen = true
            }
            
                    // Animate to full screen
                    UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                        self.vc.view.layoutIfNeeded()

                    }) { (finished) in
                        // ...
                    }
        }
        
        func setSmallScreenLayoutConstrains(){
            vPreviews[0].pin.top().left().width(previewWidth).height(previewHeight).margin(padding,padding,0,0)
            vPreviews[1].pin.top().after(of: vPreviews[0]).width(previewWidth).height(previewHeight).margin(padding,padding,0,0)
            vPreviews[2].pin.top().after(of: vPreviews[1]).width(previewWidth).height(previewHeight).margin(padding,padding,0,0)
            
            vPreviews[3].pin.below(of: vPreviews[0]).left().width(previewWidth).height(previewHeight).margin(padding,padding,0,0)
            vPreviews[4].pin.below(of: vPreviews[0]).after(of: vPreviews[3]).width(previewWidth).height(previewHeight).margin(padding,padding,0,0)
            vPreviews[5].pin.below(of: vPreviews[0]).after(of: vPreviews[4]).width(previewWidth).height(previewHeight).margin(padding,padding,0,0)
        }
        
        func setFullScreenLayoutConstrains(index: Int){
            self.vPreviews[index].pin.top().right().left().bottom().margin(3)
        }

However, current result is like this: when tap is detected, full screen but automatically and immediately back to small screen.

Any suggestion would be greatly appreciated!

0 Answers
Related