Search code examples
iosswiftxcodeuiwebview

How to make Custom Activity Indicator using Gif


I have searched through the internet in how to make a custom activity indicator, but many show that you need static images(like this one), and use those for the animation.

However, I was wondering if there was a way to make a custom activity indicator using a gif, instead of having to make several static images. I would appreciate it if the answer could be in swift.

Here is my current code for the default Activity Indicator(shows default load until web is fully loaded):

class WebViewSports: UIViewController, UIWebViewDelegate{

@IBOutlet weak var WebViewTst: UIWebView!

@IBOutlet weak var activityIndicatorWeb: UIActivityIndicatorView! 

override func viewDidLoad() {
        super.viewDidLoad()

    self.navigationController?.navigationBar.topItem!.title = "Apple"

    let URL = NSURL(string: "https://apple.com")
    WebViewTst.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)

}

func webViewDidStartLoad(_ webView: UIWebView) {

    activityIndicatorWeb.startAnimating()
    NSLog("Webview has started loading")
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    activityIndicatorWeb.stopAnimating()
    NSLog("Webview has successfully loaded")
}

Solution

  • An animated GIF is nothing more than a series of static images packaged in a common file format. There's no built-in mechanism for creating an activity indicator from an animated GIF but it would not be that hard to do.

    I suggest starting from a third party framework that knows how to display animated GIFs, and adapting the code that extracts the sequence of images from within the GIF.