如何解除Swift中的预览UI控制器?

时间:2016-04-11 19:16:22

标签: ios swift

我不明白为什么不解雇。我在我的应用程序中使用replaykit,我试图解除在完成录制屏幕后弹出的UI。左上方有一个取消按钮,当我按下它时,预览控制器不会消失。有一个委托功能来解除控制器,但它不适合我。我觉得这很容易解决,但我不确定我做错了什么。请帮我。如果您需要更多信息,请告诉我们。谢谢!

 func startRecoding() {


if RPScreenRecorder.sharedRecorder().available {
    RPScreenRecorder.sharedRecorder().startRecordingWithMicrophoneEnabled(true, handler: { (error: NSError?) -> Void in
        if error == nil { // Recording has started

        } else {
            // Handle error
        }
    })
} else {
    // Display UI for recording being unavailable

}


}

 func stopRecording() {

RPScreenRecorder.sharedRecorder().stopRecordingWithHandler { (previewController: RPPreviewViewController?, error: NSError?) -> Void in

    if previewController != nil {

        let alertController = UIAlertController(title: "Recording", message: "Do you wish to discard or view your gameplay recording?", preferredStyle: .Alert)

        let discardAction = UIAlertAction(title: "Discard", style: .Default) { (action: UIAlertAction) in
            RPScreenRecorder.sharedRecorder().discardRecordingWithHandler({ () -> Void in
                // Executed once recording has successfully been discarded
            })
        }

        let viewAction = UIAlertAction(title: "View", style: .Default, handler: { (action: UIAlertAction) -> Void in
            self.view?.window?.rootViewController?.presentViewController(previewController!, animated: true, completion: nil)



        })

        alertController.addAction(discardAction)
        alertController.addAction(viewAction)

        self.view?.window?.rootViewController!.presentViewController(alertController, animated: true, completion: nil)

    } else {
        // Handle error
    }
  }



  }


func previewControllerDidFinish(previewController: RPPreviewViewController) {
    previewController.dismissViewControllerAnimated(true, completion: nil)

}

1 个答案:

答案 0 :(得分:2)

您需要确保为预览视图控制器设置委托。

RPScreenRecorder.sharedRecorder().stopRecordingWithHandler { (previewController: RPPreviewViewController?, error: NSError?) -> Void in

    if previewController != nil {

        //  Try adding this line
        previewController.delegate = self
        //

        let alertController = UIAlertController(title: "Recording", message: "Do you wish to discard or view your gameplay recording?", preferredStyle: .Alert)
        .
        .
        .