2017-04-21 97 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) 

希望它有帮助。 :)