Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit a113fde

Browse files
Added pick to zoom feature from #214
1 parent 5609212 commit a113fde

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ALCameraViewController/ViewController/CameraViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ open class CameraViewController: UIViewController {
266266
setupActions()
267267
checkPermissions()
268268
cameraView.configureFocus()
269+
cameraView.configureZoom()
269270
}
270271

271272
/**

ALCameraViewController/Views/CameraView.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public class CameraView: UIView {
100100
line.alpha = 0
101101
}
102102
}
103+
104+
public func configureZoom() {
105+
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinch(gesture:)))
106+
addGestureRecognizer(pinchGesture)
107+
}
103108

104109
internal func focus(gesture: UITapGestureRecognizer) {
105110
let point = gesture.location(in: self)
@@ -134,6 +139,37 @@ public class CameraView: UIView {
134139
}
135140
})
136141
}
142+
143+
internal func pinch(gesture: UIPinchGestureRecognizer) {
144+
guard let device = device else { return }
145+
146+
// Return zoom value between the minimum and maximum zoom values
147+
func minMaxZoom(_ factor: CGFloat) -> CGFloat {
148+
return min(max(factor, 1.0), device.activeFormat.videoMaxZoomFactor)
149+
}
150+
151+
func update(scale factor: CGFloat) {
152+
do {
153+
try device.lockForConfiguration()
154+
defer { device.unlockForConfiguration() }
155+
device.videoZoomFactor = factor
156+
} catch {
157+
print("\(error.localizedDescription)")
158+
}
159+
}
160+
161+
let velocity = gesture.velocity
162+
let velocityFactor: CGFloat = 8.0
163+
let desiredZoomFactor = device.videoZoomFactor + atan2(velocity, velocityFactor)
164+
165+
let newScaleFactor = minMaxZoom(desiredZoomFactor)
166+
switch gesture.state {
167+
case .began, .changed:
168+
update(scale: newScaleFactor)
169+
case _:
170+
break
171+
}
172+
}
137173

138174
private func createPreview() {
139175

0 commit comments

Comments
 (0)