Commit d4b838ad by Daniel Dahan

tap to focus animation working

parent 3398c76a
......@@ -18,13 +18,18 @@
import UIKit
public class BasicCaptureView : MaterialView, CaptureSessionDelegate {
public class BasicCaptureView : MaterialView, CaptureSessionDelegate, CapturePreviewViewDelegate {
/**
:name: previewView
*/
public private(set) lazy var previewView: CapturePreviewView = CapturePreviewView()
/**
:name: focusLayer
*/
public private(set) lazy var focusLayer: MaterialLayer = MaterialLayer()
/**
:name: switchCamerasButton
*/
public var switchCamerasButton: MaterialButton? {
......@@ -66,20 +71,6 @@ public class BasicCaptureView : MaterialView, CaptureSessionDelegate {
/**
:name: init
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/**
:name: init
*/
public override init(frame: CGRect) {
super.init(frame: frame)
}
/**
:name: init
*/
public convenience init() {
self.init(frame: CGRectNull)
}
......@@ -100,6 +91,7 @@ public class BasicCaptureView : MaterialView, CaptureSessionDelegate {
public override func prepareView() {
super.prepareView()
preparePreviewView()
prepareFocusLayer()
reloadView()
}
......@@ -140,6 +132,24 @@ public class BasicCaptureView : MaterialView, CaptureSessionDelegate {
print(error)
}
/**
:name: capturePreviewViewDidTapToFocusAtPoint
*/
public func capturePreviewViewDidTapToFocusAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint) {
MaterialAnimation.animationDisabled {
self.focusLayer.position = point
self.focusLayer.hidden = false
}
MaterialAnimation.animateWithDuration(0.25, animations: {
self.focusLayer.transform = CATransform3DMakeScale(0, 0, 1)
}) {
MaterialAnimation.animationDisabled {
self.focusLayer.hidden = true
self.focusLayer.transform = CATransform3DIdentity
}
}
}
//
// :name: handleSwitchCameras
//
......@@ -153,6 +163,18 @@ public class BasicCaptureView : MaterialView, CaptureSessionDelegate {
private func preparePreviewView() {
previewView.translatesAutoresizingMaskIntoConstraints = false
previewView.captureSession.delegate = self
previewView.delegate = self
previewView.captureSession.startSession()
}
//
// :name: prepareFocusLayer
//
private func prepareFocusLayer() {
focusLayer.hidden = true
focusLayer.backgroundColor = MaterialColor.white.colorWithAlphaComponent(0.25).CGColor
focusLayer.bounds = CGRectMake(0, 0, 150, 150)
focusLayer.cornerRadius = 75
previewView.layer.addSublayer(focusLayer)
}
}
\ No newline at end of file
......@@ -19,7 +19,20 @@
import UIKit
import AVFoundation
public class CapturePreviewView : MaterialView {
@objc(CapturePreviewViewDelegate)
public protocol CapturePreviewViewDelegate : MaterialDelegate {
/**
:name: capturePreviewViewDidTapToFocusAtPoint
*/
optional func capturePreviewViewDidTapToFocusAtPoint(capturePreviewView: CapturePreviewView, point: CGPoint)
}
public class CapturePreviewView : MaterialView, UIGestureRecognizerDelegate {
//
// :name: tapToFocusGesture
//
private var tapToFocusGesture: UITapGestureRecognizer?
/**
:name: previewLayer
*/
......@@ -31,6 +44,52 @@ public class CapturePreviewView : MaterialView {
public private(set) lazy var captureSession: CaptureSession = CaptureSession()
/**
:name: tapToFocusEnabled
*/
public var tapToFocusEnabled: Bool {
didSet {
if tapToFocusEnabled {
prepareTapGesture(&tapToFocusGesture, selector: "handleTapToFocusGesture:")
} else {
removeTapGesture(&tapToFocusGesture)
}
}
}
/**
:name: tapToExposeEnabled
*/
public var tapToExposeEnabled: Bool {
didSet {
}
}
/**
:name: init
*/
public required init?(coder aDecoder: NSCoder) {
tapToFocusEnabled = true
tapToExposeEnabled = true
super.init(coder: aDecoder)
}
/**
:name: init
*/
public override init(frame: CGRect) {
tapToFocusEnabled = true
tapToExposeEnabled = true
super.init(frame: frame)
}
/**
:name: init
*/
public convenience init() {
self.init(frame: CGRectNull)
}
/**
:name: layoutSublayersOfLayer
*/
public override func layoutSublayersOfLayer(layer: CALayer) {
......@@ -46,6 +105,8 @@ public class CapturePreviewView : MaterialView {
public override func prepareView() {
super.prepareView()
preparePreviewLayer()
tapToFocusEnabled = true
tapToExposeEnabled = true
}
/**
......@@ -63,6 +124,17 @@ public class CapturePreviewView : MaterialView {
}
//
// :name: handleTapToFocusGesture
//
internal func handleTapToFocusGesture(recognizer: UITapGestureRecognizer) {
if tapToFocusEnabled && captureSession.cameraSupportsTapToFocus {
let point: CGPoint = recognizer.locationInView(self)
captureSession.focusAtPoint(captureDevicePointOfInterestForPoint(point))
(delegate as? CapturePreviewViewDelegate)?.capturePreviewViewDidTapToFocusAtPoint?(self, point: point)
}
}
//
// :name: preparePreviewLayer
//
private func preparePreviewLayer() {
......@@ -73,9 +145,28 @@ public class CapturePreviewView : MaterialView {
//
// :name: layoutPreviewLayer
//
internal func layoutPreviewLayer() {
private func layoutPreviewLayer() {
previewLayer.frame = visualLayer.bounds
previewLayer.position = CGPointMake(width / 2, height / 2)
previewLayer.cornerRadius = visualLayer.cornerRadius
}
//
// :name: prepareTapGesture
//
private func prepareTapGesture(inout gesture: UITapGestureRecognizer?, selector: Selector) {
gesture = UITapGestureRecognizer(target: self, action: selector)
gesture!.delegate = self
addGestureRecognizer(gesture!)
}
//
// :name: removeTapToFocusGesture
//
private func removeTapGesture(inout gesture: UITapGestureRecognizer?) {
if let v: UIGestureRecognizer = gesture {
removeGestureRecognizer(v)
gesture = nil
}
}
}
\ No newline at end of file
......@@ -124,10 +124,10 @@ public class CaptureSession : NSObject {
}
/**
:name: cameraSupportsTapToFocus
:name: caneraSupportsTapToFocus
*/
public var cameraSupportsTapToFocus: Bool {
return true
return true == activeCamera?.focusPointOfInterestSupported
}
/**
......@@ -232,6 +232,29 @@ public class CaptureSession : NSObject {
return activeVideoInput!.device.isExposureModeSupported(exposureMode)
}
/**
:name: focusAtPoint
*/
public func focusAtPoint(point: CGPoint) {
var error: NSError?
let device: AVCaptureDevice = activeCamera!
if device.focusPointOfInterestSupported && isFocusModeSupported(.AutoFocus) {
do {
try device.lockForConfiguration()
device.focusPointOfInterest = point
device.focusMode = .AutoFocus
device.unlockForConfiguration()
} catch let e as NSError {
error = e
}
} else {
error = NSError(domain: "[MaterialKit Error: Unsupported focusAtPoint.]", code: 0, userInfo: nil)
}
if let e: NSError = error {
delegate?.captureSessionFailedWithError?(self, error: e)
}
}
//
// :name: prepareSession
//
......
......@@ -394,10 +394,10 @@ public class MaterialButton : UIButton {
if nil != pulseColor && 0 < pulseColorOpacity {
MaterialAnimation.animationDisabled {
self.pulseLayer.hidden = false
self.pulseLayer.bounds = CGRectMake(0, 0, v, v)
self.pulseLayer.position = point
self.pulseLayer.cornerRadius = r / d
self.pulseLayer.hidden = false
}
pulseLayer.addAnimation(MaterialAnimation.scale(pulseFill ? 3 * d : 1.5 * d, duration: t), forKey: nil)
}
......
......@@ -85,10 +85,10 @@ public class MaterialPulseView : MaterialView {
if nil != pulseColor && 0 < pulseColorOpacity {
MaterialAnimation.animationDisabled {
self.pulseLayer.hidden = false
self.pulseLayer.bounds = CGRectMake(0, 0, v, v)
self.pulseLayer.position = point
self.pulseLayer.cornerRadius = r / d
self.pulseLayer.hidden = false
}
pulseLayer.addAnimation(MaterialAnimation.scale(pulseFill ? 3 * d : d, duration: t), forKey: nil)
}
......
......@@ -87,17 +87,17 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
//
// :name: sidePanGesture
//
internal var sidePanGesture: UIPanGestureRecognizer?
private var sidePanGesture: UIPanGestureRecognizer?
//
// :name: sideTapGesture
//
internal var sideTapGesture: UITapGestureRecognizer?
private var sideTapGesture: UITapGestureRecognizer?
//
// :name: isViewBasedAppearance
//
internal var isViewBasedAppearance: Bool {
private var isViewBasedAppearance: Bool {
return 0 == NSBundle.mainBundle().objectForInfoDictionaryKey("UIViewControllerBasedStatusBarAppearance") as? Int
}
......@@ -334,22 +334,6 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
return false
}
/**
:name: prepareGestures
*/
private func prepareGestures(inout pan: UIPanGestureRecognizer?, panSelector: Selector, inout tap: UITapGestureRecognizer?, tapSelector: Selector) {
if nil == pan {
pan = UIPanGestureRecognizer(target: self, action: panSelector)
pan!.delegate = self
view.addGestureRecognizer(pan!)
}
if nil == tap {
tap = UITapGestureRecognizer(target: self, action: tapSelector)
tap!.delegate = self
view.addGestureRecognizer(tap!)
}
}
//
// :name: prepareView
//
......@@ -430,15 +414,31 @@ public class SideNavigationViewController: UIViewController, UIGestureRecognizer
}
//
// :name: prepareGestures
//
private func prepareGestures(inout pan: UIPanGestureRecognizer?, panSelector: Selector, inout tap: UITapGestureRecognizer?, tapSelector: Selector) {
if nil == pan {
pan = UIPanGestureRecognizer(target: self, action: panSelector)
pan!.delegate = self
view.addGestureRecognizer(pan!)
}
if nil == tap {
tap = UITapGestureRecognizer(target: self, action: tapSelector)
tap!.delegate = self
view.addGestureRecognizer(tap!)
}
}
//
// :name: removeGestures
//
private func removeGestures(inout pan: UIPanGestureRecognizer?, inout tap: UITapGestureRecognizer?) {
if let g = pan {
view.removeGestureRecognizer(g)
if let v: UIPanGestureRecognizer = pan {
view.removeGestureRecognizer(v)
pan = nil
}
if let g = tap {
view.removeGestureRecognizer(g)
if let v: UITapGestureRecognizer = tap {
view.removeGestureRecognizer(v)
tap = nil
}
}
......
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