处理手机硬件锁屏时,通过coremotion监听屏幕旋转变化

This commit is contained in:
bluesea 2024-03-22 21:07:22 +08:00
parent d5fcf1d37c
commit b834ecf8dd
3 changed files with 121 additions and 283 deletions

View File

@ -11,6 +11,7 @@
005580782B9F1525004B9567 /* ZZHHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 005580772B9F1525004B9567 /* ZZHHelper.swift */; };
009661F82BAD6C7100FCA65F /* CCSpaceAlbumFilterPopView2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 009661F72BAD6C7100FCA65F /* CCSpaceAlbumFilterPopView2.swift */; };
009661FA2BAD876200FCA65F /* PhotosUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 009661F92BAD876200FCA65F /* PhotosUI.framework */; };
009661FC2BADB20D00FCA65F /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 009661FB2BADB20D00FCA65F /* CoreMotion.framework */; };
00B946232B67B26D00DA668F /* ddd_video.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 00B946222B67B26D00DA668F /* ddd_video.mp4 */; };
00B946252B67B7DE00DA668F /* CCSpatialPlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00B946242B67B7DE00DA668F /* CCSpatialPlayView.swift */; };
00C665732BAA81F900C309C3 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 00C665722BAA81F900C309C3 /* GoogleService-Info.plist */; };
@ -101,6 +102,7 @@
005580772B9F1525004B9567 /* ZZHHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZZHHelper.swift; sourceTree = "<group>"; };
009661F72BAD6C7100FCA65F /* CCSpaceAlbumFilterPopView2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CCSpaceAlbumFilterPopView2.swift; sourceTree = "<group>"; };
009661F92BAD876200FCA65F /* PhotosUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PhotosUI.framework; path = System/Library/Frameworks/PhotosUI.framework; sourceTree = SDKROOT; };
009661FB2BADB20D00FCA65F /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = System/Library/Frameworks/CoreMotion.framework; sourceTree = SDKROOT; };
00B946222B67B26D00DA668F /* ddd_video.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = ddd_video.mp4; sourceTree = "<group>"; };
00B946242B67B7DE00DA668F /* CCSpatialPlayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CCSpatialPlayView.swift; sourceTree = "<group>"; };
00C665722BAA81F900C309C3 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
@ -203,6 +205,7 @@
AF2120D42B4E99FA00400B7F /* MobileCoreServices.framework in Frameworks */,
009661FA2BAD876200FCA65F /* PhotosUI.framework in Frameworks */,
AF2120D62B4E9A0100400B7F /* Photos.framework in Frameworks */,
009661FC2BADB20D00FCA65F /* CoreMotion.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -317,6 +320,7 @@
4D8F3A411661D600CD07B39B /* Frameworks */ = {
isa = PBXGroup;
children = (
009661FB2BADB20D00FCA65F /* CoreMotion.framework */,
009661F92BAD876200FCA65F /* PhotosUI.framework */,
00EB2D042BA2D59000924D84 /* MessageUI.framework */,
AF478B022B577D51005C35A5 /* AVFoundation.framework */,

View File

@ -217,84 +217,6 @@
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "97D73D5E-52DA-4635-81F8-B4BED96FB19E"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "420"
endingLineNumber = "420"
landmarkName = "configureSession()"
landmarkType = "7">
<Locations>
<Location
uuid = "97D73D5E-52DA-4635-81F8-B4BED96FB19E - 928cb49c86da05db"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.CCSpatialShootController.configureSession() -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "406"
endingLineNumber = "406"
offsetFromSymbolStart = "6184">
</Location>
<Location
uuid = "97D73D5E-52DA-4635-81F8-B4BED96FB19E - 928cb49c86da05f8"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.CCSpatialShootController.configureSession() -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "407"
endingLineNumber = "407"
offsetFromSymbolStart = "5852">
</Location>
<Location
uuid = "97D73D5E-52DA-4635-81F8-B4BED96FB19E - 928cb49c86da04c2"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.CCSpatialShootController.configureSession() -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "413"
endingLineNumber = "413"
offsetFromSymbolStart = "6472">
</Location>
<Location
uuid = "97D73D5E-52DA-4635-81F8-B4BED96FB19E - 928cb49c86d9fbad"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.CCSpatialShootController.configureSession() -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "420"
endingLineNumber = "420"
offsetFromSymbolStart = "6472">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
@ -589,162 +511,6 @@
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "679B28C9-4889-4546-A3C6-246F045B6896"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "85"
endingLineNumber = "85"
landmarkName = "writeSpatialVideo(leftEyeVideoURL:rightEyeVideoURL:outputVideoURL:completion:)"
landmarkType = "7">
<Locations>
<Location
uuid = "679B28C9-4889-4546-A3C6-246F045B6896 - ad91d10e3e2f72cf"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "92"
endingLineNumber = "92"
offsetFromSymbolStart = "4848">
</Location>
<Location
uuid = "679B28C9-4889-4546-A3C6-246F045B6896 - ad91d10e3e2f73d7"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "84"
endingLineNumber = "84"
offsetFromSymbolStart = "4544">
</Location>
<Location
uuid = "679B28C9-4889-4546-A3C6-246F045B6896 - ad91d10e3e2f73f4"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "85"
endingLineNumber = "85"
offsetFromSymbolStart = "4532">
</Location>
<Location
uuid = "679B28C9-4889-4546-A3C6-246F045B6896 - ad91d10e3e2f7253"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "88"
endingLineNumber = "88"
offsetFromSymbolStart = "3468">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "82615B78-E63E-4A1B-BDE7-151EC8CAECFE"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "78"
endingLineNumber = "78"
landmarkName = "writeSpatialVideo(leftEyeVideoURL:rightEyeVideoURL:outputVideoURL:completion:)"
landmarkType = "7">
<Locations>
<Location
uuid = "82615B78-E63E-4A1B-BDE7-151EC8CAECFE - ad91d10e3e2f731d"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "78"
endingLineNumber = "78"
offsetFromSymbolStart = "4652">
</Location>
<Location
uuid = "82615B78-E63E-4A1B-BDE7-151EC8CAECFE - ad91d10e3e2f731d"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "78"
endingLineNumber = "78"
offsetFromSymbolStart = "4348">
</Location>
<Location
uuid = "82615B78-E63E-4A1B-BDE7-151EC8CAECFE - ad91d10e3e2f733a"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "79"
endingLineNumber = "79"
offsetFromSymbolStart = "4348">
</Location>
<Location
uuid = "82615B78-E63E-4A1B-BDE7-151EC8CAECFE - ad91d10e3e2f7253"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftProject.SpatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: Foundation.URL, rightEyeVideoURL: Foundation.URL, outputVideoURL: Foundation.URL, completion: (Swift.Bool, Swift.Optional&lt;Swift.Error&gt;) -&gt; ()) -&gt; ()"
moduleName = "SwiftProject"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aaa/Documents/IOS%20Dev/VR/SwiftProject/SwiftProject/Project/Util/SpatialVideoWriter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "88"
endingLineNumber = "88"
offsetFromSymbolStart = "3468">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent

View File

@ -12,6 +12,7 @@ import AVKit
import VideoToolbox
import SVProgressHUD
import Firebase
import CoreMotion
class CCSpatialShootController: BaseController {
let kNowTimeToUserDefaultKey_SpatialShootController:String = "kNowTimeToUserDefaultKey_SpatialShootController"
var wideAngleCameraDevice:AVCaptureDevice?//使
@ -33,11 +34,13 @@ class CCSpatialShootController: BaseController {
var isRecording = false
var imageCGImagePropertyOrientation:CGImagePropertyOrientation = CGImagePropertyOrientation.left//cgimage
var imageOrientation:UIImage.Orientation = UIImage.Orientation.up
var deviceOrientation = 1// 1,2
let operationQueue:OperationQueue = OperationQueue()
var leftEyeVideoURL:URL?
var rightEyeVideoURL:URL?
var outputVideoURL: URL?
let motionManager = CMMotionManager()
var imgs:NSMutableArray = NSMutableArray() //广
//================================
@ -270,9 +273,9 @@ class CCSpatialShootController: BaseController {
//======
//
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
// UIDevice.orientationDidChangeNotification
NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
// UIDevice.current.beginGeneratingDeviceOrientationNotifications()
// // UIDevice.orientationDidChangeNotification
// NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
configureSession() //
@ -286,9 +289,21 @@ class CCSpatialShootController: BaseController {
showHorizontalScreenTips()
}
if motionManager.isDeviceMotionAvailable {
motionManager.startDeviceMotionUpdates(to: OperationQueue.main) {[weak self] motion, error in
if let motion = motion {
//
self?.dealDeviceScreenOrientationByMotaion(motion)
}
}
}
//
getAlbumFirstPhoto()
orientationDidChange()
}
private func setUI() {
@ -673,18 +688,22 @@ class CCSpatialShootController: BaseController {
}else{
print("录像结束!!!")
//
stopRecordingTimer()
stopVideoRecording()
UIView.animate(withDuration: 0.3) {
// transform
self.shutterVideoButton.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
self.shutterVideoButton.layer.cornerRadius = 28
}completion: { finished in
self.isRecording = false
self.changeSwitchstatus()
}
stopRVideo()
}
}
func stopRVideo(){
print("录像结束!!!")
//
stopRecordingTimer()
stopVideoRecording()
UIView.animate(withDuration: 0.3) {
// transform
self.shutterVideoButton.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
self.shutterVideoButton.layer.cornerRadius = 28
}completion: { finished in
self.isRecording = false
self.changeSwitchstatus()
}
}
@ -841,47 +860,96 @@ class CCSpatialShootController: BaseController {
}
func enableShutterBtn(_ value:Bool) {
shutterPhotoButton.isEnabled = value
shutterVideoButton.isEnabled = value
}
//MARK: -
func dealDeviceScreenOrientationByMotaion(_ motion:CMDeviceMotion){
let x = motion.gravity.x
let y = motion.gravity.y
if (fabs(y) >= fabs(x)){
if deviceOrientation != 1 {
enableShutterBtn(false)
deviceOrientation = 1
print("竖屏");
autoStopRecordVideo()
showHorizontalScreenTips()
}
}
else{
if deviceOrientation != 2 {
enableShutterBtn(true)
deviceOrientation = 2
hidenHorizontalScreenTips()
print("横屏");
}
}
}
@objc fileprivate func orientationDidChange() {
switch UIDevice.current.orientation {
case .unknown:
print("未知")
case .portrait:
print("竖屏")
imageCGImagePropertyOrientation = .up
imageOrientation = .up
showHorizontalScreenTips()
case .portraitUpsideDown:
print("颠倒竖屏")
imageCGImagePropertyOrientation = .down
imageOrientation = .down
showHorizontalScreenTips()
case .landscapeLeft:
print("设备向左旋转横屏")
imageCGImagePropertyOrientation = .left
imageOrientation = .left
hidenHorizontalScreenTips()
case .landscapeRight:
print("设备向右旋转横屏")
imageCGImagePropertyOrientation = .right
imageOrientation = .right
hidenHorizontalScreenTips()
case .faceUp:
print("屏幕朝上")
case .faceDown:
print("屏幕朝下")
default:
break
// enableShutterBtn(false)
// switch UIDevice.current.orientation {
// case .unknown:
// print("")
// case .portrait:
// print("")
// imageCGImagePropertyOrientation = .up
// imageOrientation = .up
//
// autoStopRecordVideo()
// showHorizontalScreenTips()
// case .portraitUpsideDown:
// print("")
// autoStopRecordVideo()
// imageCGImagePropertyOrientation = .down
// imageOrientation = .down
// showHorizontalScreenTips()
// case .landscapeLeft:
// print("")
// imageCGImagePropertyOrientation = .left
// imageOrientation = .left
// hidenHorizontalScreenTips()
// enableShutterBtn(true)
// case .landscapeRight:
// print("")
// imageCGImagePropertyOrientation = .right
// imageOrientation = .right
// hidenHorizontalScreenTips()
// enableShutterBtn(true)
// case .faceUp:
// autoStopRecordVideo()
// print("")
// case .faceDown:
// autoStopRecordVideo()
// print("")
// default:
// break
// }
}
//
func autoStopRecordVideo(){
if self.isRecording {
stopRVideo()
}
}
deinit {
//
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
//
UIDevice.current.endGeneratingDeviceOrientationNotifications()
// NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
// //
// UIDevice.current.endGeneratingDeviceOrientationNotifications()
if let timer = self.timer {
//