2017-05-08 112 views
2

只是一個快速的問題,我正在爲我當前的應用程序設置設置動畫背景圖像。本質上,我想讓我目前仍然背景圖像從左到右滾動,在我的視圖中不斷地滾動;但我一直在拼命拼湊。從各種各樣的其他來源,我已經包含這個功能,目前沒有工作,我真的不知道如何或爲什麼。Swift - 動畫UIImageView圖像

func animate(_ image: UIImageView) { 
     UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: { 
      image.transform = CGAffineTransform(translationX: -100, y: 0) 
     }) { (success: Bool) in 
      image.transform = CGAffineTransform.identity 
      self.animate(image) 
     } 
    } 

任何幫助/援助非常感謝!

+0

你需要什麼樣的動畫?清楚解釋 –

+0

對不起,缺乏清晰度!我希望我的動畫能夠成爲這件事的一部分; https://cdn.dribbble.com/users/279765/screenshots/1335470/marioscroll_real_real.gif – Jordy

回答

1

你並不需要使用CAffineTranform裏面animateWithDuration,你可以簡單的定義,如新的中心:

let oldCenter = image.center 
let newCenter = CGPoint(x: oldCenter.x - 100, y: oldCenter.y) 

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

希望它能幫助。

+0

試圖實施你的建議後,我建議調整代碼如下:let oldCenter = background.center let newCenter = CGRect (origin:oldCenter.x - 100,size:oldCenter.y) UIView.animate(withDuration:5,delay:0,options:.curveLinear,animations:{ background.center = newCenter }){(success: Bool) print(「完成移動圖像」) } 但是然後我給二進制運算符錯誤爲「 - 」不能應用於輸入「Int」或「CGFloat」 – Jordy

+0

對不起,我的壞它的CGPoint不CGREct。現在試用CGPoint –

+0

動畫中的確定進展。它慢慢地開始向左滑動,然後在圖像中停止大約1/6。最重要的是,它似乎沒有從圖像的右側重新出現;所以我要實現這個功能? – Jordy