使用 Swift 在视频中淡入淡出的代码



我想淡化我正在播放的视频,但我做错了什么,因为下面的代码不起作用。希望第二双眼睛看一看?

  let videoURL = slide2show.aURL
  playerItem = AVPlayerItem(URL: videoURL)
  player = AVPlayer(playerItem: playerItem)
  playerLayer = AVPlayerLayer(player: player)
  playerLayer.frame = CGRect(x: 128, y: 128, width: 512, height: 256)
  dispatch_async(dispatch_get_main_queue()) { [unowned self] in
     self.playerLayer.opacity = 0.0;
     self.fadeInV = CABasicAnimation(keyPath: "opacity")
     self.fadeInV.fromValue = 0.0
     self.fadeInV.toValue = 1.0
     self.fadeInV.duration = 8.0
     self.fadeInV.delegate = self
     self.fadeInV.setValue("video", forKey:"fadeInV")
     self.fadeInV.removedOnCompletion = false
     self.fadeInV.fillMode = kCAFillModeForwards
     self.playerLayer.addAnimation(self.fadeInV, forKey: "opacity")
     self.view.layer.addSublayer(self.playerLayer)

而启动视频的呼声,一旦淡入?

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
    print("animationDidStop")
    let nameValue3 = anim.valueForKey("fadeInV") as? String
    if let name = nameValue3 {
        if (name == "video") {
            self.player.play()
        }
    }

我是这样做的,但是我添加了其他视图,您可以尝试添加图像

let vc: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("myVC")
        self.addChildViewController(vc)
        self.view!.addSubview(vc.view!)
        UIView.animateWithDuration(0.2, animations: {() -> Void in
            vc.view.alpha = 0
            vc.view.alpha = 1
            }, completion: {(finished: Bool) -> Void in
                UIView.animateWithDuration(0.2, animations: {() -> Void in
                    vc.view.alpha = 1
                    vc.view.alpha = 0
                    }, completion: {(finished: Bool) -> Void in
                        vc.view!.removeFromSuperview()
                })
        })

最新更新