mark mark - 5 months ago 42
Swift Question

How to add multiple images in single image view and to show all images for every 2 second continuously

I have one view controller and I kept one full mage view in that view controller.And i have some image name like 1.png,2.png,3.png , like 6 images.What I need is I need to show as like slide .i.e for every 2 second one image should be in image view. I have done it in Objective-C. But I am beginner in Swift, so not able to do it. How to do this?

@implementation ViewController {
UIImageView* imgView; // your UIImageView
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self navigationController] setNavigationBarHidden:YES animated:YES];

UIImage* img1 = [UIImage imageNamed:@"11.jpg"];
UIImage* img2 = [UIImage imageNamed:@"s2.jpg"];


imgView = [[UIImageView alloc] initWithFrame:self.view.frame]; // create a UIImageView with the same size as the view.
imgView.animationImages = @[img1, img2]; // use a nice NSArray literal to pass in your images.
imgView.animationDuration = 2.0*2.0; // 2 seconds for each image (x3 images)
[self.view addSubview:imgView]; // add UIImageView to view

[imgView startAnimating]; // start animating
}


Like that in Swift I need to do :

I am having image view name as
imagview
.And please don't compare the Obj-C code. Because there I have added some library file for animating. Can i code to do this continuous image showing for every 2 seconds in Swift?

Answer
var imagesListArray = [UIImage]()

for imageName in 1...3
{
    imagesListArray.append(UIImage(named: "\(imageName).png")!)
}

// You can also use below code to add images if not want to use loop    
// imagesListArray.append(UIImage(named: "1.png")!)
// imagesListArray.append(UIImage(named: "2.png")!)
// imagesListArray.append(UIImage(named: "3.png")!)

self.imageView.animationImages = imagesListArray
self.imageView.animationDuration = 2.0
self.imageView.startAnimating()