Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to animate Images?

Tags:

I'm trying to animate images in particular time- duration. It is working fine in Objective C. However, it is not working for Swift, where is my 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 } 
like image 308
ChenSmile Avatar asked Jun 23 '14 11:06

ChenSmile


People also ask

How do I animate in SwiftUI?

SwiftUI has built-in support for animations with its animation() modifier. To use this modifier, place it after any other modifiers for your views, tell it what kind of animation you want, and also make sure you attach it to a particular value so the animation triggers only when that specific value changes.


1 Answers

[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() 
like image 95
Anil Varghese Avatar answered Oct 05 '22 09:10

Anil Varghese