Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #18 from ejeinc/stereo-introduction-view
Browse files Browse the repository at this point in the history
Add `StereoViewController.introductionView`
  • Loading branch information
junpluse authored Apr 26, 2017
2 parents 7991960 + 2525bb5 commit a296d6f
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Examples/MonoImage/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ final class ViewController: UIViewController {
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

panoramaView?.updateInterfaceOrientation(with: coordinator)
}
}
2 changes: 2 additions & 0 deletions Examples/MonoVideo/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ final class ViewController: UIViewController {
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

panoramaView?.updateInterfaceOrientation(with: coordinator)
}

Expand Down
17 changes: 15 additions & 2 deletions Examples/StereoImage/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ final class ViewController: UIViewController {
loadStereoButton()
}

override func viewWillAppear(_ animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

panoramaView?.isPlaying = true
}

override func viewDidDisappear(_ animated: Bool) {
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

panoramaView?.isPlaying = false
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

panoramaView?.updateInterfaceOrientation(with: coordinator)
}

Expand All @@ -89,7 +95,14 @@ final class ViewController: UIViewController {
}

func presentStereoView() {
let introView = UILabel()
introView.text = "Place your phone into your Cardboard viewer."
introView.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
introView.textAlignment = .center
introView.backgroundColor = #colorLiteral(red: 0.2745098039, green: 0.3529411765, blue: 0.3921568627, alpha: 1)

let stereoViewController = StereoViewController(device: device)
stereoViewController.introductionView = introView
stereoViewController.scene = panoramaView?.scene
present(stereoViewController, animated: true, completion: nil)
}
Expand Down
17 changes: 15 additions & 2 deletions Examples/StereoVideo/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,21 @@ final class ViewController: UIViewController {
loadStereoButton()
}

override func viewWillAppear(_ animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

panoramaView?.isPlaying = true
}

override func viewDidDisappear(_ animated: Bool) {
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

panoramaView?.isPlaying = false
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

panoramaView?.updateInterfaceOrientation(with: coordinator)
}

Expand All @@ -138,7 +144,14 @@ final class ViewController: UIViewController {
}

func presentStereoView() {
let introView = UILabel()
introView.text = "Place your phone into your Cardboard viewer."
introView.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
introView.textAlignment = .center
introView.backgroundColor = #colorLiteral(red: 0.2745098039, green: 0.3529411765, blue: 0.3921568627, alpha: 1)

let stereoViewController = StereoViewController(device: device)
stereoViewController.introductionView = introView
stereoViewController.scene = panoramaView?.scene
stereoViewController.stereoView.tapGestureRecognizer.addTarget(self, action: #selector(togglePlaying))
present(stereoViewController, animated: true, completion: nil)
Expand Down
145 changes: 143 additions & 2 deletions Sources/StereoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,29 @@ open class StereoViewController: UIViewController, SceneLoadable {

open var helpButtonHandler: ((_ sender: UIButton) -> Void)?

open var introductionView: UIView? {
willSet {
introductionView?.removeFromSuperview()
}
didSet {
guard isViewLoaded else {
return
}
if let _ = introductionView {
showIntroductionView()
} else {
hideIntroductionView()
}
}
}

private weak var _stereoView: StereoView?
private weak var _closeButton: UIButton?
private weak var _helpButton: UIButton?

private weak var introdutionContainerView: UIView?
private var introductionViewUpdateTimer: DispatchSourceTimer?

#if (arch(arm) || arch(arm64)) && os(iOS)
public init(device: MTLDevice) {
self.device = device
Expand Down Expand Up @@ -105,9 +124,14 @@ open class StereoViewController: UIViewController, SceneLoadable {
stereoView.isPlaying = false
_stereoView = stereoView

let introductionContainerView = UIView(frame: stereoView.bounds)
introductionContainerView.isHidden = true
self.introdutionContainerView = introductionContainerView

let view = UIView(frame: stereoView.bounds)
view.backgroundColor = .black
view.addSubview(stereoView)
view.addSubview(introductionContainerView)
self.view = view

NSLayoutConstraint.activate([
Expand All @@ -126,24 +150,45 @@ open class StereoViewController: UIViewController, SceneLoadable {
}
}

open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

introdutionContainerView!.bounds = view!.bounds
introdutionContainerView!.center = CGPoint(x: view!.bounds.midX, y: view!.bounds.midY)
introductionView?.frame = introdutionContainerView!.bounds
}

open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if animated {
_stereoView?.alpha = 0
}
}

open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

_stereoView?.isPlaying = true

if animated {
UIView.animate(withDuration: 0.2) {
self._stereoView?.alpha = 1
}
}

if UIDevice.current.orientation != .unknown {
showIntroductionView(animated: animated)
startIntroductionViewVisibilityUpdates()
}
}

open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

_stereoView?.isPlaying = false

stopIntroductionViewVisibilityUpdates()
}

open override var prefersStatusBarHidden: Bool {
Expand All @@ -170,7 +215,7 @@ open class StereoViewController: UIViewController, SceneLoadable {
closeButton.translatesAutoresizingMaskIntoConstraints = false
_closeButton = closeButton

view.insertSubview(closeButton, aboveSubview: stereoView)
view.addSubview(closeButton)

NSLayoutConstraint.activate([
closeButton.widthAnchor.constraint(equalToConstant: 88),
Expand Down Expand Up @@ -206,7 +251,7 @@ open class StereoViewController: UIViewController, SceneLoadable {
helpButton.translatesAutoresizingMaskIntoConstraints = false
_helpButton = helpButton

view.insertSubview(helpButton, aboveSubview: stereoView)
view.addSubview(helpButton)

NSLayoutConstraint.activate([
helpButton.widthAnchor.constraint(equalToConstant: 88),
Expand All @@ -226,6 +271,102 @@ open class StereoViewController: UIViewController, SceneLoadable {
UIApplication.shared.openURL(url)
}
}

private func showIntroductionView(animated: Bool = false) {
precondition(isViewLoaded)

guard let introductionView = introductionView, let containerView = introdutionContainerView, let stereoView = _stereoView else {
return
}

if introductionView.superview != containerView {
introductionView.frame = containerView.bounds
introductionView.autoresizingMask = []
containerView.addSubview(introductionView)
}

if animated {
if containerView.isHidden {
containerView.isHidden = false
containerView.transform = CGAffineTransform(translationX: 0, y: containerView.bounds.height)
}
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [.beginFromCurrentState],
animations: {
containerView.transform = .identity
stereoView.alpha = 0
},
completion: nil)
} else {
containerView.isHidden = false
containerView.transform = .identity
stereoView.alpha = 0
}
}

private func hideIntroductionView(animated: Bool = false) {
precondition(isViewLoaded)

guard let containerView = introdutionContainerView, let stereoView = _stereoView else {
return
}

if animated {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [.beginFromCurrentState],
animations: {
containerView.transform = CGAffineTransform(translationX: 0, y: containerView.bounds.height)
stereoView.alpha = 1
},
completion: { isFinished in
guard isFinished else {
return
}
containerView.isHidden = true
containerView.transform = .identity
})
} else {
containerView.isHidden = true
containerView.transform = .identity
stereoView.alpha = 1
}
}

private func startIntroductionViewVisibilityUpdates(withInterval interval: TimeInterval = 3, afterDelay delay: TimeInterval = 3) {
precondition(introductionViewUpdateTimer == nil)

let timer = DispatchSource.makeTimerSource(queue: .main)
timer.scheduleRepeating(deadline: .now() + delay, interval: interval)
timer.setEventHandler { [weak self] in
guard self?.isViewLoaded == true, let _ = self?.introductionView else {
return
}
switch UIDevice.current.orientation {
case .landscapeLeft where self?.introdutionContainerView?.isHidden == false:
self?.hideIntroductionView(animated: true)
case .landscapeRight where self?.introdutionContainerView?.isHidden == true:
self?.showIntroductionView(animated: true)
default:
break
}
}
timer.resume()

introductionViewUpdateTimer = timer
}

private func stopIntroductionViewVisibilityUpdates() {
introductionViewUpdateTimer?.cancel()
introductionViewUpdateTimer = nil
}
}

extension StereoViewController: ImageLoadable {}
Expand Down

0 comments on commit a296d6f

Please sign in to comment.