ios - 使用 Swift、UIKit 连续循环动画背景图像

标签 ios swift animatewithduration

我想为下面的背景图像制作动画,当它向下移动时它应该再次上升。我只想为一张图片制作动画。当我运行代码时,图像不知何故不可见。图片尺寸如下 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/

相关文章:

ios - Alamofire 不同请求的不同超时

ios - 从初始化错误返回之前,未在所有路径上调用 Super.init

swift ReactiveCocoa combine最新

swift - 无法使用可选变量进行计算

ios - UIView按预期用animateWithDuration对setBackgroundColor进行动画处理。但是,UILabel立即发生。为什么?

ios - 快速动画所有添加的 View alpha

ios - QLPreviewController 编辑模式不起作用?

iphone - 保持 UIScrollView 的 subview 不调整大小?

swift - 在 10.9 上编译 Swift?

ios - 如何为数组中的 UILabel 设置动画以在它们之间以相等的延迟在屏幕上移动?