Added swipe to zoom

This commit is contained in:
Andrew Walz 2017-04-26 11:35:03 -06:00
parent e884300f85
commit 042428a4b7
4 changed files with 82 additions and 8 deletions

View file

@ -1,5 +1,11 @@
# CHANGELOG
## Version 2.3.0
- Added support to vertically swipt to zoom
- Added ```swipeToZoom``` property
- Added support for inverting zoom directions for swipeToZoom
- Added ```swipeToZoomInverted``` property
## Version 2.2.2
- Fixed issue with landscape orientation
- Fixed issue with SwiftyCam crashing by multiple double-tap camera switching

View file

@ -27,7 +27,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
cameraDelegate = self
maximumVideoDuration = 10.0
shouldUseDeviceOrientation = true
addButtons()
}

View file

@ -274,13 +274,25 @@ A value of **0.0** will allow for unlimited video recording via the SwiftyCamBut
## Camera Zoom
SwiftyCam supports digital zoom of the camera session via pinch gestures. The gestures work similar to the default iOS app and will zoom to the maximum supported zoom level. Camera zoom is only supported on the rear facing camera. AVFoundation does not currently support front facing camera zoom. To disable this feature, change the `pinchToZoom` property:
SwiftyCam supports digital zoom of the camera session via pinch and pan gestures. The gestures work similar to the default iOS app and will zoom to the maximum supported zoom level. Camera zoom is only supported on the rear facing camera. AVFoundation does not currently support front facing camera zoom. To disable this feature, change the `pinchToZoom` property:
```swift
pinchToZoom = false
```
By default, **pinchToZoom** is enabled.
By default, ```pinchToZoom``` is enabled.
SwiftyCam also supports the ability to zoom the rear facing camera with vertical pan gestures. To disable this feature, change the `swipeToZoom` property:
```swift
swipeToZoom = false
```
By default, ```swipeToZoom``` is enabled. The default gestures zoom in the capture session with a downward swipe, and zoom out with an upward swipe. This can be reversed by changing the ```swipeToZoomInverted``` property:
```swift
swipeToZoomInverted = true
```
You can also restrict the amount that the rear facing camera can zoom. To do this, use the `maxZoomScale` property:
@ -288,7 +300,7 @@ You can also restrict the amount that the rear facing camera can zoom. To do thi
maxZoomScale = 2.0
```
By default, `maxZoomScale` is set to **4.0**.
By default, `maxZoomScale` is set to **infinite**. The actual maximum zoom level is determined by the device's [videoMaxZoomFactor](https://developer.apple.com/reference/avfoundation/avcapturedeviceformat/1624635-videomaxzoomfactor).
## Camera Focus

View file

@ -109,9 +109,9 @@ open class SwiftyCamViewController: UIViewController {
public var pinchToZoom = true
/// Sets the maximum zoom scale allowed during Pinch gesture
/// Sets the maximum zoom scale allowed during gestures gesture
public var maxZoomScale = CGFloat(4.0)
public var maxZoomScale = CGFloat.greatestFiniteMagnitude
/// Sets whether Tap to Focus and Tap to Adjust Exposure is enabled for the capture session
@ -130,6 +130,14 @@ open class SwiftyCamViewController: UIViewController {
/// Sets whether a double tap to switch cameras is supported
public var doubleTapCameraSwitch = true
/// Sets whether swipe vertically to zoom is supported
public var swipeToZoom = true
/// Sets whether swipe vertically gestures should be inverted
public var swipeToZoomInverted = false
/// Set default launch camera
@ -209,6 +217,10 @@ open class SwiftyCamViewController: UIViewController {
/// UIView for front facing flash
fileprivate var flashView : UIView?
/// Pan Translation
fileprivate var previousPanTranslation : CGFloat = 0.0
/// Last changed orientation
@ -216,7 +228,6 @@ open class SwiftyCamViewController: UIViewController {
/// Disable view autorotation for forced portrait recorindg
override open var shouldAutorotate: Bool {
return false
}
@ -227,7 +238,7 @@ open class SwiftyCamViewController: UIViewController {
override open func viewDidLoad() {
super.viewDidLoad()
previewLayer = PreviewView(frame: self.view.frame)
previewLayer = PreviewView(frame: view.bounds)
// Add Gesture Recognizers
@ -1059,6 +1070,48 @@ extension SwiftyCamViewController {
}
switchCamera()
}
@objc private func panGesture(pan: UIPanGestureRecognizer) {
guard swipeToZoom == true && self.currentCamera == .rear else {
//ignore pan
return
}
let currentTranslation = pan.translation(in: view).y
let translationDifference = currentTranslation - previousPanTranslation
do {
let captureDevice = AVCaptureDevice.devices().first as? AVCaptureDevice
try captureDevice?.lockForConfiguration()
let currentZoom = captureDevice?.videoZoomFactor ?? 0.0
if swipeToZoomInverted == true {
zoomScale = min(maxZoomScale, max(1.0, min(currentZoom - (translationDifference / 75), captureDevice!.activeFormat.videoMaxZoomFactor)))
} else {
zoomScale = min(maxZoomScale, max(1.0, min(currentZoom + (translationDifference / 75), captureDevice!.activeFormat.videoMaxZoomFactor)))
}
captureDevice?.videoZoomFactor = zoomScale
// Call Delegate function with current zoom scale
DispatchQueue.main.async {
self.cameraDelegate?.swiftyCam(self, didChangeZoomLevel: self.zoomScale)
}
captureDevice?.unlockForConfiguration()
} catch {
print("[SwiftyCam]: Error locking configuration")
}
if pan.state == .ended || pan.state == .failed || pan.state == .cancelled {
previousPanTranslation = 0.0
} else {
previousPanTranslation = currentTranslation
}
}
/**
Add pinch gesture recognizer and double tap gesture recognizer to currentView
@ -1081,6 +1134,10 @@ extension SwiftyCamViewController {
doubleTapGesture.numberOfTapsRequired = 2
doubleTapGesture.delegate = self
view.addGestureRecognizer(doubleTapGesture)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGesture(pan:)))
panGesture.delegate = self
view.addGestureRecognizer(panGesture)
}
}