Commit 16f980b2 by Daniel Dahan

added Reset Exposure and Focus with animation

parent cc1668fc
...@@ -41,7 +41,10 @@ class ViewController: UIViewController { ...@@ -41,7 +41,10 @@ class ViewController: UIViewController {
:description: General usage example. :description: General usage example.
*/ */
private func prepareGeneralCaptureViewExample() { private func prepareGeneralCaptureViewExample() {
let v: CaptureView = CaptureView(frame: view.bounds)
v.switchCamerasButton = FabButton()
view.addSubview(v)
} }
} }
...@@ -30,6 +30,11 @@ public protocol CapturePreviewViewDelegate : MaterialDelegate { ...@@ -30,6 +30,11 @@ public protocol CapturePreviewViewDelegate : MaterialDelegate {
:name: capturePreviewViewDidTapToExposeAtPoint :name: capturePreviewViewDidTapToExposeAtPoint
*/ */
optional func capturePreviewViewDidTapToExposeAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint) optional func capturePreviewViewDidTapToExposeAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint)
/**
:name: capturePreviewViewDidTapToResetAtPoint
*/
optional func capturePreviewViewDidTapToResetAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint)
} }
public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
...@@ -44,6 +49,11 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -44,6 +49,11 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
private var tapToExposeGesture: UITapGestureRecognizer? private var tapToExposeGesture: UITapGestureRecognizer?
/** /**
:name: tapToResetGesture
*/
private var tapToResetGesture: UITapGestureRecognizer?
/**
:name: previewLayer :name: previewLayer
*/ */
public private(set) lazy var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer() public private(set) lazy var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer()
...@@ -59,7 +69,8 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -59,7 +69,8 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
public var tapToFocusEnabled: Bool { public var tapToFocusEnabled: Bool {
didSet { didSet {
if tapToFocusEnabled { if tapToFocusEnabled {
prepareTapGesture(&tapToFocusGesture, numberOfTapsRequired: 1, selector: "handleTapToFocusGesture:") tapToResetEnabled = true
prepareTapGesture(&tapToFocusGesture, numberOfTapsRequired: 1, numberOfTouchesRequired: 1, selector: "handleTapToFocusGesture:")
if let v: UITapGestureRecognizer = tapToExposeGesture { if let v: UITapGestureRecognizer = tapToExposeGesture {
tapToFocusGesture!.requireGestureRecognizerToFail(v) tapToFocusGesture!.requireGestureRecognizerToFail(v)
} }
...@@ -75,7 +86,8 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -75,7 +86,8 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
public var tapToExposeEnabled: Bool { public var tapToExposeEnabled: Bool {
didSet { didSet {
if tapToExposeEnabled { if tapToExposeEnabled {
prepareTapGesture(&tapToExposeGesture, numberOfTapsRequired: 2, selector: "handleTapToExposeGesture:") tapToResetEnabled = true
prepareTapGesture(&tapToExposeGesture, numberOfTapsRequired: 2, numberOfTouchesRequired: 1, selector: "handleTapToExposeGesture:")
if let v: UITapGestureRecognizer = tapToFocusGesture { if let v: UITapGestureRecognizer = tapToFocusGesture {
v.requireGestureRecognizerToFail(tapToExposeGesture!) v.requireGestureRecognizerToFail(tapToExposeGesture!)
} }
...@@ -86,11 +98,31 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -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 :name: init
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
tapToFocusEnabled = true tapToFocusEnabled = true
tapToExposeEnabled = true tapToExposeEnabled = true
tapToResetEnabled = true
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }
...@@ -100,6 +132,7 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -100,6 +132,7 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
public override init(frame: CGRect) { public override init(frame: CGRect) {
tapToFocusEnabled = true tapToFocusEnabled = true
tapToExposeEnabled = true tapToExposeEnabled = true
tapToResetEnabled = true
super.init(frame: frame) super.init(frame: frame)
} }
...@@ -167,6 +200,15 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -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 :name: preparePreviewLayer
*/ */
private func preparePreviewLayer() { private func preparePreviewLayer() {
...@@ -187,10 +229,12 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate { ...@@ -187,10 +229,12 @@ public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
/** /**
:name: prepareTapGesture :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 = UITapGestureRecognizer(target: self, action: selector)
gesture!.delegate = self gesture!.delegate = self
gesture!.numberOfTapsRequired = numberOfTapsRequired gesture!.numberOfTapsRequired = numberOfTapsRequired
gesture!.numberOfTouchesRequired = numberOfTouchesRequired
addGestureRecognizer(gesture!) addGestureRecognizer(gesture!)
} }
......
...@@ -314,6 +314,30 @@ public class CaptureSession : NSObject { ...@@ -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 :name: prepareSession
*/ */
private func prepareSession() { private func prepareSession() {
......
...@@ -164,6 +164,14 @@ public class CaptureView : MaterialView, CaptureSessionDelegate, CapturePreviewV ...@@ -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 :name: prepareView
*/ */
public override func 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