Guest User

Untitled

a guest
Mar 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. //
  2. // LoadingImageView.swift
  3. // Noverish Harold
  4. //
  5. // Created by Noverish Harold on 2018. 3. 7..
  6. // Copyright © 2018 Noverish Harold. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Alamofire
  11. import AlamofireImage
  12.  
  13. class LoadingImageView: UIImageView {
  14.  
  15. private var indicator: UIActivityIndicatorView?
  16. private var nowLoadingNum = 0
  17. var imageUrl: String? {
  18. didSet {
  19. if self.imageUrl == oldValue {
  20. return
  21. }
  22.  
  23. self.image = nil
  24. guard let imageUrl = self.imageUrl else {
  25. return
  26. }
  27.  
  28. if indicator == nil {
  29. let indicator = UIActivityIndicatorView(frame: self.bounds)
  30. indicator.color = .black
  31. indicator.startAnimating()
  32. self.addSubview(indicator)
  33. self.indicator = indicator
  34. }
  35.  
  36. self.nowLoadingNum += 1
  37. Alamofire.request(imageUrl).responseImage { response in
  38. DispatchQueue.main.async {
  39. self.nowLoadingNum -= 1
  40. if self.nowLoadingNum != 0 {
  41. return
  42. }
  43.  
  44. self.indicator?.removeFromSuperview()
  45. self.indicator = nil
  46.  
  47. if let image = response.result.value {
  48. UIView.transition(with: self,
  49. duration: 0.4,
  50. options: .transitionCrossDissolve,
  51. animations: { self.image = image },
  52. completion: nil)
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. override func layoutSubviews() {
  60. super.layoutSubviews()
  61. indicator?.frame = self.bounds
  62. }
  63. }
Add Comment
Please, Sign In to add comment