2016-03-09 37 views
0

我想淡化我正在播放的视频,并且我做错了什么,因为下面的代码不起作用。会欣赏第二双眼睛去看看?使用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() 
     } 
    } 

回答

1

我做到了这样,但我已经添加了另一种观点,你可以尝试添加图像

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() 
       }) 
     })