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 }
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.
[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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With