回到以前的控制器,没有'点击'iOS - Swift

时间:2017-02-23 12:56:58

标签: ios swift segue uistoryboardsegue

我在这里,在我的iOS应用程序(使用swift运行)上,单击我的“详细信息控制器”中的按钮,它将我带到一个新的“视频控制器”,默认情况下播放全屏视频。

关闭视频后,它会将用户带回这个新的“视频控制器”。

我希望默认情况下将用户带回“详细信息控制器”,而不是“视频控制器”

是否可以让segue在没有任何推动的情况下自动运行?或任何其他解决方案?这怎么可行?

非常感谢你们!

- 编辑这是我的'视频控制器'的代码

 import UIKit
 import FirebaseAuth
 import FirebaseDatabase
 import FirebaseStorage
 import SwiftKeychainWrapper
 import SwiftyJSON

 class VideoController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {



@IBOutlet weak var imageLabel: UILabel!
@IBOutlet weak var videoLabel: UILabel!
@IBOutlet weak var videoVRView: GVRVideoView!
@IBOutlet weak var imageVRView: GVRPanoramaView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UITextView!
@IBOutlet weak var textView: UITextView!


enum Media {

    static var photoArray = ["test.jpg"]

}


var currentView: UIView?
var currentDisplayMode = GVRWidgetDisplayMode.fullscreenVR
var isPaused = true
var isstop = true


override func viewDidLoad() {
    super.viewDidLoad()


    imageLabel.isHidden = true
    imageVRView.isHidden = true
    videoLabel.isHidden = true
    videoVRView.isHidden = true

    imageVRView.load(UIImage(named: Media.photoArray.first!),
                     of: GVRPanoramaImageType.mono)
    imageVRView.enableCardboardButton = true

    videoVRView.load(from: URL(string: "\(posts[selectedIndexPath].link)"))
    videoVRView.enableCardboardButton = true
    videoVRView.enableFullscreenButton = true
    videoVRView.delegate = self
    videoVRView.displayMode = GVRWidgetDisplayMode.fullscreen
    textView.isEditable = false
    textView.isSelectable = false
    titleLabel.isEditable = false
    titleLabel.isSelectable = false


    textView.text = posts[selectedIndexPath].caption
    titleLabel.text = posts[selectedIndexPath].title

    Storage.getImage(with: posts[selectedIndexPath].imageDetails){
        postPic in
        self.imageView.image = postPic

    }

}

override var prefersStatusBarHidden: Bool {
    return true
}




@IBAction func closeButton(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}



func refreshVideoPlayStatus() {
    if currentView == videoVRView && currentDisplayMode != GVRWidgetDisplayMode.embedded {
        videoVRView?.resume()
        isPaused = false
    } else {
        videoVRView?.pause()
        isPaused = true
    }
}

func setCurrentViewFromTouch(touchPoint point:CGPoint) {
    if imageVRView!.frame.contains(point) {
        currentView = imageVRView
    } else  if videoVRView!.frame.contains(point) {
        currentView = videoVRView
    }
}
}

extension VideoController: GVRWidgetViewDelegate {
 func widgetView(_ widgetView: GVRWidgetView!, didLoadContent content: Any!) {
    if content is UIImage {
        imageVRView.isHidden = false
        imageLabel.isHidden = false
    } else if content is NSURL {
        videoVRView.isHidden = true
        videoLabel.isHidden = false
        refreshVideoPlayStatus()

    }
 }

 func widgetView(_ widgetView: GVRWidgetView!, didFailToLoadContent content: Any!, withErrorMessage errorMessage: String!)  {
    print(errorMessage)
 }

 func widgetView(_ widgetView: GVRWidgetView!, didChange displayMode: GVRWidgetDisplayMode) {
    currentView = widgetView
    currentDisplayMode = displayMode
    refreshVideoPlayStatus()
    if currentView == imageVRView && currentDisplayMode != GVRWidgetDisplayMode.embedded {
        view.isHidden = true
    } else {
        view.isHidden = false
    }
 }

func widgetViewDidTap(_ widgetView: GVRWidgetView!) {
    guard currentDisplayMode != GVRWidgetDisplayMode.embedded else {return}
    if currentView == imageVRView {
        Media.photoArray.append(Media.photoArray.removeFirst())
        imageVRView?.load(UIImage(named: Media.photoArray.first!), of: GVRPanoramaImageType.mono)
    } else {
        if isPaused {
            videoVRView?.resume()
        } else {
            videoVRView?.pause()
        }
        isPaused = !isPaused
    }
}

}

 extension VideoController: GVRVideoViewDelegate {
func videoView(_ videoView: GVRVideoView!, didUpdatePosition position: TimeInterval) {
    if currentView == videoVRView && currentDisplayMode != GVRWidgetDisplayMode.embedded {
        videoVRView?.resume()
        isPaused = false
    } else {
        videoVRView?.pause()
        isPaused = true
    }
}
func videotwoView(_ videoView: GVRVideoView!, didUpdatePosition position: TimeInterval) {
    OperationQueue.main.addOperation() {
        if position >= videoView.duration() {
            videoView.seek(to: 0)
            videoView.resume()
        }
    }
}

 }


 class TouchVideoView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    if let VideoController = viewController() as? VideoController , event?.type == UIEventType.touches {
        VideoController.setCurrentViewFromTouch(touchPoint: point)
    }
    return true
}

func viewController() -> UIViewController? {
    if self.next!.isKind(of: VideoController.self) {
        return self.next as? UIViewController
    } else {
        return nil
    }
}

我正在通过以下方式自动启动视频全屏:

        videoVRView.displayMode = GVRWidgetDisplayMode.fullscreen

2 个答案:

答案 0 :(得分:0)

是的,你可以。如果您使用AVPlayer播放视频,则可以执行

let videoPlayer = AVPlayer(URL: url)       

NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")), 
       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: videoPlayer.currentItem)

func playerDidFinishPlaying(note: NSNotification) {
   // call segue to move other view controller 
}

答案 1 :(得分:0)

注册MPMoviePlayerPlaybackStateDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(MPMoviePlayerPlaybackStateDidChange:) 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                               object:nil];

在视频播放完毕后编写代码以弹出到上一个控制器。检查playbackState后。

- (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification
    {
      if (player.playbackState == MPMoviePlaybackStatePlaying)
      { //playing
      }
      if (player.playbackState == MPMoviePlaybackStateStopped)
      { //stopped
      }if (player.playbackState == MPMoviePlaybackStatePaused)
      { //paused
      }if (player.playbackState == MPMoviePlaybackStateInterrupted)
      { //interrupted
      }if (player.playbackState == MPMoviePlaybackStateSeekingForward)
      { //seeking forward
      }if (player.playbackState == MPMoviePlaybackStateSeekingBackward)
      { //seeking backward
      }

}