diff --git a/Sources/Gallery/Camera/CameraController.swift b/Sources/Gallery/Camera/CameraController.swift index a89bda95..28bee76c 100644 --- a/Sources/Gallery/Camera/CameraController.swift +++ b/Sources/Gallery/Camera/CameraController.swift @@ -65,6 +65,9 @@ class CameraController: UIViewController { cameraView.stackView.addTarget(self, action: #selector(stackViewTouched(_:)), for: .touchUpInside) cameraView.shutterButton.addTarget(self, action: #selector(shutterButtonTouched(_:)), for: .touchUpInside) cameraView.doneButton.addTarget(self, action: #selector(doneButtonTouched(_:)), for: .touchUpInside) + + let pinchGesture = UIPinchGestureRecognizer(target:self, action: #selector(pinchToZoom(_:))) + view.addGestureRecognizer(pinchGesture) } func setupLocation() { @@ -75,6 +78,12 @@ class CameraController: UIViewController { // MARK: - Action + @objc func pinchToZoom(_ pinch: UIPinchGestureRecognizer) { + if pinch.state == .changed { + cameraMan.cameraZoomWithPinchVelocity(velocity: pinch.velocity) + } + } + @objc func closeButtonTouched(_ button: UIButton) { EventHub.shared.close?() } diff --git a/Sources/Gallery/Camera/CameraMan.swift b/Sources/Gallery/Camera/CameraMan.swift index 46af5e1b..b7ace983 100644 --- a/Sources/Gallery/Camera/CameraMan.swift +++ b/Sources/Gallery/Camera/CameraMan.swift @@ -3,7 +3,7 @@ import AVFoundation import PhotosUI import Photos -protocol CameraManDelegate: class { +protocol CameraManDelegate: AnyObject { func cameraManNotAvailable(_ cameraMan: CameraMan) func cameraManDidStart(_ cameraMan: CameraMan) func cameraMan(_ cameraMan: CameraMan, didChangeInput input: AVCaptureDeviceInput) @@ -199,6 +199,28 @@ class CameraMan { } } } + + func cameraZoomWithPinchVelocity(velocity: CGFloat) { + var pinchVelocityDividerFactor = 40.0 + if (velocity < 0) { + pinchVelocityDividerFactor = 6.0 //zoom in + } + + if let videoInput = currentInput { + if(videoInput.device.position == .back) { + do { + try videoInput.device.lockForConfiguration() + let desiredZoomFactor = videoInput.device.videoZoomFactor + CGFloat(atan2f(Float(velocity), Float(pinchVelocityDividerFactor))); + // Check if desiredZoomFactor fits required range from 1.0 to activeFormat.videoMaxZoomFactor + let maxFactor = min(10, videoInput.device.activeFormat.videoMaxZoomFactor) + videoInput.device.videoZoomFactor = max(1.0, min(desiredZoomFactor, maxFactor)) + videoInput.device.unlockForConfiguration() + } catch { + print("cameraZoomWithPinchVelocity error: \(error)") + } + } + } + } // MARK: - Lock