Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created August 9, 2015 10:13
Show Gist options
  • Save kakopappa/f5ce52d9b29deb0bf03c to your computer and use it in GitHub Desktop.
Save kakopappa/f5ce52d9b29deb0bf03c to your computer and use it in GitHub Desktop.
Show UIActivityIndicatorView inside UIImageView
//
// UIImageViewExtention.swift
// MealSpecials
//
// Created by Aruna on 8/9/15.
// Copyright (c) 2015 com.mealspecials. All rights reserved.
//
import Foundation
import UIKit
import ObjectiveC
private var activityIndicatorAssociationKey: UInt8 = 0
extension UIImageView {
var activityIndicator: UIActivityIndicatorView! {
get {
return objc_getAssociatedObject(self, &activityIndicatorAssociationKey) as? UIActivityIndicatorView
}
set(newValue) {
objc_setAssociatedObject(self, &activityIndicatorAssociationKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN))
}
}
func showActivityIndicator() {
if (self.activityIndicator == nil) {
self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
self.activityIndicator.hidesWhenStopped = true
self.activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
self.activityIndicator.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
self.activityIndicator.autoresizingMask = .FlexibleLeftMargin | .FlexibleRightMargin | .FlexibleTopMargin | .FlexibleBottomMargin
self.activityIndicator.userInteractionEnabled = false
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.addSubview(self.activityIndicator)
self.activityIndicator.startAnimating()
})
}
}
func hideActivityIndicator() {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.activityIndicator.stopAnimating()
})
}
}
@yawboafo
Copy link

How to i implement this on a UICollectionView cell with UIImageView when loading data with from a server using URLSession ?

@awais
Copy link

awais commented Dec 13, 2019

you should use kingfisher pod to get to know that you got the response from API. Before parsing the response to views implement show indicator and after parsing call hideIndicator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment