小编典典

Swift-如何为图片制作动画?

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
}

快速代码是-

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
}

阅读 323

收藏
2020-07-07

共1个答案

小编典典

[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()
2020-07-07