GifCollectionViewCell.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // GifCollectionViewCell.swift
  3. // LiveLikeGiphyChallenge
  4. //
  5. // Created by Ljupco Nastevski on 30.1.22.
  6. //
  7. import UIKit
  8. class GifCollectionViewCell: UICollectionViewCell {
  9. static let reuseIdentifier = NSStringFromClass(GifCollectionViewCell.self)
  10. private lazy var imageView: LLAnimatableImageView = {
  11. let imageView = LLAnimatableImageView()
  12. imageView.contentMode = .center
  13. imageView.clipsToBounds = true
  14. return imageView
  15. }()
  16. private lazy var titleLabel: UILabel = {
  17. let lbl = UILabel()
  18. lbl.numberOfLines = 0
  19. return lbl
  20. }()
  21. var gif: GifObject? {
  22. didSet{
  23. self.didSetGif()
  24. }
  25. }
  26. override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. self.setupSelf()
  29. }
  30. required init?(coder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. private func setupSelf() {
  34. self.contentView.addSubview(self.imageView)
  35. self.contentView.addSubview(self.titleLabel)
  36. self.imageView.translatesAutoresizingMaskIntoConstraints = false
  37. self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
  38. NSLayoutConstraint.activate([
  39. self.imageView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
  40. self.imageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
  41. self.imageView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
  42. self.imageView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
  43. self.titleLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
  44. self.titleLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
  45. self.titleLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
  46. self.titleLabel.heightAnchor.constraint(lessThanOrEqualTo: self.contentView.heightAnchor, multiplier: 0.5)
  47. ])
  48. }
  49. private func didSetGif() {
  50. if let gif = gif {
  51. self.titleLabel.text = gif.title
  52. self.imageView.loadGifFromUrl(urlString: gif.images.fixedWidth.url)
  53. }
  54. }
  55. }