|
| 1 | +// |
| 2 | +// CameraController.swift |
| 3 | +// TiltUp |
| 4 | +// |
| 5 | +// Created by Jeremy Grenier on 10/17/19. |
| 6 | +// Copyright © 2019 Clutter. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +public final class CameraController: UIViewController { |
| 12 | + private let previewView = CameraPreviewView() |
| 13 | + private let overlayView: CameraOverlayView |
| 14 | + |
| 15 | + private let generator = UIImpactFeedbackGenerator(style: .medium) |
| 16 | + |
| 17 | + public let viewModel: CameraViewModel |
| 18 | + |
| 19 | + public override var prefersStatusBarHidden: Bool { true } |
| 20 | + |
| 21 | + public init(viewModel: CameraViewModel, hint: String?) { |
| 22 | + self.viewModel = viewModel |
| 23 | + overlayView = CameraOverlayView(hint: { _ in hint }) |
| 24 | + |
| 25 | + super.init(nibName: nil, bundle: nil) |
| 26 | + } |
| 27 | + |
| 28 | + public init(viewModel: CameraViewModel, hint: @escaping (_ numberOfPhotos: Int) -> String?) { |
| 29 | + self.viewModel = viewModel |
| 30 | + overlayView = CameraOverlayView(hint: hint) |
| 31 | + |
| 32 | + super.init(nibName: nil, bundle: nil) |
| 33 | + } |
| 34 | + |
| 35 | + required init?(coder: NSCoder) { |
| 36 | + fatalError("init(coder:) has not been implemented") |
| 37 | + } |
| 38 | + |
| 39 | + public override func viewDidLoad() { |
| 40 | + super.viewDidLoad() |
| 41 | + |
| 42 | + view.backgroundColor = .black |
| 43 | + |
| 44 | + viewModel.viewObservers.presentAlert = { [weak self] alert in |
| 45 | + DispatchQueue.main.async { |
| 46 | + self?.present(alert, animated: true) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + viewModel.viewObservers.rotateInterface = { [weak self] orientation in |
| 51 | + DispatchQueue.main.async { |
| 52 | + self?.overlayView.interfaceOrientation = orientation |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + viewModel.viewObservers.updateOverlayState = { [weak self] state in |
| 57 | + DispatchQueue.main.async { |
| 58 | + self?.overlayView.state = state |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + viewModel.viewObservers.willCapturePhotoAnimation = { [weak self] in |
| 63 | + guard let self = self else { return } |
| 64 | + DispatchQueue.main.async { |
| 65 | + self.generator.impactOccurred() |
| 66 | + self.previewView.videoPreviewLayer.opacity = 0 |
| 67 | + UIView.animate(withDuration: 0.25) { |
| 68 | + self.previewView.videoPreviewLayer.opacity = 1 |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + previewView.session = viewModel.session |
| 74 | + |
| 75 | + view.addSubview(previewView) |
| 76 | + view.addSubview(overlayView) |
| 77 | + overlayView.delegate = viewModel |
| 78 | + |
| 79 | + let pinch = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch)) |
| 80 | + overlayView.addGestureRecognizer(pinch) |
| 81 | + |
| 82 | + let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap)) |
| 83 | + overlayView.addGestureRecognizer(tap) |
| 84 | + } |
| 85 | + |
| 86 | + public override func viewWillAppear(_ animated: Bool) { |
| 87 | + super.viewWillAppear(animated) |
| 88 | + navigationController?.setNavigationBarHidden(true, animated: false) |
| 89 | + viewModel.viewWillAppear() |
| 90 | + } |
| 91 | + |
| 92 | + public override func viewWillDisappear(_ animated: Bool) { |
| 93 | + viewModel.viewWillDisappear() |
| 94 | + super.viewWillDisappear(animated) |
| 95 | + } |
| 96 | + |
| 97 | + public override func viewDidLayoutSubviews() { |
| 98 | + super.viewDidLayoutSubviews() |
| 99 | + |
| 100 | + previewView.frame = CGRect(x: 0.0, y: 64.0, width: view.bounds.width, height: view.bounds.width * 4 / 3) |
| 101 | + overlayView.frame = view.bounds |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +extension CameraController { |
| 106 | + @objc private func handlePinch(_ pinch: UIPinchGestureRecognizer) { |
| 107 | + viewModel.handlePinch(pinch) |
| 108 | + } |
| 109 | + |
| 110 | + @objc private func handleTap(_ tap: UITapGestureRecognizer) { |
| 111 | + let point = tap.location(in: previewView) |
| 112 | + |
| 113 | + guard point.x.isFinite && point.y.isFinite else { return } |
| 114 | + |
| 115 | + let devicePoint = previewView.videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: point) |
| 116 | + |
| 117 | + focusAnimation(at: point) |
| 118 | + viewModel.focusCamera(at: devicePoint) |
| 119 | + } |
| 120 | + |
| 121 | + func focusAnimation(at point: CGPoint) { |
| 122 | + let focusView = UIView(frame: CGRect(x: 0, y: 0, width: 70, height: 70)) |
| 123 | + focusView.layer.borderColor = UIColor(red: 0xFF / 0xFF, green: 0xB8 / 0xFF, blue: 0x18 / 0xFF, alpha: 1.0).cgColor |
| 124 | + focusView.layer.borderWidth = 1 |
| 125 | + focusView.center = point |
| 126 | + focusView.alpha = 0.0 |
| 127 | + previewView.subviews.forEach({ $0.removeFromSuperview() }) |
| 128 | + previewView.addSubview(focusView) |
| 129 | + |
| 130 | + let appearAnimation = { |
| 131 | + focusView.alpha = 1.0 |
| 132 | + focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25) |
| 133 | + } |
| 134 | + |
| 135 | + let disappearAnimation = { |
| 136 | + focusView.alpha = 0.0 |
| 137 | + focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6) |
| 138 | + } |
| 139 | + |
| 140 | + UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: appearAnimation, completion: { _ in |
| 141 | + UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: disappearAnimation) { _ in |
| 142 | + focusView.removeFromSuperview() |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments