diff --git a/Examples/MonoImage/Sources/ViewController.swift b/Examples/MonoImage/Sources/ViewController.swift index 0607e91..25b40b5 100644 --- a/Examples/MonoImage/Sources/ViewController.swift +++ b/Examples/MonoImage/Sources/ViewController.swift @@ -26,7 +26,7 @@ final class ViewController: UIViewController { #else let panoramaView = PanoramaView(frame: view.bounds) // iOS Simulator #endif - panoramaView.setNeedsResetRotation() + panoramaView.moveType = .all panoramaView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(panoramaView) @@ -40,7 +40,7 @@ final class ViewController: UIViewController { NSLayoutConstraint.activate(constraints) // double tap to reset rotation - let doubleTapGestureRecognizer = UITapGestureRecognizer(target: panoramaView, action: #selector(PanoramaView.setNeedsResetRotation(_:))) + let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeMode)) doubleTapGestureRecognizer.numberOfTapsRequired = 2 panoramaView.addGestureRecognizer(doubleTapGestureRecognizer) @@ -49,6 +49,16 @@ final class ViewController: UIViewController { panoramaView.load(#imageLiteral(resourceName: "Sample"), format: .mono) } + func changeMode() { + if self.panoramaView?.moveType == .all { + self.panoramaView?.moveType = .touch + } else if self.panoramaView?.moveType == .touch { + self.panoramaView?.moveType = .motion + } else { + self.panoramaView?.moveType = .all + } + } + override func viewDidLoad() { super.viewDidLoad() diff --git a/MetalScope.xcodeproj/project.pbxproj b/MetalScope.xcodeproj/project.pbxproj index 7d830cd..71bc393 100644 --- a/MetalScope.xcodeproj/project.pbxproj +++ b/MetalScope.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 46ADA8B11F7A421F00258B43 /* MoveType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ADA8B01F7A421F00258B43 /* MoveType.swift */; }; BF1452DF1E320BAC0042598E /* SceneLoadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF1452DE1E320BAC0042598E /* SceneLoadable.swift */; }; BF1453061E321CE30042598E /* CategoryBitMask.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF1453051E321CE30042598E /* CategoryBitMask.swift */; }; BF1F1D381EAF6A53004AEEB4 /* PanoramaPanGestureManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFB8922F1E2F761600FA9129 /* PanoramaPanGestureManager.swift */; }; @@ -47,6 +48,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 46ADA8B01F7A421F00258B43 /* MoveType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveType.swift; sourceTree = ""; }; BF1452DE1E320BAC0042598E /* SceneLoadable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = SceneLoadable.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; BF1453051E321CE30042598E /* CategoryBitMask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = CategoryBitMask.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; BF46F0991EADDEDA00F5CB58 /* Deprecations+Removals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Deprecations+Removals.swift"; sourceTree = ""; }; @@ -216,6 +218,7 @@ children = ( BFB8919E1E2E083000FA9129 /* PanoramaView.swift */, BFB8922F1E2F761600FA9129 /* PanoramaPanGestureManager.swift */, + 46ADA8B01F7A421F00258B43 /* MoveType.swift */, ); name = PanoramaView; sourceTree = ""; @@ -335,6 +338,7 @@ BFB891A81E2E10CA00FA9129 /* Rotation+SceneKit.swift in Sources */, BF603F551E41B57600A28013 /* OrientationIndicator.swift in Sources */, BF1F1D381EAF6A53004AEEB4 /* PanoramaPanGestureManager.swift in Sources */, + 46ADA8B11F7A421F00258B43 /* MoveType.swift in Sources */, BFB8920B1E2F364B00FA9129 /* SphericalMediaScene.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Sources/Deprecations+Removals.swift b/Sources/Deprecations+Removals.swift index 6d9bf49..7608ea8 100644 --- a/Sources/Deprecations+Removals.swift +++ b/Sources/Deprecations+Removals.swift @@ -40,7 +40,7 @@ extension OrientationNode { @available(*, renamed: "OrientationNode.resetRotation") public func resetCenter(animated: Bool, completionHanlder: (() -> Void)? = nil) { - resetRotation(animated: animated, completionHanlder: completionHanlder) + resetRotation(animated: animated, fully: false, completionHanlder: completionHanlder) } } diff --git a/Sources/MoveType.swift b/Sources/MoveType.swift new file mode 100644 index 0000000..c6409ca --- /dev/null +++ b/Sources/MoveType.swift @@ -0,0 +1,13 @@ +// +// MoveType.swift +// MetalScope +// +// Created by Иван Морозов on 9/26/17. +// Copyright © 2017 MintRocket LLC. All rights reserved. +// + +public enum PanoramaMoveType { + case all + case motion + case touch +} diff --git a/Sources/OrientationNode.swift b/Sources/OrientationNode.swift index 3a12993..83bde44 100644 --- a/Sources/OrientationNode.swift +++ b/Sources/OrientationNode.swift @@ -22,7 +22,20 @@ public final class OrientationNode: SCNNode { } } - public var deviceOrientationProvider: DeviceOrientationProvider? = DefaultDeviceOrientationProvider() + public var motionIsEnabled = true + + private var provider: DeviceOrientationProvider? = DefaultDeviceOrientationProvider() + public var deviceOrientationProvider: DeviceOrientationProvider? { + get { + if motionIsEnabled { + return provider + } + return nil + } + set { + provider = newValue + } + } public var interfaceOrientationProvider: InterfaceOrientationProvider? = DefaultInterfaceOrientationProvider() @@ -96,7 +109,13 @@ public final class OrientationNode: SCNNode { userRotationNode.transform = SCNMatrix4Identity } - public func resetRotation(animated: Bool, completionHanlder: (() -> Void)? = nil) { + public func fullyResetRotation() { + userRotationNode.transform = SCNMatrix4Identity + referenceRotationNode.transform = SCNMatrix4Identity + deviceOrientationNode.transform = SCNMatrix4Identity + } + + public func resetRotation(animated: Bool, fully: Bool, completionHanlder: (() -> Void)? = nil) { SCNTransaction.lock() SCNTransaction.begin() SCNTransaction.animationDuration = 0.6 @@ -104,7 +123,11 @@ public final class OrientationNode: SCNNode { SCNTransaction.completionBlock = completionHanlder SCNTransaction.disableActions = !animated - resetRotation() + if fully { + fullyResetRotation() + } else { + resetRotation() + } SCNTransaction.commit() SCNTransaction.unlock() @@ -113,9 +136,9 @@ public final class OrientationNode: SCNNode { /// Requests reset of rotation in the next rendering frame. /// /// - Parameter animated: Pass true to animate the transition. - public func setNeedsResetRotation(animated: Bool) { + public func setNeedsResetRotation(animated: Bool, fully: Bool) { let action = SCNAction.run { node in - (node as! OrientationNode).resetRotation(animated: animated) + (node as! OrientationNode).resetRotation(animated: animated, fully: fully) } runAction(action, forKey: "setNeedsResetRotation") } diff --git a/Sources/PanoramaView.swift b/Sources/PanoramaView.swift index b2e1ab2..9c42e1b 100644 --- a/Sources/PanoramaView.swift +++ b/Sources/PanoramaView.swift @@ -14,6 +14,12 @@ public final class PanoramaView: UIView, SceneLoadable { public let device: MTLDevice #endif + public var moveType: PanoramaMoveType = .all { + didSet { + updateMoveType() + } + } + public var scene: SCNScene? { get { return scnView.scene @@ -98,6 +104,12 @@ public final class PanoramaView: UIView, SceneLoadable { interfaceOrientationUpdater.updateInterfaceOrientation() } } + + fileprivate func updateMoveType() { + self.panGestureManager.gestureRecognizer.isEnabled = [.all, .touch].contains(self.moveType) + self.orientationNode.motionIsEnabled = [.all, .motion].contains(self.moveType) + self.setNeedsResetRotation(animated: true, fully: true) + } } extension PanoramaView: ImageLoadable {} @@ -145,9 +157,9 @@ extension PanoramaView { interfaceOrientationUpdater.updateInterfaceOrientation(with: transitionCoordinator) } - public func setNeedsResetRotation(animated: Bool = false) { + public func setNeedsResetRotation(animated: Bool = false, fully: Bool = false) { panGestureManager.stopAnimations() - orientationNode.setNeedsResetRotation(animated: animated) + orientationNode.setNeedsResetRotation(animated: animated, fully: fully) } public func setNeedsResetRotation(_ sender: Any?) { diff --git a/Sources/StereoView.swift b/Sources/StereoView.swift index 1819d76..a8378e0 100644 --- a/Sources/StereoView.swift +++ b/Sources/StereoView.swift @@ -193,7 +193,7 @@ extension StereoView { } public func setNeedsResetRotation() { - orientationNode.setNeedsResetRotation(animated: false) + orientationNode.setNeedsResetRotation(animated: false, fully: false) } }