Commit 16f980b2 by Daniel Dahan

added Reset Exposure and Focus with animation

parent cc1668fc
......@@ -41,7 +41,10 @@ class ViewController: UIViewController {
:description: General usage example.
*/
private func prepareGeneralCaptureViewExample() {
let v: CaptureView = CaptureView(frame: view.bounds)
v.switchCamerasButton = FabButton()
view.addSubview(v)
}
}
......@@ -30,6 +30,11 @@ public protocol CapturePreviewViewDelegate : MaterialDelegate {
:name: capturePreviewViewDidTapToExposeAtPoint
*/
optional func capturePreviewViewDidTapToExposeAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint)
/**
:name: capturePreviewViewDidTapToResetAtPoint
*/
optional func capturePreviewViewDidTapToResetAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint)
}
public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
......@@ -44,6 +49,11 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
private var tapToExposeGesture: UITapGestureRecognizer?
/**
:name: tapToResetGesture
*/
private var tapToResetGesture: UITapGestureRecognizer?
/**
:name: previewLayer
*/
public private(set) lazy var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer()
......@@ -59,7 +69,8 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
public var tapToFocusEnabled: Bool {
didSet {
if tapToFocusEnabled {
prepareTapGesture(&tapToFocusGesture, numberOfTapsRequired: 1, selector: "handleTapToFocusGesture:")
tapToResetEnabled = true
prepareTapGesture(&tapToFocusGesture, numberOfTapsRequired: 1, numberOfTouchesRequired: 1, selector: "handleTapToFocusGesture:")
if let v: UITapGestureRecognizer = tapToExposeGesture {
tapToFocusGesture!.requireGestureRecognizerToFail(v)
}
......@@ -75,7 +86,8 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
public var tapToExposeEnabled: Bool {
didSet {
if tapToExposeEnabled {
prepareTapGesture(&tapToExposeGesture, numberOfTapsRequired: 2, selector: "handleTapToExposeGesture:")
tapToResetEnabled = true
prepareTapGesture(&tapToExposeGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 1, selector: "handleTapToExposeGesture:")
if let v: UITapGestureRecognizer = tapToFocusGesture {
v.requireGestureRecognizerToFail(tapToExposeGesture!)
}
......@@ -86,11 +98,31 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
}
/**
:name: tapToResetEnabled
*/
public var tapToResetEnabled: Bool {
didSet {
if tapToResetEnabled {
prepareTapGesture(&tapToResetGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 2, selector: "handleTapToResetGesture:")
if let v: UITapGestureRecognizer = tapToFocusGesture {
v.requireGestureRecognizerToFail(tapToResetGesture!)
}
if let v: UITapGestureRecognizer = tapToExposeGesture {
v.requireGestureRecognizerToFail(tapToResetGesture!)
}
} else {
removeTapGesture(&tapToResetGesture)
}
}
}
/**
:name: init
*/
public required init?(coder aDecoder: NSCoder) {
tapToFocusEnabled = true
tapToExposeEnabled = true
tapToResetEnabled = true
super.init(coder: aDecoder)
}
......@@ -100,6 +132,7 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
public override init(frame: CGRect) {
tapToFocusEnabled = true
tapToExposeEnabled = true
tapToResetEnabled = true
super.init(frame: frame)
}
......@@ -167,6 +200,15 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
}
/**
:name: handleTapToResetGesture
*/
internal func handleTapToResetGesture(recognizer: UITapGestureRecognizer) {
if tapToResetEnabled {
(delegate as? CapturePreviewViewDelegate)?.capturePreviewViewDidTapToResetAtPoint?(self, point: pointForCaptureDevicePointOfInterest(CGPointMake(0.5, 0.5)))
}
}
/**
:name: preparePreviewLayer
*/
private func preparePreviewLayer() {
......@@ -187,10 +229,12 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
/**
:name: prepareTapGesture
*/
private func prepareTapGesture(inout gesture: UITapGestureRecognizer?, numberOfTapsRequired: Int, selector: Selector) {
private func prepareTapGesture(inout gesture: UITapGestureRecognizer?, numberOfTapsRequired: Int, numberOfTouchesRequired: Int, selector: Selector) {
removeTapGesture(&gesture)
gesture = UITapGestureRecognizer(target: self, action: selector)
gesture!.delegate = self
gesture!.numberOfTapsRequired = numberOfTapsRequired
gesture!.numberOfTouchesRequired = numberOfTouchesRequired
addGestureRecognizer(gesture!)
}
......
......@@ -314,6 +314,30 @@ public class CaptureSession : NSObject {
}
/**
:name: resetFocusAndExposureModes
*/
public func resetFocusAndExposureModes() {
let device: AVCaptureDevice = activeCamera!
let canResetFocus: Bool = device.focusPointOfInterestSupported && device.isFocusModeSupported(.ContinuousAutoFocus)
let canResetExposure: Bool = device.exposurePointOfInterestSupported && device.isExposureModeSupported(.ContinuousAutoExposure)
let centerPoint: CGPoint = CGPointMake(0.5, 0.5)
do {
try device.lockForConfiguration()
if canResetFocus {
device.focusMode = .ContinuousAutoFocus
device.focusPointOfInterest = centerPoint
}
if canResetExposure {
device.exposureMode = .ContinuousAutoExposure
device.exposurePointOfInterest = centerPoint
}
device.unlockForConfiguration()
} catch let e as NSError {
self.delegate?.captureSessionFailedWithError?(self, error: e)
}
}
/**
:name: prepareSession
*/
private func prepareSession() {
......
......@@ -164,6 +164,14 @@ public class CaptureView : MaterialView, CaptureSessionDelegate, CapturePreviewV
}
/**
:name: capturePreviewViewDidTapToResetAtPoint
*/
public func capturePreviewViewDidTapToResetAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint) {
capturePreviewViewDidTapToFocusAtPoint(capturePreviewView, point: point)
capturePreviewViewDidTapToExposeAtPoint(capturePreviewView, point: point)
}
/**
:name: prepareView
*/
public override func prepareView() {
......
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