首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后退按钮启动UIViewPropertyAnimator?

后退按钮启动UIViewPropertyAnimator?
EN

Stack Overflow用户
提问于 2018-06-13 20:00:30
回答 2查看 166关注 0票数 0

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

代码语言:javascript
复制
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阻止它。然后,当视频完成对输出文件的记录时,它会切换到newView控制器。当我按下该视图控制器时,它使用的是展开- unwindToCameraWithSender:

当我放松并回到相机前时,视频没有录制,但是动画正在播放。是什么让这个动画重新开始的?我怎么才能阻止这一切?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-13 21:41:47

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

animator?.stopAnimation(true)

而不是

animator?.pauseAnimation()

票数 1
EN

Stack Overflow用户

发布于 2018-06-13 22:32:23

因此,为了解决这个问题,我做了一件事,但没有修复它,就是像这样使用UIDynamicAnimator:

代码语言:javascript
复制
@objc func handleTap(_ gesture:UITapGestureRecognizer) {
        startRecording()

        if let rotate = rotate{
            animator.removeBehavior(rotate)
            self.rotate = nil
        } else {
            rotate = UIDynamicItemBehavior(items: [self.recordingSpinner])
            rotate?.allowsRotation = true
            rotate?.angularResistance = 0
            rotate?.addAngularVelocity(1, for: self.recordingSpinner)
            animator.addBehavior(rotate!)
        }
    }

摘自这个答案:Proper way to stop an infinitely rotating image? and how does one implement removeAllAnimations?

有趣的是,似乎没有开始旋转,当我执行的时候,虽然我不知道为什么。如果有人想知道我为什么想听他们的话

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50845320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档