2016-04-25 70 views
1

這裏寫的功能是爲了動畫視圖如下:2秒淡出,2秒淡入,2秒延遲並重復。出於某種原因,動畫只會執行一次,不會重複。我在這裏做錯了什麼?快速重複鏈接動畫

UIView.animateWithDuration(6.0, 
      delay: 0.0, 
      options: [.AllowUserInteraction,.Repeat,.BeginFromCurrentState], 
      animations: { 

      UIView.animateWithDuration(2.0, 
       delay: 0.0, 
       options: [.AllowUserInteraction,.BeginFromCurrentState] , 
       animations: { 
        //fade out 
        self.alpha = 0.5 
       }, 
       completion: { finished in 

        UIView.animateWithDuration(2.0, 
         delay: 0.0, 
         options: [.AllowUserInteraction,.BeginFromCurrentState], 
         animations: { 
          //fade in 

          self.alpha = 1.0 
         }, 
         completion: { finished in 

          UIView.animateWithDuration(2.0, 
           delay: 0.0, 
           options: [.AllowUserInteraction,.BeginFromCurrentState], 
           animations: { 

           }, 
           completion: { finished in          
          })                
        })        
      }) 
     }, 
     completion: { finished in 
    }) 
} 
+0

因爲上一個'animation'中的'completion'塊沒有任何內容。你想要什麼動畫,你只能叫'自我'? – Khuong

回答

2

您可以使用Core Animation的代表永遠重複你的動畫,例如:

func animateCustomView(layer: CALayer) { 
    let speed = 60.0/Double(view.layer.frame.size.width) 
    let duration: NSTimeInterval = Double(view.layer.frame.size.width - layer.frame.origin.x) * speed 

    let move = CABasicAnimation(keyPath: "position.x") 
    move.duration = duration 
    move.toValue = self.view.bounds.size.width + layer.bounds.width/2 
    move.delegate = self 
    move.setValue("view", forKey: "name") 
    move.setValue(layer, forKey: "layer") 

    layer.addAnimation(move, forKey: nil) 
} 

// Core Animation Delegate implementation 

override func animationDidStop(anim: CAAnimation, finished flag: Bool) { 

    if let name = anim.valueForKey("name") as? String{ 
     if name == "view" { 
      let layer = anim.valueForKey("layer") as? CALayer 
      layer?.position.x = -(layer?.bounds.width)!/2 
      delay(seconds: 0.5, completion: { 
       self.animateCustomView(layer!) 
      }) 
      // Here add more delays and in completion handler block, add your chained animations. 
     } else if name == "other name" { 
      // or you can reinitialize it your another chained animations, delay it and call it again. 
     } 
    } 
} 

override func viewDidAppear(animated: Bool) { 
    animateCustomView(viewToAnimate.layer) 
    // Here call your other chained animations 
} 

的funcs中的含量是例如目的只是代碼,你可以在那裏添加自定義動畫。

+0

thanx爲解決方案。 – nevgauker