我正在尝试将捏缩放添加到以编程方式添加的相机预览图层。我将下面的代码作为一个函数,但这就是我所有的,从提示。我没有任何与之相关的其他代码。我找不到更多的信息,他们似乎都专注于静止图像。
override func viewDidLoad() {
super.viewDidLoad()
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:)))
pinchRecognizer.delegate = self
self.cameraPreviewlayer.addGestureRecognizer(pinchRecognizer)
}
我在这一行收到错误
self.cameraPreviewlayer.addGestureRecognizer(pinchRecognizer)
它说cameraPreviewLayer没有添加geture。
这是功能。
@objc func pinch(_ pinch: UIPinchGestureRecognizer) {
let device = videoDeviceInput.device
// Return zoom value between the minimum and maximum zoom values
func minMaxZoom(_ factor: CGFloat) -> CGFloat {
return min(min(max(factor, minimumZoom), maximumZoom), device.activeFormat.videoMaxZoomFactor)
}
func update(scale factor: CGFloat) {
do {
try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.videoZoomFactor = factor
} catch {
print("\(error.localizedDescription)")
}
}
let newScaleFactor = minMaxZoom(pinch.scale * lastZoomFactor)
switch pinch.state {
case .began: fallthrough
case .changed: update(scale: newScaleFactor)
case .ended:
lastZoomFactor = minMaxZoom(newScaleFactor)
update(scale: lastZoomFactor)
default: break
}
}
答案 0 :(得分:2)
我能解决这个问题。
我所要做的只是移动
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:)))
pinchRecognizer.delegate = self
self.cameraPreviewlayer.addGestureRecognizer(pinchRecognizer)
从视图加载到我设置相机预览图层的位置。
cameraPreviewlayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewlayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewlayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewlayer?.frame = self.view.frame
// scanArea.setRegionOfInterestWithProposedRegionOfInterest(regionOfInterest)
self.view.layer.insertSublayer(cameraPreviewlayer!, at: 0)
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:)))
pinchRecognizer.delegate = self
self.view.addGestureRecognizer(pinchRecognizer)