使用 Swift、UIKit 连续循环动画背景图像
Animate Background Image continously in a loop using Swift, UIKit
我想为下面的背景图像制作动画,当它向下移动时它应该再次上升。我只想为一张图片制作动画。当我 运行 代码时,不知何故图像不可见。图片尺寸如下宽:375,高: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)
希望对您有所帮助。 :)
我想为下面的背景图像制作动画,当它向下移动时它应该再次上升。我只想为一张图片制作动画。当我 运行 代码时,不知何故图像不可见。图片尺寸如下宽:375,高: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)
希望对您有所帮助。 :)