2017-05-10 137 views
0

只是快速查詢實現我與動畫UIImageView的問題。我成功實現了將圖像動畫化以滑出屏幕;但是我希望它在它存在的視圖模擬一個側滾動遊戲動畫時重新出現。Xcode Swift - 重複動畫UIImageView

事情是這樣的: enter image description here

我試圖實現一個完成處理程序,但努力理解如何實現它的邏輯,所以我打消了我的企圖;所以我的代碼保留如下:

let oldCenter = background.center 
     let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y) 

     UIView.animate(withDuration: 2, delay: 0, options: .curveLinear, animations: { 
      self.background.center = newCenter 
     }) { (success: Bool) in 
      print("Done moving image") 
     } 

有關如何實現我所需的動畫的示例或指針,將不勝感激!

回答

2
let oldCenter = view.center 
    let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y) 
    UIView.animate(withDuration: 2.0, delay: 0.0, options: 
    [.curveLinear, .repeat], animations: { 
     view.center = newCenter 
    }, completion: nil) 
0

您可以嘗試的UIView UIView的重複選項

let oldCenter = background.center 
    let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y) 

    UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear, repeat], animations: { 
     self.background.center = newCenter 
    }) { (success: Bool) in 
     print("Done moving image") 
     if(success) 
     { 
      self.background.center = oldCenter 
     } 
    } 
1

如果你想重複動畫, 沒有必要實施completionanimate(withDuration:delay:options:animations:completion:)options參數(UIViewAnimationOptions),它是:

指示要如何進行動畫選項的面具。 有關有效常量的列表,請參閱UIViewAnimationOptions。

一個常數爲UIViewAnimationOptions的是repeat

動畫的重複下去。

所以,你應該做的:

UIView.animateKeyframes(withDuration: 2.0, delay: 0.0, options: [.curveLinear, .repeat], animations: { 
    self.background.center = newCenter 
}, completion: nil) 

再次,實施completion不需要重複的動畫,實現它是由你的情況。