ios - 后退按钮启动 UIViewPropertyAnimator?

标签 ios swift uiviewanimation uiviewpropertyanimator

我有一个带有摄像头的 ViewController,用于录制视频。顶部有一个旋转的圆圈,表示正在录制视频。这是这样设置的:

class CameraViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    private var animator: UIViewPropertyAnimator?

    @objc func handleTap(_ gesture:UITapGestureRecognizer) {
        if animator == nil {
            createAnimation()
        }
        startRecording()
    }
    private func createAnimation() {
        animator = UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 4, delay: 0, options: [.curveLinear,.allowUserInteraction], animations: {
            UIView.animateKeyframes(withDuration: 4, delay: 0, animations: {
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0 / 3.0) {
                    self.recordingSpinner.transform = .init(rotationAngle: .pi * 2 * 1 / 3)
                }
                UIView.addKeyframe(withRelativeStartTime: 1.0 / 3.0, relativeDuration: 1.0 / 3.0) {
                    self.recordingSpinner.transform = .init(rotationAngle: .pi * 2 * 2 / 3)
                }
                UIView.addKeyframe(withRelativeStartTime: 2.0 / 3.0, relativeDuration: 1.0 / 3.0) {
                    self.recordingSpinner.transform = .identity
                }
            })
        }, completion: { [weak self] _ in
            self?.createAnimation()
        })
    }

    func startRecording() {

        if movieOutput.isRecording == false {

            animator?.startAnimation()
            let connection = movieOutput.connection(with: AVMediaType.video)
            if (connection?.isVideoOrientationSupported)! {
                connection?.videoOrientation = currentVideoOrientation()
            }

            if (connection?.isVideoStabilizationSupported)! {
                connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
            }

            let device = activeInput.device
            if (device.isSmoothAutoFocusSupported) {
                do {
                    try device.lockForConfiguration()
                    device.isSmoothAutoFocusEnabled = false
                    device.unlockForConfiguration()
                } catch {
                    print("Error setting configuration: \(error)")
                }

            }

            let outputFileName = NSUUID().uuidString
            let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
            movieOutput.startRecording(to: URL(fileURLWithPath: outputFilePath), recordingDelegate: self)

        }
        else {
            stopRecording()
        }
    }

    func stopRecording() {
        if movieOutput.isRecording == true {
            animator?.pauseAnimation()
            movieOutput.stopRecording()
        }
    }
    @IBAction func unwindToCamera(sender: UIStoryboardSegue) {
    }
    ...
}

extension CameraViewController: AVCaptureFileOutputRecordingDelegate{
    func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
        if (error != nil) {
            print("Error recording movie: \(error!.localizedDescription)")
        } else {
            self.footageURL = outputFileURL as URL
            //print(self.videoRecorded!)
            self.performSegue(withIdentifier: "TrimFootage_Segue", sender: nil)
        }
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        if segue.identifier == "TrimFootage_Segue" {
            let controller = segue.destination as! TrimFootageViewController
            controller.footageURL = self.footageURL
        }
    }
}

因此,如果它不存在,它会创建一个动画师,然后调用启动动画的 startRecording。然后 stopRecording 停止它。然后,当视频完成录制到输出文件时,它会转到一个新的 View Controller 。当我按回那个 View Controller 时,它使用一个展开的 segue - unwindToCameraWithSender:

当我放松并回到相机前时,视频并未录制,但动画正在播放。是什么导致这个动画重新开始?我怎样才能防止这种情况发生?

最佳答案

我认为只是动画暂停是原因。在 stopRecording() 方法中尝试

动画师?.stopAnimation(true)

代替

动画师?.pauseAnimation()

关于ios - 后退按钮启动 UIViewPropertyAnimator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50845320/

相关文章:

ios - 即使应用程序未运行,如何拥有一个可用的地理围栏?

ios - 更改 iPhone 的语言设置对我的应用程序的影响

swift - 如何将 SwipeCellKit 的自定义滑动功能用于从 .xib 文件创建的自定义单元格?

ios - 在调整 scrollView 的框架大小时,UIScrollView subview 的大小调整不正确

ios - UIView animateWithDuration 跳转到没有动画的最终变换

swift - 为什么 UIView 和 UIImageView 的动画效果不同?

ios - 如何在连续的 repeatForever 中链接 Facebook pop 动画?

ios - 将离线数据保存到 CB Lite 2.0

ios - 动画期间的帧值错误

ios - 满足条件时执行完成处理程序