UIImageView动画无效

时间:2016-05-10 06:14:01

标签: ios swift uiimageview nsarray

我正在研究swift中的UIImageView转换。我将六个图像存储在一个数组中,并将该数组提供给“imageview.animationImages “,我试图让它作为'Gif动画'工作,我写的代码如下:

  logoImages = NSMutableArray(array: ["backGround4.jpeg","backGround3.jpeg","backGround.jpeg","16.jpeg","23.jpeg","backGround1.jpeg"])

 imageview.animationImages = NSArray(array:  ["backGround4.jpeg","backGround3.jpeg","backGround.jpeg","16.jpeg","23.jpeg","backGround1.jpeg"]) as? [UIImage]



        imageview.animationDuration = 1.5

        imageview.animationRepeatCount = 1

        imageview.startAnimating()

它不起作用。任何人都可以告诉我,我在这里做的错误是什么? 在此先感谢

3 个答案:

答案 0 :(得分:2)

首先更正您的图片名称,例如backGround1.jpeg,backGround2.jpeg等。

之后创建一个将保存所有图像的图像数组:

var imageArray = [UIImage]()

然后在图像阵列中添加所有图像:

for var i = 1; i < totalImageCount; i++ {

    let image = UIImage(named: "backGround4\(i).png")
    imageArray.append(image!)
}

之后你可以用这种方式创建动画:

imageview.animationImages = imageArray
imageview.animationDuration = 1.5
imageview.startAnimating()

答案 1 :(得分:0)

请将字符串转换为UIImage,如下所示:

imageview.animationImages = NSArray(array:  ["backGround4.jpeg","backGround3.jpeg","backGround.jpeg","16.jpeg","23.jpeg","backGround1.jpeg"].map({ (string) -> UIImage in
    UIImage(named: string)! })) as? [UIImage]

答案 2 :(得分:0)

请试试这个

var images: [UIImage] = []
for i in 1...2 {
    images.append(UIImage(named: "c\(i)")!)
}
myImageView.animationImages = images
myImageView.animationDuration = 1.0
myImageView.startAnimating()