Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add camera zoom function #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/Gallery/Camera/CameraController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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?()
}
Expand Down
24 changes: 23 additions & 1 deletion Sources/Gallery/Camera/CameraMan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down