CustomOverrideCollectionViewCell.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // CustomOverrideCollectionViewCell.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 11/5/19.
  6. // Copyright © 2019 LoopKit Authors. All rights reserved.
  7. //
  8. import UIKit
  9. final class CustomOverrideCollectionViewCell: UICollectionViewCell, IdentifiableClass {
  10. @IBOutlet weak var titleLabel: UILabel!
  11. private lazy var overlayDimmerView: UIView = {
  12. let view = UIView()
  13. if #available(iOSApplicationExtension 13.0, *) {
  14. view.backgroundColor = .systemBackground
  15. } else {
  16. view.backgroundColor = .white
  17. }
  18. view.alpha = 0
  19. view.translatesAutoresizingMaskIntoConstraints = false
  20. return view
  21. }()
  22. override func awakeFromNib() {
  23. super.awakeFromNib()
  24. let selectedBackgroundView = UIView()
  25. self.selectedBackgroundView = selectedBackgroundView
  26. if #available(iOSApplicationExtension 13.0, iOS 13.0, *) {
  27. selectedBackgroundView.backgroundColor = .tertiarySystemFill
  28. backgroundColor = .secondarySystemGroupedBackground
  29. layer.cornerCurve = .continuous
  30. } else {
  31. selectedBackgroundView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
  32. backgroundColor = .white
  33. }
  34. layer.cornerRadius = 16
  35. addSubview(overlayDimmerView)
  36. NSLayoutConstraint.activate([
  37. overlayDimmerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  38. overlayDimmerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  39. overlayDimmerView.topAnchor.constraint(equalTo: topAnchor),
  40. overlayDimmerView.bottomAnchor.constraint(equalTo: bottomAnchor)
  41. ])
  42. }
  43. override func prepareForReuse() {
  44. removeOverlay(animated: false)
  45. }
  46. func applyOverlayToFade(animated: Bool) {
  47. if animated {
  48. UIView.animate(withDuration: 0.3, animations: {
  49. self.overlayDimmerView.alpha = 0.5
  50. })
  51. } else {
  52. self.overlayDimmerView.alpha = 0.5
  53. }
  54. }
  55. func removeOverlay(animated: Bool) {
  56. if animated {
  57. UIView.animate(withDuration: 0.3, animations: {
  58. self.overlayDimmerView.alpha = 0
  59. })
  60. } else {
  61. self.overlayDimmerView.alpha = 0
  62. }
  63. }
  64. }