Skip to content

Commit

Permalink
Merge pull request hyperoslo#331 from weslindsay/add-zoom-gesture
Browse files Browse the repository at this point in the history
Add pinch to zoom
  • Loading branch information
zenangst authored Aug 24, 2017
2 parents df3f728 + 2aafc31 commit 8f7952b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Source/CameraView/CameraMan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ class CameraMan {
}
}

func zoom(_ zoomFactor: CGFloat) {
guard let device = currentInput?.device, device.position == .back else { return }

queue.async {
self.lock {
device.videoZoomFactor = zoomFactor
}
}
}

// MARK: - Lock

func lock(_ block: () -> Void) {
Expand Down
42 changes: 42 additions & 0 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
return gesture
}()

lazy var pinchGestureRecognizer: UIPinchGestureRecognizer = { [unowned self] in
let gesture = UIPinchGestureRecognizer()
gesture.addTarget(self, action: #selector(pinchGestureRecognizerHandler(_:)))

return gesture
}()

let cameraMan = CameraMan()

var previewLayer: AVCaptureVideoPreviewLayer?
Expand All @@ -89,6 +96,12 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
var locationManager: LocationManager?
var startOnFrontCamera: Bool = false

private let minimumZoomFactor: CGFloat = 1.0
private let maximumZoomFactor: CGFloat = 3.0

private var currentZoomFactor: CGFloat = 1.0
private var previousZoomFactor: CGFloat = 1.0

public init(configuration: Configuration? = nil) {
if let configuration = configuration {
self.configuration = configuration
Expand Down Expand Up @@ -118,6 +131,10 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate

view.addGestureRecognizer(tapGestureRecognizer)

if configuration.allowPinchToZoom {
view.addGestureRecognizer(pinchGestureRecognizer)
}

cameraMan.delegate = self
cameraMan.setup(self.startOnFrontCamera)
}
Expand Down Expand Up @@ -244,6 +261,16 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
})
}

func zoomTo(_ zoomFactor: CGFloat) {
guard let device = cameraMan.currentInput?.device else { return }

let maximumDeviceZoomFactor = device.activeFormat.videoMaxZoomFactor
let newZoomFactor = previousZoomFactor * zoomFactor
currentZoomFactor = min(maximumZoomFactor, max(minimumZoomFactor, min(newZoomFactor, maximumDeviceZoomFactor)))

cameraMan.zoom(currentZoomFactor)
}

// MARK: - Tap

func tapGestureRecognizerHandler(_ gesture: UITapGestureRecognizer) {
Expand All @@ -254,6 +281,21 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
focusTo(touch)
}

// MARK: - Pinch

func pinchGestureRecognizerHandler(_ gesture: UIPinchGestureRecognizer) {
switch gesture.state {
case .began:
fallthrough
case .changed:
zoomTo(gesture.scale)
case .ended:
zoomTo(gesture.scale)
previousZoomFactor = currentZoomFactor
default: break
}
}

// MARK: - Private helpers

func showNoCamera(_ show: Bool) {
Expand Down
1 change: 1 addition & 0 deletions Source/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public struct Configuration {
public var showsImageCountLabel = true
public var flashButtonAlwaysHidden = false
public var managesAudioSession = true
public var allowPinchToZoom = true

// MARK: Images
public var indicatorView: UIView = {
Expand Down

0 comments on commit 8f7952b

Please sign in to comment.