GifCollectionViewCell.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // GifCollectionViewCell.swift
  3. import UIKit
  4. class GifCollectionViewCell: UICollectionViewCell {
  5. static let reuseIdentifier = NSStringFromClass(GifCollectionViewCell.self)
  6. private lazy var imageView: LLAnimatableImageView = {
  7. let imageView = LLAnimatableImageView()
  8. imageView.contentMode = .center
  9. imageView.clipsToBounds = true
  10. return imageView
  11. }()
  12. private lazy var titleLabel: UILabel = {
  13. let lbl = UILabel()
  14. lbl.numberOfLines = 0
  15. return lbl
  16. }()
  17. var gif: GifObject? {
  18. didSet{
  19. self.didSetGif()
  20. }
  21. }
  22. override init(frame: CGRect) {
  23. super.init(frame: frame)
  24. self.setupSelf()
  25. }
  26. required init?(coder: NSCoder) {
  27. fatalError("init(coder:) has not been implemented")
  28. }
  29. private func setupSelf() {
  30. self.contentView.addSubview(self.titleLabel)
  31. // TODO: Add an Image to the UICollectionViewCell
  32. // Use `self.imageView` variable to add an image to the cell.
  33. // Please make the image height and width conform to the cell's bounds
  34. self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
  35. NSLayoutConstraint.activate([
  36. self.titleLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
  37. self.titleLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
  38. self.titleLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
  39. self.titleLabel.heightAnchor.constraint(lessThanOrEqualTo: self.contentView.heightAnchor, multiplier: 0.5)
  40. ])
  41. }
  42. private func didSetGif() {
  43. if let gif = gif {
  44. self.titleLabel.text = gif.title
  45. self.imageView.loadGifFromUrl(urlString: gif.images.fixedWidth.url)
  46. }
  47. }
  48. }