2016-03-09 35 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() 
       }) 
     })