# DreamParadise **Repository Path**: wlyshadow/DreamParadise ## Basic Information - **Project Name**: DreamParadise - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-03-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DreamParadise - AR Album ARKit ![image](https://github.com/SherlockQi/HKNote/blob/master/image/WeAre.gif) # DreamParadise **Technical point** >ARinitialization You can create an AR project directly when you create a new project, and xcode will create a template for the AR project.. You can also create a normal project and implement the following code to initialize in the controller that needs to implement the AR function.. ``` import ARKit let sceneView = ARSCNView() override func viewDidLoad() { super.viewDidLoad() sceneView.frame = view.bounds view.addSubview(sceneView) sceneView.delegate = self sceneView.showsStatistics = true // Create a scene, the system default is not let scene = SCNScene() sceneView.scene = scene //User is not allowed to operate the camera sceneView.allowsCameraControl = false //Anti-aliasing sceneView.antialiasingMode = .multisampling4X } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let configuration = ARWorldTrackingConfiguration() sceneView.session.run(configuration) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) sceneView.session.pause() } ``` >Add node ``` //I am using SCNPlane to act as a photo frame, or I can use SCNBox with a small "thickness". let photo = SCNPlane(width: 1, height: 1) //photo.cornerRadius = 0.01 let image = UIImage(named: "0") //Lines can make pictures or colors photo.firstMaterial?.diffuse.contents = image //photo.firstMaterial?.diffuse.contents = UIColor.red let photoNode = SCNNode(geometry: photo) //Node location let vector3 = SCNVector3Make(-1, -1, -1) photoNode.position = vector3 sceneView.scene.rootNode.addChildNode(photoNode) ``` ``` let text = SCNText(string: "Text", extrusionDepth: 0.1) text.font = UIFont.systemFont(ofSize: 0.4) let textNode = SCNNode(geometry: text) textNode.position = SCNVector3Make(0, 0, -1) //Picture/color of text text.firstMaterial?.diffuse.contents = UIImage(named: color) sceneView.scene.rootNode.addChildNode(textNode) ``` ``` Available geometry SCNText Text SCNPlane flat SCNBox box SCNPyramid Cone SCNSphere ball SCNCylinder cylinder SCNCone Cone SCNTube Cylinder SCNCapsule capsule SCNTorus Ring SCNFloor floor SCNShape customize ``` >Panorama implementation ``` Imagine yourself standing on the center of a ball, the inner surface of the ball is painted with murals, then whether the panorama is realized. So with a Sphere node wrapped around the camera node (that is, the 0 position node), and then set the inner surface texture of the Sphere node, the function is realized. let sphere = SCNSphere(radius: 15) let sphereNode = SCNNode(geometry: sphere) sphere.firstMaterial?.isDoubleSided = true sphere.firstMaterial?.diffuse.contents = image sphereNode.position = SCNVector3Zero scene.rootNode.addChildNode(sphereNode) ``` >Play video ``` let height:CGFloat = CGFloat(width) * videoSize.height/videoSize.width let box = SCNBox(width: width, height: height, length: 0.3, chamferRadius: 0) boxNode.geometry = box boxNode.geometry?.firstMaterial?.isDoubleSided = true boxNode.position = SCNVector3Make(0, 0, -5) box.firstMaterial?.diffuse.contents = UIColor.red self.scene.rootNode.addChildNode(boxNode) let avplayer = AVPlayer(url: url) avplayer.volume = rescoucceConfiguration.video_isSilence ? 0.0 : 3.0 videoPlayer = avplayer let videoNode = SKVideoNode(avPlayer: avplayer) NotificationCenter.default.addObserver(self, selector: #selector(playEnd(notify: )), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) videoNode.size = CGSize(width: 1600, height: 900) videoNode.position = CGPoint(x: videoNode.size.width/2, y: videoNode.size.height/2) videoNode.zRotation = CGFloat(Float.pi) let skScene = SKScene() skScene.addChild(videoNode) skScene.size = videoNode.size box.firstMaterial?.diffuse.contents = skScene videoNode.play() ``` >Particle effect ``` /* particleName = "bokeh.scnp" particleName = "rain.scnp" particleName = "confetti.scnp" **/ particleSytem = SCNParticleSystem(named: particleName, inDirectory: nil){ particleNode.addParticleSystem(particleSytem) particleNode.position = SCNVector3Make(0, Y, 0) self.scene.rootNode.addChildNode(particleNode) ``` >Node click event ``` //Add a click event to the scene view sceneView let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandle(gesture:))) sceneView.addGestureRecognizer(tap) ``` ``` @objc func tapHandle(gesture:UITapGestureRecognizer) { let results:[SCNHitTestResult] = (self.sceneView?.hitTest(gesture.location(ofTouch: 0, in: self.sceneView), options: nil))! guard let firstNode = results.first else{ return } // This is the node that you clicked on. You can do something for him or perform different methods based on certain properties of this node. let node = firstNode.node.copy() as! SCNNode if firstNode.node == self.selectNode { ...Push far photo... } else { ...Zoom in photo... selectNode = node } } ``` >Node animation ``` //This is just one of the methods let newPosition = SCNVector3Make(firstNode.node.worldPosition.x*2, firstNode.node.worldPosition.y*2, firstNode.node.worldPosition.z*2) let comeOut = SCNAction.move(to: newPosition, duration: 1.2) firstNode.node.runAction(comeOut) ``` >Autobiography/revolution ``` //rotation let box = SCNBox(width: boxW, height: boxW, length: boxW, chamferRadius: 0) let boxNode = SCNNode(geometry: box) boxNode.position = vector3 let emptyNode = SCNNode() emptyNode.position = SCNVector3Zero emptyNode.rotation = SCNVector4Make(0, 1, 0, Float.pi/Float(L/2) * Float(index)) emptyNode.addChildNode(boxNode) photoRingNode.addChildNode(emptyNode) let ringAction = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: right, z: 0, duration: 2)) boxNode.runAction(ringAction) //Transfer the node to a node that is autobiving. ``` >Screen recording Screen recording is done using ReplayKit Start recording ``` protocol RPScreenRecorderDelegate,RPPreviewViewControllerDelegate RPScreenRecorder.shared().startRecording(handler: nil) RPScreenRecorder.shared().delegate = self ``` Recording agent ``` func screenRecorder(_ screenRecorder: RPScreenRecorder, didStopRecordingWith previewViewController: RPPreviewViewController?, error: Error?) { if error != nil { DispatchQueue.main.async { let string = error?.localizedDescription ITTPromptView .showMessage(string, andFrameY: 0) //Failed during recording self.showFailReplay() } } } //Recording failed func showFailReplay() { let sb = UIStoryboard(name: "Main", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "HKExplainViewController") self.navigationController?.pushViewController(vc, animated: true) self.replayButtonRight.constant = 85; for button in self.smailButtons { button.alpha = 1 } self.mainButton.alpha = 1 UIView.animate(withDuration: 2.5) { self.stopReplayButton.alpha = 0 self.view.layoutIfNeeded() } } ``` End and pop up the preview controller ``` RPScreenRecorder.shared().stopRecording { (vc, error) in if error != nil { vc?.previewControllerDelegate = self vc?.title = "We Are" self.present(vc!, animated: true, completion: nil) } } ``` Preview controller's delegate ``` func previewController(_ previewController: RPPreviewViewController, didFinishWithActivityTypes activityTypes: Set) { print(activityTypes) //cancel if activityTypes.count == 0 { previewController.dismiss(animated: true, completion: nil) } //save if activityTypes.contains("com.apple.UIKit.activity.SaveToCameraRoll") { ITTPromptView .showMessage("The video has been saved in the album", andFrameY: 0) previewController.dismiss(animated: true, completion: nil) //Detected that you just saved the video, want to share let delay = DispatchTime.now() + .seconds(2) DispatchQueue.main.asyncAfter(deadline: delay) { self.outputVideo() } } } ```