我想为下面的背景图像制作动画,当它向下移动时它应该再次上升。我只想为一张图片制作动画。当我运行代码时,图像不知何故不可见。图片尺寸如下 width: 375 height: 666
这是图片
这是代码
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.
}
最佳答案
您可以在动画中使用[.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)
希望对您有所帮助。 :)
关于ios - 使用 Swift、UIKit 连续循环动画背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43532182/