diff --git a/Source/LightboxDataSource.swift b/Source/LightboxDataSource.swift index 750bd880..8463d655 100644 --- a/Source/LightboxDataSource.swift +++ b/Source/LightboxDataSource.swift @@ -14,13 +14,18 @@ extension LightboxController: UICollectionViewDataSource { let config = LightboxConfig.sharedInstance.config cell.parentViewController = self + cell.loadingIndicator.alpha = 0 cell.setupTransitionManager() if let imageString = image as? String { if let imageURL = NSURL(string: imageString) where config.remoteImages { + cell.loadingIndicator.alpha = 1 config.loadImage( imageView: cell.lightboxView.imageView, URL: imageURL) { error in - if error == nil { cell.lightboxView.updateViewLayout() } + if error == nil { + cell.loadingIndicator.alpha = 0 + cell.lightboxView.updateViewLayout() + } } } else { cell.lightboxView.imageView.image = UIImage(named: imageString) diff --git a/Source/LightboxView.swift b/Source/LightboxView.swift index c42ce288..b79ebaa9 100644 --- a/Source/LightboxView.swift +++ b/Source/LightboxView.swift @@ -10,8 +10,9 @@ public class LightboxView: UIView { let imageView = UIImageView(frame: CGRectZero) imageView.translatesAutoresizingMaskIntoConstraints = false imageView.userInteractionEnabled = true + return imageView - }() + }() lazy var scrollView: UIScrollView = { [unowned self] in let scrollView = UIScrollView(frame: CGRectZero) @@ -24,7 +25,7 @@ public class LightboxView: UIView { scrollView.showsHorizontalScrollIndicator = false return scrollView - }() + }() var imageConstraintLeading: NSLayoutConstraint! var imageConstraintTrailing: NSLayoutConstraint! @@ -45,7 +46,7 @@ public class LightboxView: UIView { minimumZoomScale = config.zoom.minimumScale maximumZoomScale = config.zoom.maximumScale - scrollView.addSubview(self.imageView) + scrollView.addSubview(imageView) addSubview(scrollView) } diff --git a/Source/LightboxViewCell.swift b/Source/LightboxViewCell.swift index aba66cad..83bf490f 100644 --- a/Source/LightboxViewCell.swift +++ b/Source/LightboxViewCell.swift @@ -16,8 +16,20 @@ public class LightboxViewCell: UICollectionViewCell { return lightboxView }() + public lazy var loadingIndicator: UIActivityIndicatorView = { + let loadingIndicator = UIActivityIndicatorView(activityIndicatorStyle: .White) + loadingIndicator.startAnimating() + loadingIndicator.alpha = 0 + loadingIndicator.translatesAutoresizingMaskIntoConstraints = false + + return loadingIndicator + }() + public override func layoutSubviews() { super.layoutSubviews() + + if loadingIndicator.superview == nil { self.contentView.addSubview(loadingIndicator) } + setupConstraints() lightboxView.updateViewLayout() } @@ -37,6 +49,14 @@ public class LightboxViewCell: UICollectionViewCell { multiplier: 1, constant: 0)) } + addConstraint(NSLayoutConstraint(item: loadingIndicator, attribute: .CenterX, + relatedBy: .Equal, toItem: self.contentView, attribute: .CenterX, + multiplier: 1, constant: 0)) + + addConstraint(NSLayoutConstraint(item: loadingIndicator, attribute: .CenterY, + relatedBy: .Equal, toItem: self.contentView, attribute: .CenterY, + multiplier: 1, constant: 0)) + constraintsAdded = true } }