Last active
September 7, 2023 17:19
-
-
Save stucarney/2fe0710e772b6ede7d078ce56eef250c to your computer and use it in GitHub Desktop.
AVCaptureDevice - Tap-to-Focus feature (iOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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