Skip to content

Instantly share code, notes, and snippets.

@stucarney
Last active September 7, 2023 17:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stucarney/2fe0710e772b6ede7d078ce56eef250c to your computer and use it in GitHub Desktop.
Save stucarney/2fe0710e772b6ede7d078ce56eef250c to your computer and use it in GitHub Desktop.
AVCaptureDevice - Tap-to-Focus feature (iOS)
@IBAction func handleTapToFocus(sender: UITapGestureRecognizer) {
if let device = captureDevice {
let focusPoint = sender.locationInView(previewView)
let focusScaledPointX = focusPoint.x / previewView.frame.size.width
let focusScaledPointY = focusPoint.y / previewView.frame.size.height
if device.isFocusModeSupported(.AutoFocus) && device.focusPointOfInterestSupported {
do {
try device.lockForConfiguration()
} catch {
print("ERROR: Could not lock camera device for configuration")
return
}
device.focusMode = .AutoFocus
device.focusPointOfInterest = CGPointMake(focusScaledPointX, focusScaledPointY)
device.unlockForConfiguration()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment