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

Commit

Permalink
Added tap to focus support when cropping is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLittlejohn committed Feb 17, 2016
1 parent 1066e61 commit e3df23f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Binary file modified ALCameraViewController.sketch
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public class ALCameraViewController: UIViewController {

if allowCropping {
layoutCropView()
} else {
cameraView.configureFocus()
}

cameraButton.enabled = true
Expand Down
68 changes: 68 additions & 0 deletions ALCameraViewController/Views/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class CameraView: UIView {

let cameraQueue = dispatch_queue_create("com.zero.ALCameraViewController.Queue", DISPATCH_QUEUE_SERIAL);

let focusView = CropOverlay(frame: CGRect(x: 0, y: 0, width: 80, height: 80))

public var currentPosition = AVCaptureDevicePosition.Back

public func startSession() {
Expand Down Expand Up @@ -48,6 +50,71 @@ public class CameraView: UIView {
}
}

public func configureFocus() {

if let gestureRecognizers = gestureRecognizers {
for gesture in gestureRecognizers {
removeGestureRecognizer(gesture)
}
}

let tapGesture = UITapGestureRecognizer(target: self, action: "focus:")
addGestureRecognizer(tapGesture)
userInteractionEnabled = true
addSubview(focusView)

focusView.hidden = true

let lines = focusView.horizontalLines + focusView.verticalLines + focusView.outerLines

lines.forEach { line in
line.alpha = 0
}

}

internal func focus(gesture: UITapGestureRecognizer) {
let point = gesture.locationInView(self)
guard let device = device else { return }
do { try device.lockForConfiguration() } catch {
return
}

if device.isFocusModeSupported(.Locked) {

let focusPoint = CGPoint(x: point.x / frame.width, y: point.y / frame.height)

device.focusPointOfInterest = focusPoint
device.unlockForConfiguration()

focusView.hidden = false
focusView.center = point
focusView.alpha = 0
focusView.transform = CGAffineTransformMakeScale(1.2, 1.2)

bringSubviewToFront(focusView)

UIView.animateKeyframesWithDuration(1.5, delay: 0, options: UIViewKeyframeAnimationOptions(), animations: {

UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.15, animations: { () -> Void in
self.focusView.alpha = 1
self.focusView.transform = CGAffineTransformIdentity
})

UIView.addKeyframeWithRelativeStartTime(0.80, relativeDuration: 0.20, animations: { () -> Void in
self.focusView.alpha = 0
self.focusView.transform = CGAffineTransformMakeScale(0.8, 0.8)
})


}, completion: { finished in
if finished {
self.focusView.hidden = true
}
})
}
}

private func createSession() {
session = AVCaptureSession()
session.sessionPreset = AVCaptureSessionPresetHigh
Expand Down Expand Up @@ -89,6 +156,7 @@ public class CameraView: UIView {
preview.videoGravity = AVLayerVideoGravityResizeAspectFill
preview.frame = bounds


layer.addSublayer(preview)
}

Expand Down
5 changes: 5 additions & 0 deletions ALCameraViewController/Views/CropOverlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ internal class CropOverlay: UIView {
super.init(frame: CGRectZero)
createLines()
}

internal override init(frame: CGRect) {
super.init(frame: frame)
createLines()
}

internal required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Expand Down

0 comments on commit e3df23f

Please sign in to comment.