ios - Swift - 如何为图像制作动画?

标签 ios swift

我正在尝试在特定的持续时间内为图像制作动画。它在 Objective C 中运行良好。但是,它不适用于 Swift,我的错误在哪里?

Objective-C 的代码是 -

-(void)viewDidLoad
{
   [super viewDidLoad];
   NSMutableArray *imgListArray=[NSMutableArray array];
   for (int i=0; i<=11; i++)

   {
       NSString *strImageName=[NSString stringWithFormat:@"c%d.png", i];
       NSLog(@"%@",strImageName);
       UIImage *image=[UIImage imageNamed:strImageName];
       [imgListArray addObject:image];
   }

   self.imgView.animationImages = imgListArray;
   self.imgView.animationDuration =1.0f;    
   [self.imgView startAnimating];
   // Do any additional setup after loading the view, typically from a nib
}

swift 的代码是-

override func viewDidLoad()
{
   super.viewDidLoad()

   var imgListArray :NSMutableArray = []
   for countValue in 1...11
   {
      var strImageName : String = "c\(countValue).png"
      var image = UIImage(named:strImageName) // suggested by Anil
      imgListArray.addObject(image)
   }

   // Swift code HERE for Objective c
}

最佳答案

[UIImage imageNamed (strImageName)]

这不是快速代码。很快就会是

UIImage(named:strImageName)  

修改后的代码:

var imgListArray :NSMutableArray = []
for countValue in 1...11
    {

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

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

关于ios - Swift - 如何为图像制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24364504/

相关文章:

javascript - 您如何检测移动 safari 上的 3G 与 Wifi 连接?

ios - 从 MapKit 中获取中心坐标并显示在 UILabel 中

ios - 使用泛型时的 EXC_BAD_ACCESS

swift - 如何快速绘制一个复杂的圆?

ios - IOS 的应用程序内可以接收短信通知吗?

objective-c - 如何使用 Facebook iOS SDK 在照片中标记用户?

ios - 安装新pod后依赖分析错误

json - Swift 解析 JSON、嵌套数组

ios - Swift 2.0 中的 supportedInterfaceOrientations() 错误

ios - 为什么这个 AES128 解密在 iPhone 上需要这么长时间?