I want to animate the following background image, when it reaches down it should go up again. I want to animate only one image. Somehow the image is not visible when I run the code. The image dimensions are as follows width: 375 and height: 666
Here is the image
Here is the code
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.
}
}
You can use [.repeat, .autoreverse]
options in your animation
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)
Hope it help. :)