ゲームに実装ReplayKit
しましたSpriteKit
が、すべてがGameViewController
記録ボタン内で行われるため、表示されるのが早すぎます。GameViewController
以下の私のクラスをご覧ください:
class GameViewController: UIViewController, RPPreviewViewControllerDelegate {
var videoRecButton: UIButton!
var videoRecImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
let skView = self.view as? SKView
if skView?.scene == nil {
skView?.showsFPS = true
skView?.showsNodeCount = true
skView?.showsPhysics = true
skView?.ignoresSiblingOrder = false
//starting the game with the Poster Scene
let posterScene = PosterScene(size: skView!.bounds.size)
posterScene.scaleMode = .aspectFill
skView?.presentScene(posterScene)
}
videoRecButton = UIButton(type: .custom)
videoRecImage = UIImage(named:"videoRecButton.png")
videoRecButton.frame = CGRect(x:0, y: 0, width: (videoRecImage?.size.width)!, height: (videoRecImage?.size.height)!)
videoRecButton.setImage(videoRecImage, for: .normal)
videoRecButton.addTarget(self, action:#selector(self.videoRecButtonClicked), for: .touchUpInside)
self.view.addSubview(videoRecButton)
}
func videoRecButtonClicked() {
print("Button Clicked")
startRecording()
}
func startRecording() {
let recorder = RPScreenRecorder.shared()
recorder.startRecording{ [unowned self] (error) in
if let unwrappedError = error {
print(unwrappedError.localizedDescription)
} else {
self.videoRecButton.addTarget(self, action:#selector(self.stopRecording), for: .touchUpInside)
}
}
}
func stopRecording() {
let recorder = RPScreenRecorder.shared()
recorder.stopRecording { [unowned self] (preview, error) in
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .plain, target: self, action: #selector(self.startRecording))
if let unwrappedPreview = preview {
unwrappedPreview.previewControllerDelegate = self
self.present(unwrappedPreview, animated: true)
}
}
}
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
dismiss(animated: true)
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}
likeクラスから継承するクラスから
startRecording
and関数を呼び出すにはどうすればよいですか?stopRecording
SKScene
GameScene
クラス
videoRecButton
からボタンを有効、無効、非表示にするにはどうすればよいですか?GameScene
アップデート
crashoverride777 からの回答に基づいて、SKScene
クラスに次のコードを配置しましたが、録画されたビデオのプレビューを含むナビゲーション コントローラーが表示される前に、画面がわずか数秒間記録されます。録画されたビデオはただの黒い画面で、キャンセル ボタンと保存ボタンは反応しません。
func startRecording() {
let recorder = RPScreenRecorder.shared()
if #available(iOS 10.0, *) {
recorder.startRecording{ [unowned self] (error) in
if let unwrappedError = error {
print(unwrappedError.localizedDescription)
} else {
self.stopRecording()
}
}
} else {
// Fallback on earlier versions
}
}
func stopRecording() {
let recorder = RPScreenRecorder.shared()
recorder.stopRecording { [unowned self] (preview, error) in
self.view?.window?.rootViewController?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .plain, target: self, action: #selector(self.startRecording))
if let unwrappedPreview = preview {
unwrappedPreview.previewControllerDelegate = self
self.view?.window?.rootViewController?.present(unwrappedPreview, animated: true)
}
}
}
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
view?.window?.rootViewController?.dismiss(animated: true)
}
録音ボタンを作成しました:
let videoRecButtonSprite = SKSpriteNode(imageNamed: "videoButton")
videoRecButtonSprite.position = CGPoint(x: self.frame.width/15, y: self.frame.height - self.frame.height/12)
self.addChild(videoRecButtonSprite)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
if videoRecButtonSprite.contains(location){
startRecording()
}
}
}