3

目前,我有一个 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 {

    }
}

有没有办法删除这个不断增长的动画?这是问题的 GIF:

GIF动画

4

2 回答 2

4

当您更改图层框架时,会有一个隐式动画。您可以使用CATransaction来禁用动画。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    CATransaction.begin()
    CATransaction.setDisableActions(true)
    previewLayer?.frame = self.view.frame
    CATransaction.commit()
}
于 2019-04-12T03:08:29.853 回答
4

你分两个阶段做事。在viewWillAppear中,您添加了预览层而不给它任何大小,因此它是零原点的零大小层:

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
self.view.layer.addSublayer(previewLayer!)

然后,在 中viewDidAppear,您通过给它一个实际的框架来扩展预览层:

previewLayer?.frame = self.view.frame

这两个阶段是按顺序发生的,我们可以看到预览层的帧变化引起的跳跃。

如果您不想看到跳跃,请不要这样做。在您可以先给它实际帧之前,不要添加预览层。

于 2019-04-12T02:43:17.060 回答