【问题标题】:How to make a video pause when a calayer appears如何在出现 calayer 时暂停视频
【发布时间】:2016-08-03 02:28:54
【问题描述】:

我正在尝试让 calayer 在视频出现时暂停它。我不知道如何将其添加为另一个 calayer 或动画。这是我在导出之前添加 calayer 时的代码。因此,当动画发生时,我希望视频出现时暂停,然后在动画停止后恢复。

 let titleLayer = CATextLayer()
    titleLayer.backgroundColor = NSColor.clearColor().CGColor
    titleLayer.string = "Dummy text"
    titleLayer.font = NSFont(name: "Helvetica", size: 28)
    titleLayer.shadowOpacity = 0.5
    titleLayer.alignmentMode = kCAAlignmentCenter
    titleLayer.frame = CGRectMake(0, 50, size.width, size.height / 6)

    let animation: CABasicAnimation = CABasicAnimation(keyPath: "opacity")
    animation.duration = 0
    animation.fromValue = Int(1.0)
    animation.toValue = Int(0.0)
    animation.beginTime = 5
    animation.removedOnCompletion = false
    animation.fillMode = kCAFillModeForwards
    titleLayer.addAnimation(animation, forKey: "animateOpacity")

【问题讨论】:

    标签: swift macos avfoundation


    【解决方案1】:

    您可以在将动画添加到图层之前暂停视频,然后在动画完成后继续播放。要知道动画何时结束,您可以让一个对象成为 animation 的委托,然后在 animationDidStop:finished: 委托方法中恢复播放。

    func showTitle() {
        // configure animation { .. }
         animation.delegate = self 
        // pause video
         titleLayer.addAnimation(animation, forKey: "animateOpacity")
    }
    
    func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        // play video
    }
    

    【讨论】:

    • 感谢@gravicle 的回答。最后一个问题,我将如何使用 avexportsession 导出它,以便在播放 quicktime 移动时显示动画时间暂停然后恢复?
    • @spel 您必须为此使用AVMutableComposition。这个问题中的代码 sn-p 正在做你想做的事情:stackoverflow.com/questions/10887449/…
    猜你喜欢
    • 2014-09-25
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多