Imran Imran - 1 month ago 8
Swift Question

Swift - How to animate Images?

I'm trying to animate images in particular time- duration. It is working fine in Objective C. But it is not working for SWIFT, please suggest me, where m i doing mistake.

The code for Objective C is -

-(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
}


The Code for swift is-

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
}

Answer
[UIImage imageNamed (strImageName)]

This not swift code. In swift it would be

UIImage(named:strImageName)  

Modified code:

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