当前,我有一个vie控制器,该控制器以模态形式显示一个包含相机的视图控制器。但是,每当我过渡时,预览层都会有一个动画,因此它会从左上角起逐渐增大以填充屏幕的其余部分。我尝试禁用CALayer隐式动画,但没有成功。这是视图出现时的代码。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previewLayer?.frame = self.view.frame
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
capturedImageView.center = self.view.center
captureSession = AVCaptureSession()
if usingFrontCamera == true {
captureSession?.sessionPreset = AVCaptureSession.Preset.hd1920x1080
}
else {
captureSession?.sessionPreset = AVCaptureSession.Preset.hd1280x720
}
captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
do {
let input = try AVCaptureDeviceInput(device: captureDevice!)
if (captureSession?.canAddInput(input) != nil) {
captureSession?.addInput(input)
stillImageOutput = AVCapturePhotoOutput()
captureSession?.addOutput(stillImageOutput!)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
self.view.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
} catch {
}
}
有没有要删除这个不断增长的动画?这是问题的图像:
答案 0 :(得分:4)
您正在分两个阶段进行操作。在viewWillAppear
中,您添加的预览层根本不提供任何尺寸,因此它是零起点的零尺寸层:
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
self.view.layer.addSublayer(previewLayer!)
然后,在viewDidAppear
中,通过为预览层提供实际的帧来对其进行扩展:
previewLayer?.frame = self.view.frame
这两个阶段按此顺序进行,我们可以看到由于预览层框架变化而引起的跳跃。
如果您不想看到跳跃,请不要这样做。在您首先为其提供 actual 框架之前,不要添加预览层。
答案 1 :(得分:3)
更改图层框架时,会出现一个隐式动画。您可以使用CATransaction
禁用动画。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
CATransaction.begin()
CATransaction.setDisableActions(true)
previewLayer?.frame = self.view.frame
CATransaction.commit()
}