AVCaptureEventInteraction
An object that registers handlers to respond to capture events from system hardware buttons.
Declaration
@MainActor class AVCaptureEventInteractionOverview
The system Camera app allows people to perform capture functions by pressing hardware buttons on their iOS device. UIKit apps can add similar functionality by using this type to register handlers that respond to interactions from device hardware.
The following example shows how to add a handler that captures a photo when a user presses a hardware button on their device.
class CameraViewController: UIViewController {
/// An object that manages the camera functionality.
private let camera = CameraModel()
/// A capture event interaction to handle hardware button presses.
private var eventInteraction: AVCaptureEventInteraction?
override func viewDidLoad() {
super.viewDidLoad()
// Configure the app to take a photo on hardware button press.
configureHardwareInteraction()
}
private func configureHardwareInteraction() {
// Create a new capture event interaction with a handler that captures a photo.
let interaction = AVCaptureEventInteraction { [weak self] event in
// Capture a photo on "press up" of a hardware button.
if event.phase == .ended {
self?.camera.capturePhoto()
}
}
// Add the interaction to the view controller's view.
view.addInteraction(interaction)
eventInteraction = interaction
}
}The event handler queries the capture event to determine its phase, and when the interaction ends, captures a photo.