Commit 321d15fb by Daniel Dahan

development: progression commit for CaptureController

parent baa54bba
...@@ -519,9 +519,9 @@ ...@@ -519,9 +519,9 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
96717B0D1DBE6AF600DA84DB /* Capture.swift */, 96717B0D1DBE6AF600DA84DB /* Capture.swift */,
96717B0E1DBE6AF600DA84DB /* CaptureController.swift */,
96717B0F1DBE6AF600DA84DB /* CapturePreview.swift */, 96717B0F1DBE6AF600DA84DB /* CapturePreview.swift */,
96717B101DBE6AF600DA84DB /* CaptureSession.swift */, 96717B101DBE6AF600DA84DB /* CaptureSession.swift */,
96717B0E1DBE6AF600DA84DB /* CaptureController.swift */,
); );
path = Capture; path = Capture;
sourceTree = "<group>"; sourceTree = "<group>";
......
...@@ -37,152 +37,16 @@ public enum CaptureMode: Int { ...@@ -37,152 +37,16 @@ public enum CaptureMode: Int {
case video case video
} }
@objc(CaptureDelegate) open class Capture: View {
public protocol CaptureDelegate {
/**
A delegation method that is fired when the record timer has started.
- Parameter capture: A reference to the calling capture.
*/
@objc
optional func captureDidStartRecordTimer(capture: Capture)
/**
A delegation method that is fired when the record timer was updated.
- Parameter capture: A reference to the calling capture.
- Parameter hours: An integer representing hours.
- Parameter minutes: An integer representing minutes.
- Parameter seconds: An integer representing seconds.
*/
@objc
optional func captureDidUpdateRecordTimer(capture: Capture, hours: Int, minutes: Int, seconds: Int)
/**
A delegation method that is fired when the record timer has stopped.
- Parameter capture: A reference to the calling capture.
- Parameter hours: An integer representing hours.
- Parameter minutes: An integer representing minutes.
- Parameter seconds: An integer representing seconds.
*/
@objc
optional func captureDidStopRecordTimer(capture: Capture, hours: Int, minutes: Int, seconds: Int)
/**
A delegation method that is fired when the user tapped to adjust the focus.
- Parameter capture: A reference to the calling capture.
- Parameter point: CGPoint that the user tapped at.
*/
@objc
optional func captureDidTapToFocusAtPoint(capture: Capture, point: CGPoint)
/**
A delegation method that is fired when the user tapped to adjust the exposure.
- Parameter capture: A reference to the calling capture.
- Parameter point: CGPoint that the user tapped at.
*/
@objc
optional func captureDidTapToExposeAtPoint(capture: Capture, point: CGPoint)
/**
A delegation method that is fired when the user tapped to reset.
- Parameter capture: A reference to the calling capture.
- Parameter point: CGPoint that the user tapped at.
*/
@objc
optional func captureDidTapToResetAtPoint(capture: Capture, point: CGPoint)
/**
A delegation method that is fired when the user pressed the flash button.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressFlashButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user pressed the switch camera button.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressSwitchCamerasButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user pressed capture button.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressCaptureButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user enabled the photo camera.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressCameraButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user enabled the video camera.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressVideoButton(capture: Capture, button: UIButton)
}
open class Capture: View, UIGestureRecognizerDelegate {
/// A delegation reference.
open weak var delegate: CaptureDelegate?
/// A reference to the capture mode. /// A reference to the capture mode.
open var mode = CaptureMode.video open var mode = CaptureMode.video
/// Insets preset value for content.
open var contentEdgeInsetsPreset = EdgeInsetsPreset.none {
didSet {
contentEdgeInsets = EdgeInsetsPresetToValue(preset: contentEdgeInsetsPreset)
}
}
/// Content insert value.
@IBInspectable
open var contentEdgeInsets = EdgeInsets.zero {
didSet {
layoutSubviews()
}
}
/// A reference to the CapturePreview view. /// A reference to the CapturePreview view.
open internal(set) var preview: CapturePreview! open internal(set) var preview: CapturePreview!
/// A reference to the CaptureSession. /// A reference to the CaptureSession.
open internal(set) var session: CaptureSession! open internal(set) var session: CaptureSession!
/// A reference to the focusView used in focus animations.
open internal(set) var focusView: UIView?
/// A reference to the exposureView used in exposure animations.
open internal(set) var exposureView: UIView?
/// A reference to the resetView used in reset animations.
open internal(set) var resetView: UIView?
/// A reference to the cameraButton.
open private(set) var cameraButton: IconButton!
/// A reference to the captureButton.
open private(set) var captureButton: FabButton!
/// A reference to the videoButton.
open private(set) var videoButton: IconButton!
/// A reference to the switchCameraButton.
open private(set) var switchCamerasButton: IconButton!
/// A reference to the flashButton.
open private(set) var flashButton: IconButton!
/// A convenience initializer. /// A convenience initializer.
public convenience init() { public convenience init() {
self.init(frame: .zero) self.init(frame: .zero)
...@@ -203,154 +67,17 @@ open class Capture: View, UIGestureRecognizerDelegate { ...@@ -203,154 +67,17 @@ open class Capture: View, UIGestureRecognizerDelegate {
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
backgroundColor = .black backgroundColor = .black
contentEdgeInsetsPreset = .square4
prepareCaptureSession() prepareCaptureSession()
preparePreview() preparePreview()
prepareCaptureButton()
prepareCameraButton()
prepareVideoButton()
prepareSwitchCamerasButton()
prepareFlashButton()
} }
/// Reloads the view. /// Reloads the view.
open func reload() { open func reload() {
preview.frame = bounds preview.frame = bounds
} }
/** /// Prepare the session.
Handler for the flashButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleFlashButton(button: UIButton) {
delegate?.captureDidPressFlashButton?(capture: self, button: button)
}
/**
Handler for the switchCameraButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleSwitchCamerasButton(button: UIButton) {
session.switchCameras()
delegate?.captureDidPressSwitchCamerasButton?(capture: self, button: button)
}
/**
Handler for the captureButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleCaptureButton(button: UIButton) {
delegate?.captureDidPressCaptureButton?(capture: self, button: button)
}
/**
Handler for the cameraButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleCameraButton(button: UIButton) {
mode = .photo
delegate?.captureDidPressCameraButton?(capture: self, button: button)
}
/**
Handler for the videoButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleVideoButton(button: UIButton) {
mode = .video
delegate?.captureDidPressVideoButton?(capture: self, button: button)
}
/**
Handler for the tapToFocusGesture.
- Parameter recognizer: A UITapGestureRecognizer that is associated with the event.
*/
@objc
internal func handleTapToFocusGesture(recognizer: UITapGestureRecognizer) {
guard isTapToFocusEnabled && session.isFocusPointOfInterestSupported else {
return
}
let point = recognizer.location(in: self)
session.focus(at: preview.captureDevicePointOfInterestForPoint(point: point))
animateTap(view: focusView!, point: point)
delegate?.captureDidTapToFocusAtPoint?(capture: self, point: point)
}
/**
Handler for the tapToExposeGesture.
- Parameter recognizer: A UITapGestureRecognizer that is associated with the event.
*/
@objc
internal func handleTapToExposeGesture(recognizer: UITapGestureRecognizer) {
guard isTapToExposeEnabled && session.isExposurePointOfInterestSupported else {
return
}
let point = recognizer.location(in: self)
session.expose(at: preview.captureDevicePointOfInterestForPoint(point: point))
animateTap(view: exposureView!, point: point)
delegate?.captureDidTapToExposeAtPoint?(capture: self, point: point)
}
/**
Handler for the tapToResetGesture.
- Parameter recognizer: A UITapGestureRecognizer that is associated with the event.
*/
@objc
internal func handleTapToResetGesture(recognizer: UITapGestureRecognizer) {
guard isTapToResetEnabled else {
return
}
session.reset()
let point = preview.pointForCaptureDevicePointOfInterest(point: CGPoint(x: 0.5, y: 0.5))
animateTap(view: resetView!, point: point)
delegate?.captureDidTapToResetAtPoint?(capture: self, point: point)
}
/**
Prepares a given tap gesture.
- Parameter gesture: An optional UITapGestureRecognizer to prepare.
- Parameter numberOfTapsRequired: An integer of the number of taps required
to activate the gesture.
- Parameter numberOfTouchesRequired: An integer of the number of touches, fingers,
required to activate the gesture.
- Parameter selector: A Selector to handle the event.
*/
private func prepareTapGesture(gesture: inout UITapGestureRecognizer?, numberOfTapsRequired: Int, numberOfTouchesRequired: Int, selector: Selector) {
guard nil == gesture else {
return
}
gesture = UITapGestureRecognizer(target: self, action: selector)
gesture!.delegate = self
gesture!.numberOfTapsRequired = numberOfTapsRequired
gesture!.numberOfTouchesRequired = numberOfTouchesRequired
addGestureRecognizer(gesture!)
}
/**
Removes a given tap gesture.
- Parameter gesture: An optional UITapGestureRecognizer to remove.
*/
private func removeTapGesture(gesture: inout UITapGestureRecognizer?) {
guard let v = gesture else {
return
}
removeGestureRecognizer(v)
gesture = nil
}
/// Prepare the session.
private func prepareCaptureSession() { private func prepareCaptureSession() {
session = CaptureSession() session = CaptureSession()
} }
...@@ -363,103 +90,4 @@ open class Capture: View, UIGestureRecognizerDelegate { ...@@ -363,103 +90,4 @@ open class Capture: View, UIGestureRecognizerDelegate {
(preview.layer as! AVCaptureVideoPreviewLayer).session = session.session (preview.layer as! AVCaptureVideoPreviewLayer).session = session.session
session.startSession() session.startSession()
} }
/// Prepares the captureButton.
private func prepareCaptureButton() {
captureButton = FabButton()
captureButton.addTarget(self, action: #selector(handleCaptureButton), for: .touchUpInside)
}
/// Prepares the cameraButton.
private func prepareCameraButton() {
cameraButton = IconButton(image: Icon.cm.photoCamera, tintColor: .white)
cameraButton.addTarget(self, action: #selector(handleCameraButton), for: .touchUpInside)
}
/// Preapres the videoButton.
private func prepareVideoButton() {
videoButton = IconButton(image: Icon.cm.videocam, tintColor: .white)
videoButton.addTarget(self, action: #selector(handleVideoButton), for: .touchUpInside)
}
/// Prepares the switchCameraButton.
private func prepareSwitchCamerasButton() {
switchCamerasButton = IconButton(image: Icon.cameraFront, tintColor: .white)
switchCamerasButton.addTarget(self, action: #selector(handleSwitchCamerasButton), for: .touchUpInside)
}
/// Prepares the flashButton.
private func prepareFlashButton() {
flashButton = IconButton(image: UIImage(named: Icon.flashAuto, tintColor: .white)
flashButton.addTarget(self, action: #selector(handleFlashButton), for: .touchUpInside)
session.flashMode = .auto
}
/// Prepares the focusLayer.
private func prepareFocusLayer() {
guard nil == focusView else {
return
}
focusView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
focusView!.isHidden = true
focusView!.borderWidth = 2
focusView!.borderColor = .white
preview.addSubview(focusView!)
}
/// Prepares the exposureLayer.
private func prepareExposureLayer() {
guard nil == exposureView else {
return
}
exposureView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
exposureView!.isHidden = true
exposureView!.borderWidth = 2
exposureView!.borderColor = Color.yellow.darken1
preview.addSubview(exposureView!)
}
/// Prepares the resetLayer.
private func prepareResetLayer() {
guard nil == resetView else {
return
}
resetView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
resetView!.isHidden = true
resetView!.borderWidth = 2
resetView!.borderColor = Color.red.accent1
preview.addSubview(resetView!)
}
/// Animates the tap and layer.
private func animateTap(view: UIView, point: CGPoint) {
// Animation.disable { [weak layer] in
// guard let v = layer else {
// return
// }
// v.transform = CATransform3DIdentity
// v.position = point
// v.isHidden = false
// }
// Animation.animateWithDuration(duration: 0.25, animations: { [weak layer] in
// guard let v = layer else {
// return
// }
// v.transform = CATransform3DMakeScale(0.5, 0.5, 1)
// }) {
// Animation.delay(time: 0.4) { [weak layer] in
// Animation.disable { [weak layer] in
// guard let v = layer else {
// return
// }
// v.isHidden = true
// }
// }
// }
}
} }
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
*/ */
import UIKit import UIKit
import AVFoundation
extension UIViewController { extension UIViewController {
/** /**
...@@ -48,10 +49,107 @@ extension UIViewController { ...@@ -48,10 +49,107 @@ extension UIViewController {
} }
} }
open class CaptureController: ToolbarController, CaptureDelegate, CaptureSessionDelegate { @objc(CaptureControllerDelegate)
public protocol CaptureControllerDelegate: ToolbarControllerDelegate {
/**
A delegation method that is fired when the record timer has started.
- Parameter capture: A reference to the calling capture.
*/
@objc
optional func captureDidStartRecordTimer( capture: Capture)
/**
A delegation method that is fired when the record timer was updated.
- Parameter capture: A reference to the calling capture.
- Parameter hours: An integer representing hours.
- Parameter minutes: An integer representing minutes.
- Parameter seconds: An integer representing seconds.
*/
@objc
optional func captureDidUpdateRecordTimer(capture: Capture, hours: Int, minutes: Int, seconds: Int)
/**
A delegation method that is fired when the record timer has stopped.
- Parameter capture: A reference to the calling capture.
- Parameter hours: An integer representing hours.
- Parameter minutes: An integer representing minutes.
- Parameter seconds: An integer representing seconds.
*/
@objc
optional func captureDidStopRecordTimer(capture: Capture, hours: Int, minutes: Int, seconds: Int)
/**
A delegation method that is fired when the user tapped to adjust the focus.
- Parameter capture: A reference to the calling capture.
- Parameter point: CGPoint that the user tapped at.
*/
@objc
optional func captureDidTapToFocusAtPoint(capture: Capture, point: CGPoint)
/**
A delegation method that is fired when the user tapped to adjust the exposure.
- Parameter capture: A reference to the calling capture.
- Parameter point: CGPoint that the user tapped at.
*/
@objc
optional func captureDidTapToExposeAtPoint(capture: Capture, point: CGPoint)
/**
A delegation method that is fired when the user tapped to reset.
- Parameter capture: A reference to the calling capture.
- Parameter point: CGPoint that the user tapped at.
*/
@objc
optional func captureDidTapToResetAtPoint(capture: Capture, point: CGPoint)
/**
A delegation method that is fired when the user pressed the flash button.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressFlashButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user pressed the switch camera button.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressSwitchCamerasButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user pressed capture button.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressCaptureButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user enabled the photo camera.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressCameraButton(capture: Capture, button: UIButton)
/**
A delegation method that is fired when the user enabled the video camera.
- Parameter capture: A reference to the calling capture.
- Parameter button: A reference to the UIButton that the user pressed.
*/
@objc
optional func captureDidPressVideoButton(capture: Capture, button: UIButton)
}
open class CaptureController: ToolbarController, CaptureControllerDelegate, CaptureSessionDelegate, UIGestureRecognizerDelegate {
/// A reference to the Capture instance. /// A reference to the Capture instance.
open private(set) lazy var capture: Capture = Capture() open private(set) lazy var capture: Capture = Capture()
/// A Timer reference for when recording is enabled.
internal var timer: Timer?
/// A tap gesture reference for focus events. /// A tap gesture reference for focus events.
private var tapToFocusGesture: UITapGestureRecognizer? private var tapToFocusGesture: UITapGestureRecognizer?
...@@ -61,20 +159,32 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -61,20 +159,32 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
/// A tap gesture reference for reset events. /// A tap gesture reference for reset events.
private var tapToResetGesture: UITapGestureRecognizer? private var tapToResetGesture: UITapGestureRecognizer?
/// A reference to the cameraButton.
open private(set) var cameraButton: IconButton!
/// A reference to the captureButton.
open private(set) var captureButton: FabButton!
/// A reference to the videoButton.
open private(set) var videoButton: IconButton!
/// A reference to the switchCameraButton.
open private(set) var switchCamerasButton: IconButton!
/// A reference to the flashButton.
open private(set) var flashButton: IconButton!
/// A boolean indicating whether to enable tap to focus. /// A boolean indicating whether to enable tap to focus.
@IBInspectable @IBInspectable
open var isTapToFocusEnabled = false { open var isTapToFocusEnabled = false {
didSet { didSet {
guard isTapToFocusEnabled else { guard isTapToFocusEnabled else {
removeTapGesture(gesture: &tapToFocusGesture) removeTapGesture(gesture: &tapToFocusGesture)
focusView?.removeFromSuperview()
focusView = nil
return return
} }
isTapToResetEnabled = true isTapToResetEnabled = true
prepareFocusLayer()
prepareTapGesture(gesture: &tapToFocusGesture, numberOfTapsRequired: 1, numberOfTouchesRequired: 1, selector: #selector(handleTapToFocusGesture)) prepareTapGesture(gesture: &tapToFocusGesture, numberOfTapsRequired: 1, numberOfTouchesRequired: 1, selector: #selector(handleTapToFocusGesture))
if let v = tapToExposeGesture { if let v = tapToExposeGesture {
...@@ -89,14 +199,11 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -89,14 +199,11 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
didSet { didSet {
guard isTapToExposeEnabled else { guard isTapToExposeEnabled else {
removeTapGesture(gesture: &tapToExposeGesture) removeTapGesture(gesture: &tapToExposeGesture)
exposureView?.removeFromSuperview()
exposureView = nil
return return
} }
isTapToResetEnabled = true isTapToResetEnabled = true
prepareExposureLayer()
prepareTapGesture(gesture: &tapToExposeGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 1, selector: #selector(handleTapToExposeGesture)) prepareTapGesture(gesture: &tapToExposeGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 1, selector: #selector(handleTapToExposeGesture))
if let v = tapToFocusGesture { if let v = tapToFocusGesture {
...@@ -111,12 +218,9 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -111,12 +218,9 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
didSet { didSet {
guard isTapToResetEnabled else { guard isTapToResetEnabled else {
removeTapGesture(gesture: &tapToResetGesture) removeTapGesture(gesture: &tapToResetGesture)
resetView?.removeFromSuperview()
resetView = nil
return return
} }
prepareResetLayer()
prepareTapGesture(gesture: &tapToResetGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 2, selector: #selector(handleTapToResetGesture)) prepareTapGesture(gesture: &tapToResetGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 2, selector: #selector(handleTapToResetGesture))
if let v = tapToFocusGesture { if let v = tapToFocusGesture {
...@@ -129,34 +233,10 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -129,34 +233,10 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
} }
} }
/// A reference to capture's cameraButton.
open var cameraButton: IconButton {
return capture.cameraButton
}
/// A reference to capture's videoButton.
open var videoButton: IconButton {
return capture.videoButton
}
/// A reference to capture's switchCamerasButton.
open var switchCamerasButton: IconButton {
return capture.switchCamerasButton
}
/// A reference to capture's flashButton.
open var flashButton: IconButton {
return capture.flashButton
}
/// A reference to capture's captureButton.
open var captureButton: FabButton {
return capture.captureButton
}
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
/**
if let v = cameraButton { if let v = cameraButton {
v.y = bounds.height - contentEdgeInsets.bottom - v.bounds.height v.y = bounds.height - contentEdgeInsets.bottom - v.bounds.height
v.x = contentEdgeInsets.left v.x = contentEdgeInsets.left
...@@ -175,6 +255,7 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -175,6 +255,7 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
if let v = (preview.layer as! AVCaptureVideoPreviewLayer).connection { if let v = (preview.layer as! AVCaptureVideoPreviewLayer).connection {
v.videoOrientation = session.videoOrientation v.videoOrientation = session.videoOrientation
} }
*/
} }
/** /**
...@@ -186,12 +267,20 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -186,12 +267,20 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
*/ */
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
view.backgroundColor = .black
display = .full display = .full
delegate = self
view.backgroundColor = .black
isTapToFocusEnabled = true
isTapToExposeEnabled = true
prepareStatusBar() prepareStatusBar()
prepareToolbar() prepareToolbar()
prepareCapture() prepareCapture()
prepareCaptureButton()
prepareCameraButton()
prepareVideoButton()
prepareSwitchCamerasButton()
prepareFlashButton()
} }
/// Prepares the statusBar. /// Prepares the statusBar.
...@@ -207,10 +296,181 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession ...@@ -207,10 +296,181 @@ open class CaptureController: ToolbarController, CaptureDelegate, CaptureSession
/// Prepares capture. /// Prepares capture.
private func prepareCapture() { private func prepareCapture() {
capture.delegate = self
capture.session.delegate = self capture.session.delegate = self
capture.isTapToFocusEnabled = true }
capture.isTapToExposeEnabled = true
/// Prepares the captureButton.
private func prepareCaptureButton() {
captureButton = FabButton()
captureButton.addTarget(self, action: #selector(handleCaptureButton), for: .touchUpInside)
}
/// Prepares the cameraButton.
private func prepareCameraButton() {
cameraButton = IconButton(image: Icon.cm.photoCamera, tintColor: .white)
cameraButton.addTarget(self, action: #selector(handleCameraButton), for: .touchUpInside)
}
/// Preapres the videoButton.
private func prepareVideoButton() {
videoButton = IconButton(image: Icon.cm.videocam, tintColor: .white)
videoButton.addTarget(self, action: #selector(handleVideoButton), for: .touchUpInside)
}
/// Prepares the switchCameraButton.
private func prepareSwitchCamerasButton() {
switchCamerasButton = IconButton(image: Icon.cameraFront, tintColor: .white)
switchCamerasButton.addTarget(self, action: #selector(handleSwitchCamerasButton), for: .touchUpInside)
}
/// Prepares the flashButton.
private func prepareFlashButton() {
flashButton = IconButton(image: Icon.flashAuto, tintColor: .white)
flashButton.addTarget(self, action: #selector(handleFlashButton), for: .touchUpInside)
capture.session.flashMode = .auto
}
/**
Prepares a given tap gesture.
- Parameter gesture: An optional UITapGestureRecognizer to prepare.
- Parameter numberOfTapsRequired: An integer of the number of taps required
to activate the gesture.
- Parameter numberOfTouchesRequired: An integer of the number of touches, fingers,
required to activate the gesture.
- Parameter selector: A Selector to handle the event.
*/
private func prepareTapGesture(gesture: inout UITapGestureRecognizer?, numberOfTapsRequired: Int, numberOfTouchesRequired: Int, selector: Selector) {
guard nil == gesture else {
return
}
gesture = UITapGestureRecognizer(target: self, action: selector)
gesture!.delegate = self
gesture!.numberOfTapsRequired = numberOfTapsRequired
gesture!.numberOfTouchesRequired = numberOfTouchesRequired
view.addGestureRecognizer(gesture!)
}
/**
Removes a given tap gesture.
- Parameter gesture: An optional UITapGestureRecognizer to remove.
*/
private func removeTapGesture(gesture: inout UITapGestureRecognizer?) {
guard let v = gesture else {
return
}
view.removeGestureRecognizer(v)
gesture = nil
}
}
extension CaptureController {
/**
Handler for the captureButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleCaptureButton(button: UIButton) {
switch capture.mode {
case .photo:
capture.session.captureStillImage()
case .video:
if capture.session.isRecording {
capture.session.stopRecording()
stopTimer()
} else {
capture.session.startRecording()
startTimer()
}
}
(delegate as? CaptureControllerDelegate)?.captureDidPressCaptureButton?(capture: capture, button: button)
}
/**
Handler for the cameraButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleCameraButton(button: UIButton) {
capture.mode = .photo
(delegate as? CaptureControllerDelegate)?.captureDidPressCameraButton?(capture: capture, button: button)
}
/**
Handler for the flashButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleFlashButton(button: UIButton) {
(delegate as? CaptureControllerDelegate)?.captureDidPressFlashButton?(capture: capture, button: button)
}
/**
Handler for the switchCameraButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleSwitchCamerasButton(button: UIButton) {
capture.session.switchCameras()
(delegate as? CaptureControllerDelegate)?.captureDidPressSwitchCamerasButton?(capture: capture, button: button)
}
/**
Handler for the videoButton.
- Parameter button: A UIButton that is associated with the event.
*/
@objc
internal func handleVideoButton(button: UIButton) {
capture.mode = .video
(delegate as? CaptureControllerDelegate)?.captureDidPressVideoButton?(capture: capture, button: button)
}
/**
Handler for the tapToFocusGesture.
- Parameter recognizer: A UITapGestureRecognizer that is associated with the event.
*/
@objc
internal func handleTapToFocusGesture(recognizer: UITapGestureRecognizer) {
guard isTapToFocusEnabled && capture.session.isFocusPointOfInterestSupported else {
return
}
let point = recognizer.location(in: view)
capture.session.focus(at: capture.preview.captureDevicePointOfInterestForPoint(point: point))
(delegate as? CaptureControllerDelegate)?.captureDidTapToFocusAtPoint?(capture: capture, point: point)
}
/**
Handler for the tapToExposeGesture.
- Parameter recognizer: A UITapGestureRecognizer that is associated with the event.
*/
@objc
internal func handleTapToExposeGesture(recognizer: UITapGestureRecognizer) {
guard isTapToExposeEnabled && capture.session.isExposurePointOfInterestSupported else {
return
}
let point = recognizer.location(in: view)
capture.session.expose(at: capture.preview.captureDevicePointOfInterestForPoint(point: point))
(delegate as? CaptureControllerDelegate)?.captureDidTapToExposeAtPoint?(capture: capture, point: point)
}
/**
Handler for the tapToResetGesture.
- Parameter recognizer: A UITapGestureRecognizer that is associated with the event.
*/
@objc
internal func handleTapToResetGesture(recognizer: UITapGestureRecognizer) {
guard isTapToResetEnabled else {
return
}
capture.session.reset()
let point = capture.preview.pointForCaptureDevicePointOfInterest(point: CGPoint(x: 0.5, y: 0.5))
(delegate as? CaptureControllerDelegate)?.captureDidTapToResetAtPoint?(capture: capture, point: point)
} }
} }
...@@ -222,23 +482,23 @@ extension CaptureController { ...@@ -222,23 +482,23 @@ extension CaptureController {
RunLoop.main.add(timer!, forMode: .commonModes) RunLoop.main.add(timer!, forMode: .commonModes)
delegate?.captureDidStartRecordTimer?(capture: self) (delegate as? CaptureControllerDelegate)?.captureDidStartRecordTimer?(capture: capture)
} }
/// Updates the timer when recording. /// Updates the timer when recording.
internal func updateTimer() { internal func updateTimer() {
let duration = session.recordedDuration let duration = capture.session.recordedDuration
let time = CMTimeGetSeconds(duration) let time = CMTimeGetSeconds(duration)
let hours = Int(time / 3600) let hours = Int(time / 3600)
let minutes = Int((time / 60).truncatingRemainder(dividingBy: 60)) let minutes = Int((time / 60).truncatingRemainder(dividingBy: 60))
let seconds = Int(time.truncatingRemainder(dividingBy: 60)) let seconds = Int(time.truncatingRemainder(dividingBy: 60))
delegate?.captureDidUpdateRecordTimer?(capture: self, hours: hours, minutes: minutes, seconds: seconds) (delegate as? CaptureControllerDelegate)?.captureDidUpdateRecordTimer?(capture: capture, hours: hours, minutes: minutes, seconds: seconds)
} }
/// Stops the timer when recording. /// Stops the timer when recording.
internal func stopTimer() { internal func stopTimer() {
let duration = session.recordedDuration let duration = capture.session.recordedDuration
let time = CMTimeGetSeconds(duration) let time = CMTimeGetSeconds(duration)
let hours = Int(time / 3600) let hours = Int(time / 3600)
let minutes = Int((time / 60).truncatingRemainder(dividingBy: 60)) let minutes = Int((time / 60).truncatingRemainder(dividingBy: 60))
...@@ -247,21 +507,6 @@ extension CaptureController { ...@@ -247,21 +507,6 @@ extension CaptureController {
timer?.invalidate() timer?.invalidate()
timer = nil timer = nil
delegate?.captureDidStopRecordTimer?(capture: self, hours: hours, minutes: minutes, seconds: seconds) (delegate as? CaptureControllerDelegate)?.captureDidStopRecordTimer?(capture: capture, hours: hours, minutes: minutes, seconds: seconds)
}
func captureDidPressCaptureButton(capture: Capture, button: UIButton) {
switch captureMode {
case .photo:
session.captureStillImage()
case .video:
if session.isRecording {
session.stopRecording()
stopTimer()
} else {
session.startRecording()
startTimer()
}
}
} }
} }
...@@ -59,6 +59,7 @@ open class StatusBarController: RootController { ...@@ -59,6 +59,7 @@ open class StatusBarController: RootController {
*/ */
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
statusBar.width = view.width
statusBar.zPosition = Device.isLandscape && .phone == Device.userInterfaceIdiom ? 0 : 3000 statusBar.zPosition = Device.isLandscape && .phone == Device.userInterfaceIdiom ? 0 : 3000
rootViewController.view.frame = view.bounds rootViewController.view.frame = view.bounds
} }
...@@ -78,6 +79,7 @@ open class StatusBarController: RootController { ...@@ -78,6 +79,7 @@ open class StatusBarController: RootController {
/// Prepares the statusBar. /// Prepares the statusBar.
private func prepareStatusBar() { private func prepareStatusBar() {
statusBar.backgroundColor = .white statusBar.backgroundColor = .white
view.layout(statusBar).top().horizontally().height(20) statusBar.height = 20
view.addSubview(statusBar)
} }
} }
...@@ -173,7 +173,7 @@ open class ToolbarController: StatusBarController { ...@@ -173,7 +173,7 @@ open class ToolbarController: StatusBarController {
super.layoutSubviews() super.layoutSubviews()
statusBar.layoutIfNeeded() statusBar.layoutIfNeeded()
let y = 0 == statusBar.zPosition ? 0 : statusBar.height let y = 0 == statusBar.zPosition || statusBar.isHidden ? 0 : statusBar.height
let p = y + toolbar.height let p = y + toolbar.height
toolbar.y = y toolbar.y = y
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment