2014-10-27 95 views
1

我已經更新到與iOS 8.1 SDK的Xcode的最後一個版本,我得到我的函數的錯誤離子聲明:如何在Swift中動畫圖像?

@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") 
    ] 

的錯誤是:「類型的UIImage?不符合協議'AnyObject'

如何動畫圖像?

謝謝!

+2

不再!你會在錯誤信息中看到尾隨問號「UIImage?」嗎?這表明一個*可選*必須解開... – 2014-10-27 09:50:56

+0

或這一個:http://stackoverflow.com/questions/26442059/could-not-find-an-overload-for-init-that-accepts-the -wpplied-arguments-in-swi – 2014-10-27 09:55:05

+1

將解決你的問題http://stackoverflow.com/questions/26511448/could-not-find-overload-for-init-uiimage – leedream 2015-02-25 10:24:22

回答

8

小代碼示例。使用數組和循環來做到這一點。

//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()