Search code examples
iosswiftresetsubviewaddsubview

How to reset subviews?


On push of a button, I have a subview starting to play a video using the following:

@IBAction func cardboardButton(_ sender: UIButton) {

    if let button = self.videoVRView.subviews[2] as? UIButton {
        button.sendActions(for: .touchUpInside)


               }

}

which works like a charm, it launche a specific video player.

When quitting thtat subview, id like to reset it. At the moment, if i quite the subview, and then relaunch it, it start the video from where i left it. I need to reset that subview basically to make restart the video from 0.

I tried the following:

    @IBAction func cardboardButton(_ sender: UIButton) {

    if let button = self.videoVRView.subviews[2] as? UIButton {
        button.sendActions(for: .touchUpInside)
        let parent = self.videoVRView.subviews[2]
        self.videoVRView.removeFromSuperview()
        self.videoVRView = nil
        parent?.addSubview(self.videoVRView)

               }

}

Without any success...

Does anybody have any tips for that?


Solution

  • Are you using AVPlayer? AVPlayer has the method: seekTo() where you can go a specific second of the video. This way it always starts on 0.

    I am not sure what you are trying to do with your code here `

    self.videoVRView.removeFromSuperview()
    self.videoVRView = nil
    parent?.addSubview(self.videoVRView)`
    

    Yes, you are removing the view from super view and then adding it again but does the player get destroyed and created and set up again? Because that is what will make it start from the beginning. You are adding the view but you are not creating it again, are you? it is set to nil.