xcode 如何在 Swift 中为图像设置动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26584729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:01:07  来源:igfitidea点击:

How to animate images in Swift?

iosxcodeswift

提问by user3745888

I have updated to last version of xcode with ios 8.1 sdk and I get an error ion declaration of my function in:

我已使用 ios 8.1 sdk 更新到最新版本的 xcode,并且在以下位置收到我的函数的错误离子声明:

  @IBOutlet weak var imageAnimation: UIImageView!

  imageAnimation.animationImages = [
        UIImage(named: "5.png"),
        UIImage(named: "4.png"),
        UIImage(named: "3.png"),
        UIImage(named: "2.png"),
        UIImage(named: "1.png")
    ]

The error is: 'Type UIImage? does not conform to protocol 'AnyObject'

错误是:'键入 UIImage?不符合协议“AnyObject”

How can animate images?

如何使图像动画化?

Thanks!

谢谢!

回答by Oleg Gordiichuk

Small code example for you. Use array and for loop to do this.

给你的小代码示例。使用数组和 for 循环来做到这一点。

    //add images to the array
    var imagesListArray :NSMutableArray = []
    //use for loop
    for position in 1...5
    {

        var strImageName : String = "c\(position).png"
        var image  = UIImage(named:strImageName)
        imagesListArray.addObject(image)
    }

    self.imageView.animationImages = imagesListArray;
    self.imageView.animationDuration = 1.0
    self.imageView.startAnimating()