【问题标题】:Fade out CABasicAnimation淡出 CABasicAnimation
【发布时间】:2016-04-07 23:16:45
【问题描述】:

我正在使用一个CABasicAnimation 来反复上下缩放CALayer。这意味着动画是自动反转的,重复无限次,它的关键路径是transform.scale

我想做的是在某个点“淡出”动画。所以我不想立即停止它(使用layer.removeAnimationForKey:),但我希望在例如一秒的时间跨度内平滑地删除动画。我尝试将layer.removeAnimationForKey: 包装在CATransaction 块中并设置其animationDuration,但这也不起作用。

感谢任何帮助。

【问题讨论】:

    标签: iphone swift calayer cabasicanimation


    【解决方案1】:

    这是一个淡出的例子,当我用 AVPlayer 播放的视频结束时调用它。

    override func viewWillAppear(animated: Bool) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.finishedPlaying(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
    }
    
    var fadeOut:CABasicAnimation!
    
    func finishedPlaying(myNotification:NSNotification) {
    
        self.commsRightNow = commsMode.appleTV.rawValue
        fadeOut = CABasicAnimation(keyPath: "opacity")
        fadeOut.fromValue = 1.0
        fadeOut.toValue = 0.0
        fadeOut.duration = 8.0
        fadeOut.delegate = self
        fadeOut.setValue("video", forKey:"fadeOut")
        fadeOut.removedOnCompletion = false
        fadeOut.fillMode = kCAFillModeForwards
        playerLayer.addAnimation(fadeOut, forKey: nil)
    }
    

    您还可以在动画完成时调用以下方法来执行其他操作。这个完全删除了 AVPlayer 对象。请注意,fadeOut 和 video 键是用户定义的值并包含在内,因为您可能需要识别多个动画开始(&停止)。

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        let nameValue = anim.valueForKey("fadeOut") as? String
        if let name = nameValue {
            if (name == "video") {
                playerLayer?.removeFromSuperlayer()
    
    
            }
        }
    

    您也可以在动画开始时调用它来触发。

    override func animationDidStart(anim: CAAnimation) {
        // Blah Blah
    }
    

    【讨论】:

    • 谢谢。但我不认为这个答案能达到我想要的。我不想让图层淡出;我希望动画本身淡出。我的动画反复将变换的比例从 1 更改为 1.5,反之亦然。我希望这个效果能够平滑地淡出,以便最后的比例为 1(默认)。
    猜你喜欢
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多