Skip to content

Instantly share code, notes, and snippets.

@mzeeshanid
Created April 5, 2020 11:19
Show Gist options
  • Save mzeeshanid/e9cc25e2b769eec8c2ebf1e8fa8b5f92 to your computer and use it in GitHub Desktop.
Save mzeeshanid/e9cc25e2b769eec8c2ebf1e8fa8b5f92 to your computer and use it in GitHub Desktop.
Synchronised splash animation
//
// ViewController.swift
// splashanimation
//
// Created by Muhammad Zeeshan on 05/04/2020.
// Copyright © 2020 mzeeshanid. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
@IBOutlet var loader: UIActivityIndicatorView!
var images: [UIImage] = []
var currentIndex: Int = 0
var isScreenVisible: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
for i in 1..<7 {
let imageName = "image_slice_" + "\(i)"
let image = UIImage(named: imageName)!
self.images.append(image)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.isScreenVisible = true
self.animateImageChange()
self.loader.startAnimating()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
self.isScreenVisible = false
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.loader.stopAnimating()
}
//MARK: My Methods
func animateImageChange() {
UIView.transition(with: self.imageView, duration: 1.8, options: .transitionCrossDissolve, animations: {
self.imageView.image = self.images[self.currentIndex]
}) { (finished) in
if self.isScreenVisible {
self.currentIndex += 1
if self.currentIndex > (self.images.count - 1) {
self.currentIndex = 0
}
self.animateImageChange()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment