Commit 37eec866 by Daniel Dahan

added tap focus, tap exposure, tap reset layers

parent 2685c8f6
...@@ -26,10 +26,9 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -26,10 +26,9 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
lazy var closeButton: FlatButton = FlatButton() lazy var closeButton: FlatButton = FlatButton()
lazy var cameraButton: FlatButton = FlatButton() lazy var cameraButton: FlatButton = FlatButton()
lazy var videoButton: FlatButton = FlatButton() lazy var videoButton: FlatButton = FlatButton()
lazy var switchCameraButton: FlatButton = FlatButton() lazy var switchCamerasButton: FlatButton = FlatButton()
lazy var flashButton: FlatButton = FlatButton() lazy var flashButton: FlatButton = FlatButton()
lazy var captureButton: FabButton = FabButton() lazy var captureButton: FabButton = FabButton()
lazy var captureMode: CaptureMode = .Video
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
...@@ -38,15 +37,10 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -38,15 +37,10 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
prepareCameraButton() prepareCameraButton()
prepareVideoButton() prepareVideoButton()
prepareCloseButton() prepareCloseButton()
prepareSwitchCameraButton() prepareSwitchCamerasButton()
prepareFlashButton() prepareFlashButton()
prepareNavigationBarView()
prepareCaptureView() prepareCaptureView()
} prepareNavigationBarView()
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
} }
/** /**
...@@ -75,6 +69,13 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -75,6 +69,13 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
*/ */
func captureDidStartRecordingToOutputFileAtURL(capture: CaptureSession, captureOutput: AVCaptureFileOutput, fileURL: NSURL, fromConnections connections: [AnyObject]) { func captureDidStartRecordingToOutputFileAtURL(capture: CaptureSession, captureOutput: AVCaptureFileOutput, fileURL: NSURL, fromConnections connections: [AnyObject]) {
print("Capture Started Recording \(fileURL)") print("Capture Started Recording \(fileURL)")
cameraButton.hidden = true
videoButton.hidden = true
switchCamerasButton.hidden = true
flashButton.hidden = true
navigationBarView.backgroundColor = nil
} }
/** /**
...@@ -82,6 +83,13 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -82,6 +83,13 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
*/ */
func captureDidFinishRecordingToOutputFileAtURL(capture: CaptureSession, captureOutput: AVCaptureFileOutput, outputFileURL: NSURL, fromConnections connections: [AnyObject], error: NSError!) { func captureDidFinishRecordingToOutputFileAtURL(capture: CaptureSession, captureOutput: AVCaptureFileOutput, outputFileURL: NSURL, fromConnections connections: [AnyObject], error: NSError!) {
print("Capture Stopped Recording \(outputFileURL)") print("Capture Stopped Recording \(outputFileURL)")
cameraButton.hidden = false
videoButton.hidden = false
switchCamerasButton.hidden = false
flashButton.hidden = false
navigationBarView.backgroundColor = MaterialColor.black.colorWithAlphaComponent(0.3)
} }
/** /**
...@@ -91,7 +99,6 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -91,7 +99,6 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
MaterialAnimation.animationDisabled { MaterialAnimation.animationDisabled {
self.navigationBarView.titleLabel!.text = String(format: "%02i:%02i:%02i", arguments: [hours, minutes, seconds]) self.navigationBarView.titleLabel!.text = String(format: "%02i:%02i:%02i", arguments: [hours, minutes, seconds])
} }
} }
/** /**
...@@ -127,117 +134,66 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -127,117 +134,66 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
} }
} }
func captureViewDidPressFlashButton(captureView: CaptureView, button: UIButton) {
if .Back == captureView.captureSession.cameraPosition {
var img: UIImage?
switch captureView.captureSession.flashMode {
case .Off:
img = UIImage(named: "ic_flash_on_white")
captureView.captureSession.flashMode = .On
case .On:
img = UIImage(named: "ic_flash_auto_white")
captureView.captureSession.flashMode = .Auto
case .Auto:
img = UIImage(named: "ic_flash_off_white")
captureView.captureSession.flashMode = .Off
}
button.setImage(img, forState: .Normal)
button.setImage(img, forState: .Highlighted)
}
}
/** /**
:name: handleCameraButton :name: captureViewDidPressCameraButton
*/ */
internal func handleCameraButton(button: UIButton) { func captureViewDidPressCameraButton(captureView: CaptureView, button: UIButton) {
captureButton.backgroundColor = MaterialColor.blue.darken1.colorWithAlphaComponent(0.3) captureButton.backgroundColor = MaterialColor.blue.darken1.colorWithAlphaComponent(0.3)
captureMode = .Photo
} }
/** /**
:name: handleVideoButton :name: captureViewDidPressVideoButton
*/ */
internal func handleVideoButton(button: UIButton) { func captureViewDidPressVideoButton(captureView: CaptureView, button: UIButton) {
captureButton.backgroundColor = MaterialColor.red.darken1.colorWithAlphaComponent(0.3) captureButton.backgroundColor = MaterialColor.red.darken1.colorWithAlphaComponent(0.3)
captureMode = .Video
} }
/** /**
:name: handleCaptureButton :name: captureViewDidPressCaptureButton
*/ */
internal func handleCaptureButton(button: UIButton) { func captureViewDidPressCaptureButton(captureView: CaptureView, button: UIButton) {
if .Photo == captureMode { if .Photo == captureView.captureMode {
captureView.captureSession.captureStillImage() // ... do something
} else if .Video == captureMode { } else if .Video == captureView.captureMode {
if captureView.captureSession.isRecording { // ... do something
captureView.captureSession.stopRecording()
captureView.stopTimer()
cameraButton.hidden = false
videoButton.hidden = false
if let v: Array<UIButton> = navigationBarView.leftButtons {
for x in v {
x.hidden = false
}
}
if let v: Array<UIButton> = navigationBarView.rightButtons {
for x in v {
x.hidden = false
}
}
navigationBarView.backgroundColor = MaterialColor.black.colorWithAlphaComponent(0.3)
} else {
captureView.previewView.layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
captureView.captureSession.startRecording()
captureView.startTimer()
cameraButton.hidden = true
videoButton.hidden = true
if let v: Array<UIButton> = navigationBarView.leftButtons {
for x in v {
x.hidden = true
}
}
if let v: Array<UIButton> = navigationBarView.rightButtons {
for x in v {
x.hidden = true
}
}
navigationBarView.backgroundColor = nil
}
} }
} }
/** /**
:name: handleSwitchCameraButton :name: captureViewDidPressSwitchCamerasButton
*/ */
internal func handleSwitchCameraButton(button: UIButton) { func captureViewDidPressSwitchCamerasButton(captureView: CaptureView, button: UIButton) {
var img: UIImage? var img: UIImage?
captureView.previewView.layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
if .Back == captureView.captureSession.cameraPosition { if .Back == captureView.captureSession.cameraPosition {
img = UIImage(named: "ic_camera_rear_white")
captureView.captureSession.switchCameras()
} else if .Front == captureView.captureSession.cameraPosition {
img = UIImage(named: "ic_camera_front_white") img = UIImage(named: "ic_camera_front_white")
captureView.captureSession.switchCameras() } else if .Front == captureView.captureSession.cameraPosition {
} img = UIImage(named: "ic_camera_rear_white")
switchCameraButton.setImage(img, forState: .Normal)
switchCameraButton.setImage(img, forState: .Highlighted)
}
/**
:name: handleFlashButton
*/
internal func handleFlashButton(button: UIButton) {
if .Back == captureView.captureSession.cameraPosition {
var img: UIImage?
switch captureView.captureSession.flashMode {
case .Off:
img = UIImage(named: "ic_flash_on_white")
captureView.captureSession.flashMode = .On
case .On:
img = UIImage(named: "ic_flash_auto_white")
captureView.captureSession.flashMode = .Auto
case .Auto:
img = UIImage(named: "ic_flash_off_white")
captureView.captureSession.flashMode = .Off
} }
flashButton.setImage(img, forState: .Normal) switchCamerasButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted) switchCamerasButton.setImage(img, forState: .Highlighted)
}
} }
/** /**
...@@ -252,8 +208,11 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -252,8 +208,11 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
*/ */
private func prepareCaptureView() { private func prepareCaptureView() {
view.addSubview(captureView) view.addSubview(captureView)
captureView.tapToFocusEnabled = true
captureView.tapToExposeEnabled = true
captureView.translatesAutoresizingMaskIntoConstraints = false captureView.translatesAutoresizingMaskIntoConstraints = false
captureView.delegate = self captureView.delegate = self
captureView.captureSession.delegate = self
MaterialLayout.alignToParent(view, child: captureView) MaterialLayout.alignToParent(view, child: captureView)
} }
...@@ -283,7 +242,7 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -283,7 +242,7 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
navigationBarView.detailLabel = detailLabel navigationBarView.detailLabel = detailLabel
navigationBarView.leftButtons = [closeButton] navigationBarView.leftButtons = [closeButton]
navigationBarView.rightButtons = [switchCameraButton, flashButton] navigationBarView.rightButtons = [switchCamerasButton, flashButton]
view.addSubview(navigationBarView) view.addSubview(navigationBarView)
navigationBarView.translatesAutoresizingMaskIntoConstraints = false navigationBarView.translatesAutoresizingMaskIntoConstraints = false
...@@ -304,8 +263,6 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -304,8 +263,6 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
captureButton.borderWidth = .Border2 captureButton.borderWidth = .Border2
captureButton.borderColor = MaterialColor.white captureButton.borderColor = MaterialColor.white
captureButton.shadowDepth = .None captureButton.shadowDepth = .None
captureButton.addTarget(self, action: "handleCaptureButton:", forControlEvents: .TouchUpInside)
view.addSubview(captureButton)
captureView.captureButton = captureButton captureView.captureButton = captureButton
} }
...@@ -315,10 +272,11 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -315,10 +272,11 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
*/ */
private func prepareCameraButton() { private func prepareCameraButton() {
let img4: UIImage? = UIImage(named: "ic_photo_camera_white_36pt") let img4: UIImage? = UIImage(named: "ic_photo_camera_white_36pt")
cameraButton.width = 72
cameraButton.height = 72
cameraButton.pulseColor = nil cameraButton.pulseColor = nil
cameraButton.setImage(img4, forState: .Normal) cameraButton.setImage(img4, forState: .Normal)
cameraButton.setImage(img4, forState: .Highlighted) cameraButton.setImage(img4, forState: .Highlighted)
cameraButton.addTarget(self, action: "handleCameraButton:", forControlEvents: .TouchUpInside)
captureView.cameraButton = cameraButton captureView.cameraButton = cameraButton
} }
...@@ -328,10 +286,11 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -328,10 +286,11 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
*/ */
private func prepareVideoButton() { private func prepareVideoButton() {
let img5: UIImage? = UIImage(named: "ic_videocam_white_36pt") let img5: UIImage? = UIImage(named: "ic_videocam_white_36pt")
videoButton.width = 72
videoButton.height = 72
videoButton.pulseColor = nil videoButton.pulseColor = nil
videoButton.setImage(img5, forState: .Normal) videoButton.setImage(img5, forState: .Normal)
videoButton.setImage(img5, forState: .Highlighted) videoButton.setImage(img5, forState: .Highlighted)
videoButton.addTarget(self, action: "handleVideoButton:", forControlEvents: .TouchUpInside)
captureView.videoButton = videoButton captureView.videoButton = videoButton
} }
...@@ -344,35 +303,30 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg ...@@ -344,35 +303,30 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
closeButton.pulseColor = nil closeButton.pulseColor = nil
closeButton.setImage(img, forState: .Normal) closeButton.setImage(img, forState: .Normal)
closeButton.setImage(img, forState: .Highlighted) closeButton.setImage(img, forState: .Highlighted)
captureView.closeButton = closeButton
} }
/** /**
:name: prepareSwitchCameraButton :name: prepareSwitchCamerasButton
*/ */
private func prepareSwitchCameraButton() { private func prepareSwitchCamerasButton() {
let img: UIImage? = UIImage(named: "ic_camera_front_white") let img: UIImage? = UIImage(named: "ic_camera_front_white")
switchCameraButton.pulseColor = nil switchCamerasButton.pulseColor = nil
switchCameraButton.setImage(img, forState: .Normal) switchCamerasButton.setImage(img, forState: .Normal)
switchCameraButton.setImage(img, forState: .Highlighted) switchCamerasButton.setImage(img, forState: .Highlighted)
switchCameraButton.addTarget(self, action: "handleSwitchCameraButton:", forControlEvents: .TouchUpInside)
captureView.switchCameraButton = switchCameraButton captureView.switchCamerasButton = switchCamerasButton
} }
/** /**
:name: prepareFlashButton :name: prepareFlashButton
*/ */
private func prepareFlashButton() { private func prepareFlashButton() {
captureView.captureSession.flashMode = .Auto
let img: UIImage? = UIImage(named: "ic_flash_auto_white") let img: UIImage? = UIImage(named: "ic_flash_auto_white")
flashButton.pulseColor = nil flashButton.pulseColor = nil
flashButton.setImage(img, forState: .Normal) flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted) flashButton.setImage(img, forState: .Highlighted)
flashButton.addTarget(self, action: "handleFlashButton:", forControlEvents: .TouchUpInside)
captureView.captureSession.flashMode = .Auto
captureView.flashButton = flashButton captureView.flashButton = flashButton
} }
} }
...@@ -392,7 +392,7 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -392,7 +392,7 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
/** /**
:name: switchCameras :name: switchCameras
*/ */
public func switchCameras() { public func switchCameras(completion: ((success: Bool) -> Void)? = nil) {
if canSwitchCameras { if canSwitchCameras {
do { do {
self.delegate?.captureSessionWillSwitchCameras?(self, position: self.cameraPosition) self.delegate?.captureSessionWillSwitchCameras?(self, position: self.cameraPosition)
...@@ -407,8 +407,10 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -407,8 +407,10 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
self.session.addInput(self.activeVideoInput) self.session.addInput(self.activeVideoInput)
} }
self.session.commitConfiguration() self.session.commitConfiguration()
completion?(success: true)
self.delegate?.captureSessionDidSwitchCameras?(self, position: self.cameraPosition) self.delegate?.captureSessionDidSwitchCameras?(self, position: self.cameraPosition)
} catch let e as NSError { } catch let e as NSError {
completion?(success: false)
self.delegate?.captureSessionFailedWithError?(self, error: e) self.delegate?.captureSessionFailedWithError?(self, error: e)
} }
} }
...@@ -560,7 +562,7 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -560,7 +562,7 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
:name: startRecording :name: startRecording
*/ */
public func startRecording() { public func startRecording() {
if !self.isRecording { if !isRecording {
dispatch_async(sessionQueue) { dispatch_async(sessionQueue) {
let connection: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo) let connection: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo)
connection.videoOrientation = self.currentVideoOrientation connection.videoOrientation = self.currentVideoOrientation
......
...@@ -55,6 +55,31 @@ public protocol CaptureViewDelegate : MaterialDelegate { ...@@ -55,6 +55,31 @@ public protocol CaptureViewDelegate : MaterialDelegate {
:name: captureViewDidTapToResetAtPoint :name: captureViewDidTapToResetAtPoint
*/ */
optional func captureViewDidTapToResetAtPoint(captureView: CaptureView, point: CGPoint) optional func captureViewDidTapToResetAtPoint(captureView: CaptureView, point: CGPoint)
/**
:name: captureViewDidPressFlashButton
*/
optional func captureViewDidPressFlashButton(captureView: CaptureView, button: UIButton)
/**
:name: captureViewDidPressSwitchCamerasButton
*/
optional func captureViewDidPressSwitchCamerasButton(captureView: CaptureView, button: UIButton)
/**
:name: captureViewDidPressCaptureButton
*/
optional func captureViewDidPressCaptureButton(captureView: CaptureView, button: UIButton)
/**
:name: captureViewDidPressCameraButton
*/
optional func captureViewDidPressCameraButton(captureView: CaptureView, button: UIButton)
/**
:name: captureViewDidPressVideoButton
*/
optional func captureViewDidPressVideoButton(captureView: CaptureView, button: UIButton)
} }
public class CaptureView : MaterialView, UIGestureRecognizerDelegate { public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
...@@ -79,11 +104,52 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -79,11 +104,52 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
private var tapToResetGesture: UITapGestureRecognizer? private var tapToResetGesture: UITapGestureRecognizer?
/** /**
:name: tapToFocusEnabled
*/
public var tapToFocusEnabled: Bool = false {
didSet {
if tapToFocusEnabled {
tapToResetEnabled = true
prepareFocusLayer()
prepareTapGesture(&tapToFocusGesture, numberOfTapsRequired: 1, numberOfTouchesRequired: 1, selector: "handleTapToFocusGesture:")
if let v: UITapGestureRecognizer = tapToExposeGesture {
tapToFocusGesture!.requireGestureRecognizerToFail(v)
}
} else {
removeTapGesture(&tapToFocusGesture)
focusLayer?.removeFromSuperlayer()
focusLayer = nil
}
}
}
/**
:name: tapToExposeEnabled
*/
public var tapToExposeEnabled: Bool = false {
didSet {
if tapToExposeEnabled {
tapToResetEnabled = true
prepareExposureLayer()
prepareTapGesture(&tapToExposeGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 1, selector: "handleTapToExposeGesture:")
if let v: UITapGestureRecognizer = tapToFocusGesture {
v.requireGestureRecognizerToFail(tapToExposeGesture!)
}
} else {
removeTapGesture(&tapToExposeGesture)
exposureLayer?.removeFromSuperlayer()
exposureLayer = nil
}
}
}
/**
:name: tapToResetEnabled :name: tapToResetEnabled
*/ */
private var tapToResetEnabled: Bool = false { private var tapToResetEnabled: Bool = false {
didSet { didSet {
if tapToResetEnabled { if tapToResetEnabled {
prepareResetLayer()
prepareTapGesture(&tapToResetGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 2, selector: "handleTapToResetGesture:") prepareTapGesture(&tapToResetGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 2, selector: "handleTapToResetGesture:")
if let v: UITapGestureRecognizer = tapToFocusGesture { if let v: UITapGestureRecognizer = tapToFocusGesture {
v.requireGestureRecognizerToFail(tapToResetGesture!) v.requireGestureRecognizerToFail(tapToResetGesture!)
...@@ -93,11 +159,18 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -93,11 +159,18 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
} }
} else { } else {
removeTapGesture(&tapToResetGesture) removeTapGesture(&tapToResetGesture)
resetLayer?.removeFromSuperlayer()
resetLayer = nil
} }
} }
} }
/** /**
:name: captureMode
*/
public private(set) lazy var captureMode: CaptureMode = .Video
/**
:name: contentInsets :name: contentInsets
*/ */
public var contentInsets: MaterialEdgeInsets = .None { public var contentInsets: MaterialEdgeInsets = .None {
...@@ -126,52 +199,28 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -126,52 +199,28 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
public private(set) lazy var captureSession: CaptureSession = CaptureSession() public private(set) lazy var captureSession: CaptureSession = CaptureSession()
/** /**
:name: focusView :name: focusLayer
*/ */
public var focusView: MaterialView? { public private(set) var focusLayer: MaterialLayer?
didSet {
if nil == focusView {
removeTapGesture(&tapToFocusGesture)
} else {
tapToResetEnabled = true
prepareFocusView()
prepareTapGesture(&tapToFocusGesture, numberOfTapsRequired: 1, numberOfTouchesRequired: 1, selector: "handleTapToFocusGesture:")
if let v: UITapGestureRecognizer = tapToExposeGesture {
tapToFocusGesture!.requireGestureRecognizerToFail(v)
}
}
}
}
/** /**
:name: exposureView :name: exposureLayer
*/ */
public var exposureView: MaterialView? { public private(set) var exposureLayer: MaterialLayer?
didSet {
if nil == exposureView {
removeTapGesture(&tapToExposeGesture)
} else {
tapToResetEnabled = true
prepareExposureView()
prepareTapGesture(&tapToExposeGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 1, selector: "handleTapToExposeGesture:")
if let v: UITapGestureRecognizer = tapToFocusGesture {
v.requireGestureRecognizerToFail(tapToExposeGesture!)
}
}
}
}
/** /**
:name: closeButton :name: resetLayer
*/ */
public var closeButton: MaterialButton? public private(set) var resetLayer: MaterialLayer?
/** /**
:name: cameraButton :name: cameraButton
*/ */
public var cameraButton: MaterialButton? { public var cameraButton: UIButton? {
didSet { didSet {
cameraButton?.translatesAutoresizingMaskIntoConstraints = false if let v: UIButton = cameraButton {
v.addTarget(self, action: "handleCameraButton:", forControlEvents: .TouchUpInside)
}
reloadView() reloadView()
} }
} }
...@@ -179,27 +228,49 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -179,27 +228,49 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
/** /**
:name: captureButton :name: captureButton
*/ */
public var captureButton: MaterialButton? public var captureButton: UIButton? {
didSet {
if let v: UIButton = captureButton {
v.addTarget(self, action: "handleCaptureButton:", forControlEvents: .TouchUpInside)
}
reloadView()
}
}
/** /**
:name: videoButton :name: videoButton
*/ */
public var videoButton: MaterialButton? { public var videoButton: UIButton? {
didSet { didSet {
videoButton?.translatesAutoresizingMaskIntoConstraints = false if let v: UIButton = videoButton {
v.addTarget(self, action: "handleVideoButton:", forControlEvents: .TouchUpInside)
}
reloadView() reloadView()
} }
} }
/** /**
:name: switchCameraButton :name: switchCamerasButton
*/ */
public var switchCameraButton: MaterialButton? public var switchCamerasButton: UIButton? {
didSet {
if let v: UIButton = switchCamerasButton {
v.addTarget(self, action: "handleSwitchCamerasButton:", forControlEvents: .TouchUpInside)
}
}
}
/** /**
:name: flashButton :name: flashButton
*/ */
public var flashButton: MaterialButton? public var flashButton: UIButton? {
didSet {
if let v: UIButton = flashButton {
v.addTarget(self, action: "handleFlashButton:", forControlEvents: .TouchUpInside)
}
}
}
/** /**
:name: init :name: init
...@@ -213,9 +284,17 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -213,9 +284,17 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
*/ */
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
if let v: MaterialButton = captureButton { if let v: UIButton = cameraButton {
v.y = bounds.height - contentInsetsRef.bottom - v.height v.frame.origin.y = bounds.height - contentInsetsRef.bottom - v.bounds.height
v.x = (bounds.width - v.width) / 2 v.frame.origin.x = contentInsetsRef.left
}
if let v: UIButton = captureButton {
v.frame.origin.y = bounds.height - contentInsetsRef.bottom - v.bounds.height
v.frame.origin.x = (bounds.width - v.bounds.width) / 2
}
if let v: UIButton = videoButton {
v.frame.origin.y = bounds.height - contentInsetsRef.bottom - v.bounds.height
v.frame.origin.x = bounds.width - v.bounds.width - contentInsetsRef.right
} }
(previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = captureSession.currentVideoOrientation (previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = captureSession.currentVideoOrientation
} }
...@@ -254,21 +333,23 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -254,21 +333,23 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
addSubview(previewView) addSubview(previewView)
MaterialLayout.alignToParent(self, child: previewView) MaterialLayout.alignToParent(self, child: previewView)
if let v: MaterialButton = cameraButton { if let v: UIButton = captureButton {
addSubview(v)
}
if let v: UIButton = cameraButton {
addSubview(v) addSubview(v)
MaterialLayout.alignFromBottomLeft(self, child: v, bottom: contentInsetsRef.bottom, left: contentInsetsRef.left)
} }
if let v: MaterialButton = videoButton { if let v: UIButton = videoButton {
addSubview(v) addSubview(v)
MaterialLayout.alignFromBottomRight(self, child: v, bottom: contentInsetsRef.bottom, right: contentInsetsRef.right)
} }
} }
/** /**
:name: startTimer :name: startTimer
*/ */
public func startTimer() { internal func startTimer() {
timer?.invalidate() timer?.invalidate()
timer = NSTimer(timeInterval: 0.5, target: self, selector: "updateTimer", userInfo: nil, repeats: true) timer = NSTimer(timeInterval: 0.5, target: self, selector: "updateTimer", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes) NSRunLoop.mainRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
...@@ -278,7 +359,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -278,7 +359,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
/** /**
:name: updateTimer :name: updateTimer
*/ */
public func updateTimer() { internal func updateTimer() {
let duration: CMTime = captureSession.recordedDuration let duration: CMTime = captureSession.recordedDuration
let time: Double = CMTimeGetSeconds(duration) let time: Double = CMTimeGetSeconds(duration)
let hours: Int = Int(time / 3600) let hours: Int = Int(time / 3600)
...@@ -290,7 +371,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -290,7 +371,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
/** /**
:name: stopTimer :name: stopTimer
*/ */
public func stopTimer() { internal func stopTimer() {
let duration: CMTime = captureSession.recordedDuration let duration: CMTime = captureSession.recordedDuration
let time: Double = CMTimeGetSeconds(duration) let time: Double = CMTimeGetSeconds(duration)
let hours: Int = Int(time / 3600) let hours: Int = Int(time / 3600)
...@@ -302,29 +383,66 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -302,29 +383,66 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
} }
/** /**
:name: handleTapToFocusGesture :name: handleFlashButton
*/ */
internal func handleTapToFocusGesture(recognizer: UITapGestureRecognizer) { internal func handleFlashButton(button: UIButton) {
if let v: MaterialView = focusView { (delegate as? CaptureViewDelegate)?.captureViewDidPressFlashButton?(self, button: button)
if captureSession.cameraSupportsTapToFocus {
let point: CGPoint = recognizer.locationInView(self)
captureSession.focusAtPoint(previewView.captureDevicePointOfInterestForPoint(point))
MaterialAnimation.animationDisabled {
v.layer.transform = CATransform3DIdentity
v.position = point
v.hidden = false
} }
MaterialAnimation.animateWithDuration(0.25, animations: {
v.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1) /**
}) { :name: handleSwitchCamerasButton
MaterialAnimation.delay(0.4) { */
MaterialAnimation.animationDisabled { internal func handleSwitchCamerasButton(button: UIButton) {
v.hidden = true previewView.layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
captureSession.switchCameras { (success: Bool) in
(self.delegate as? CaptureViewDelegate)?.captureViewDidPressSwitchCamerasButton?(self, button: button)
} }
} }
/**
:name: handleCaptureButton
*/
internal func handleCaptureButton(button: UIButton) {
if .Photo == captureMode {
captureSession.captureStillImage()
} else if .Video == captureMode {
if captureSession.isRecording {
captureSession.stopRecording()
stopTimer()
} else {
previewView.layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
captureSession.startRecording()
startTimer()
} }
(delegate as? CaptureViewDelegate)?.captureViewDidTapToFocusAtPoint?(self, point: point)
} }
(delegate as? CaptureViewDelegate)?.captureViewDidPressCaptureButton?(self, button: button)
}
/**
:name: handleCameraButton
*/
internal func handleCameraButton(button: UIButton) {
captureMode = .Photo
(delegate as? CaptureViewDelegate)?.captureViewDidPressCameraButton?(self, button: button)
}
/**
:name: handleVideoButton
*/
internal func handleVideoButton(button: UIButton) {
captureMode = .Video
(delegate as? CaptureViewDelegate)?.captureViewDidPressVideoButton?(self, button: button)
}
/**
:name: handleTapToFocusGesture
*/
internal func handleTapToFocusGesture(recognizer: UITapGestureRecognizer) {
if tapToFocusEnabled && captureSession.cameraSupportsTapToFocus {
let point: CGPoint = recognizer.locationInView(self)
captureSession.focusAtPoint(previewView.captureDevicePointOfInterestForPoint(point))
animateTapLayer(layer: focusLayer!, point: point)
(delegate as? CaptureViewDelegate)?.captureViewDidTapToFocusAtPoint?(self, point: point)
} }
} }
...@@ -332,9 +450,10 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -332,9 +450,10 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
:name: handleTapToExposeGesture :name: handleTapToExposeGesture
*/ */
internal func handleTapToExposeGesture(recognizer: UITapGestureRecognizer) { internal func handleTapToExposeGesture(recognizer: UITapGestureRecognizer) {
if nil != exposureView && captureSession.cameraSupportsTapToExpose { if tapToExposeEnabled && captureSession.cameraSupportsTapToExpose {
let point: CGPoint = recognizer.locationInView(self) let point: CGPoint = recognizer.locationInView(self)
captureSession.exposeAtPoint(previewView.captureDevicePointOfInterestForPoint(point)) captureSession.exposeAtPoint(previewView.captureDevicePointOfInterestForPoint(point))
animateTapLayer(layer: exposureLayer!, point: point)
(delegate as? CaptureViewDelegate)?.captureViewDidTapToExposeAtPoint?(self, point: point) (delegate as? CaptureViewDelegate)?.captureViewDidTapToExposeAtPoint?(self, point: point)
} }
} }
...@@ -346,6 +465,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -346,6 +465,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
if tapToResetEnabled { if tapToResetEnabled {
captureSession.resetFocusAndExposureModes() captureSession.resetFocusAndExposureModes()
let point: CGPoint = previewView.pointForCaptureDevicePointOfInterest(CGPointMake(0.5, 0.5)) let point: CGPoint = previewView.pointForCaptureDevicePointOfInterest(CGPointMake(0.5, 0.5))
animateTapLayer(layer: resetLayer!, point: point)
(delegate as? CaptureViewDelegate)?.captureViewDidTapToResetAtPoint?(self, point: point) (delegate as? CaptureViewDelegate)?.captureViewDidTapToResetAtPoint?(self, point: point)
} }
} }
...@@ -382,20 +502,61 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate { ...@@ -382,20 +502,61 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
} }
/** /**
:name: prepareFocusView :name: prepareFocusLayer
*/ */
private func prepareFocusView() { private func prepareFocusLayer() {
if let v: MaterialView = focusView { if nil == focusLayer {
v.hidden = true focusLayer = MaterialLayer(frame: CGRectMake(0, 0, 150, 150))
focusLayer!.hidden = true
focusLayer!.borderWidth = 2
focusLayer!.borderColor = MaterialColor.white.CGColor
previewView.layer.addSublayer(focusLayer!)
}
}
/**
:name: prepareExposureLayer
*/
private func prepareExposureLayer() {
if nil == exposureLayer {
exposureLayer = MaterialLayer(frame: CGRectMake(0, 0, 150, 150))
exposureLayer!.hidden = true
exposureLayer!.borderWidth = 2
exposureLayer!.borderColor = MaterialColor.yellow.darken1.CGColor
previewView.layer.addSublayer(exposureLayer!)
} }
} }
/** /**
:name: prepareExposureView :name: prepareResetLayer
*/ */
private func prepareExposureView() { private func prepareResetLayer() {
if let v: MaterialView = exposureView { if nil == resetLayer {
resetLayer = MaterialLayer(frame: CGRectMake(0, 0, 150, 150))
resetLayer!.hidden = true
resetLayer!.borderWidth = 2
resetLayer!.borderColor = MaterialColor.purple.darken1.CGColor
previewView.layer.addSublayer(resetLayer!)
}
}
/**
:name: animateTapLayer
*/
private func animateTapLayer(layer v: MaterialLayer, point: CGPoint) {
MaterialAnimation.animationDisabled {
v.transform = CATransform3DIdentity
v.position = point
v.hidden = false
}
MaterialAnimation.animateWithDuration(0.25, animations: {
v.transform = CATransform3DMakeScale(0.5, 0.5, 1)
}) {
MaterialAnimation.delay(0.4) {
MaterialAnimation.animationDisabled {
v.hidden = true v.hidden = true
} }
} }
}
}
} }
\ No newline at end of file
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