diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d5991..0059b42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Version 1.5.0 +- Added support for front-facing camera tap to adjust exposure +- Updated demo application with tap animation + ## Version 1.4.0 - Added support for background audio during video capture - Added allowBackgroundAudio property diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/Contents.json b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/Contents.json new file mode 100644 index 0000000..5f4d857 --- /dev/null +++ b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/Contents.json @@ -0,0 +1,22 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "focus@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "filename" : "focus@3x.png", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/focus@2x.png b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/focus@2x.png new file mode 100644 index 0000000..db68a8d Binary files /dev/null and b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/focus@2x.png differ diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/focus@3x.png b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/focus@3x.png new file mode 100644 index 0000000..583309a Binary files /dev/null and b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/focus.imageset/focus@3x.png differ diff --git a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift index 2a9c58c..580d359 100644 --- a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift +++ b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift @@ -63,7 +63,22 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate { } func SwiftyCamDidFocusAtPoint(focusPoint: CGPoint) { - print(focusPoint) + let focusView = UIImageView(image: #imageLiteral(resourceName: "focus")) + focusView.center = focusPoint + focusView.alpha = 0.0 + view.addSubview(focusView) + + UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: { + focusView.alpha = 1.0 + focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25) + }, completion: { (success) in + UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: { + focusView.alpha = 0.0 + focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6) + }, completion: { (success) in + focusView.removeFromSuperview() + }) + }) } func SwiftyCamDidChangeZoomLevel(zoomLevel: CGFloat) { diff --git a/README.md b/README.md index ba353a4..ecd9ed7 100644 --- a/README.md +++ b/README.md @@ -255,7 +255,7 @@ By default, **pinchToZoom** is enabled. ## Camera Focus -SwiftyCam, by default, support tap to focus on the video preview. SwiftyCam will set the focus and exposure levels of the session to the tapped point. Autofocus and autoexposure will be resumed once SwiftyCam detects significant movement from the tapped point. To disable this feature, change the `tapToFocus` property: +SwiftyCam, by default, support tap to focus on the video preview. SwiftyCam will set the focus and exposure levels of the session to the tapped point. While tap to set exposure is supported on both cameras, tap to focus is only supported on rear facing cameras. Autofocus and autoexposure will be resumed once SwiftyCam detects significant movement from the tapped point. To disable this feature, change the `tapToFocus` property: ```swift tapToFocus = false diff --git a/Source/SwiftyCamViewController.swift b/Source/SwiftyCamViewController.swift index 2302983..78bf72b 100644 --- a/Source/SwiftyCamViewController.swift +++ b/Source/SwiftyCamViewController.swift @@ -109,7 +109,7 @@ open class SwiftyCamViewController: UIViewController { public var pinchToZoom = true - /// Sets whether Tap to Focus is enabled for the capture session + /// Sets whether Tap to Focus and Tap to Adjust Exposure is enabled for the capture session public var tapToFocus = true @@ -127,6 +127,10 @@ open class SwiftyCamViewController: UIViewController { public var allowBackgroundAudio = true + /// Sets whether a double tap to switch cameras is supported + + public var doubleTapCameraSwitch = true + // MARK: Public Get-only Variable Declarations @@ -213,7 +217,7 @@ open class SwiftyCamViewController: UIViewController { super.viewDidLoad() previewLayer = PreviewView(frame: self.view.frame) - // Add Pinch Gesture Recognizer for pinch to zoom + // Add Gesture Recognizers addGestureRecognizersTo(view: previewLayer) @@ -477,43 +481,6 @@ open class SwiftyCamViewController: UIViewController { // If flash is enabled, disable it as the torch is needed for front facing camera disableFlash() } - - - /// Override Touches Began for Tap to Focus - - override open func touchesBegan(_ touches: Set, with event: UIEvent?) { - guard tapToFocus == true && currentCamera == .rear else { - // Ignore taps - return - } - - let screenSize = previewLayer!.bounds.size - if let touchPoint = touches.first { - let x = touchPoint.location(in: previewLayer!).y / screenSize.height - let y = 1.0 - touchPoint.location(in: previewLayer!).x / screenSize.width - let focusPoint = CGPoint(x: x, y: y) - - if let device = videoDevice { - do { - try device.lockForConfiguration() - - device.focusPointOfInterest = focusPoint - device.focusMode = .autoFocus - device.exposurePointOfInterest = focusPoint - device.exposureMode = AVCaptureExposureMode.continuousAutoExposure - device.unlockForConfiguration() - //Call delegate function and pass in the location of the touch - - DispatchQueue.main.async { - self.cameraDelegate?.SwiftyCamDidFocusAtPoint(focusPoint: touchPoint.location(in: self.previewLayer)) - } - } - catch { - // just ignore - } - } - } - } // MARK: Private Functions @@ -741,8 +708,54 @@ open class SwiftyCamViewController: UIViewController { } } + /// Handle single tap gesture + + @objc fileprivate func singleTapGesture(tap: UITapGestureRecognizer) { + guard tapToFocus == true else { + // Ignore taps + return + } + + let screenSize = previewLayer!.bounds.size + let tapPoint = tap.location(in: previewLayer!) + let x = tapPoint.y / screenSize.height + let y = 1.0 - tapPoint.x / screenSize.width + let focusPoint = CGPoint(x: x, y: y) + + if let device = videoDevice { + do { + try device.lockForConfiguration() + + if device.isFocusPointOfInterestSupported == true { + device.focusPointOfInterest = focusPoint + device.focusMode = .autoFocus + } + device.exposurePointOfInterest = focusPoint + device.exposureMode = AVCaptureExposureMode.continuousAutoExposure + device.unlockForConfiguration() + //Call delegate function and pass in the location of the touch + + DispatchQueue.main.async { + self.cameraDelegate?.SwiftyCamDidFocusAtPoint(focusPoint: tapPoint) + } + } + catch { + // just ignore + } + } + } + + /// Handle double tap gesture + + @objc fileprivate func doubleTapGesture(tap: UITapGestureRecognizer) { + guard doubleTapCameraSwitch == true else { + return + } + switchCamera() + } + /** - Add pinch gesture recognizer to currentView + Add pinch gesture recognizer and double tap gesture recognizer to currentView - Parameter view: View to add gesture recognzier @@ -752,6 +765,16 @@ open class SwiftyCamViewController: UIViewController { let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(zoomGesture(pinch:))) pinchGesture.delegate = self view.addGestureRecognizer(pinchGesture) + + let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(singleTapGesture(tap:))) + singleTapGesture.numberOfTapsRequired = 1 + singleTapGesture.delegate = self + view.addGestureRecognizer(singleTapGesture) + + let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleTapGesture(tap:))) + doubleTapGesture.numberOfTapsRequired = 2 + doubleTapGesture.delegate = self + view.addGestureRecognizer(doubleTapGesture) } /// Handle Denied App Privacy Settings