2017-04-21 92 views
0

我想爲後面的背景圖片設置動畫效果,當它到達時它應該再次上升。我想只動畫一個圖像。不知何故,當我運行代碼時圖像不可見。圖像尺寸如下寬度:375和高度:666動畫背景圖片使用Swift連續循環使用,UIKit

這裏是圖像

這裏是代碼

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var myView: UIView! 
var water:UIImageView! 

var hover:CABasicAnimation! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    water = UIImageView(image: UIImage(named: "water")) 
    water.frame = CGRect(x: 0, y: 0, width: 375.0, height: 666.0) 

    water.contentMode = .scaleAspectFill 

    UIView.animate(withDuration: 4.0, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { 
     self.water.frame.origin.y = self.view.bounds.height + self.water.frame.height - 50 

    }, completion: nil) 

    self.view.insertSubview(water, aboveSubview: myView) 





} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

+0

嗨。請檢查我的答案。它工作嗎? –

回答

2

可以使用[.repeat, .autoreverse]動畫中的選項

UIView.animate(withDuration: 4.0, delay: 0, options: [.repeat, .autoreverse, .curveLinear], animations: { 
    self.water.frame.origin.y = self.view.bounds.height + self.water.frame.height - 50 

}, completion: nil) 

希望它有幫助。 :)