FSPagerViewCell.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // FSPagerViewCell.swift
  3. // FSPagerView
  4. //
  5. // Created by Wenchao Ding on 17/12/2016.
  6. // Copyright © 2016 Wenchao Ding. All rights reserved.
  7. //
  8. import UIKit
  9. open class FSPagerViewCell: UICollectionViewCell {
  10. /// Returns the label used for the main textual content of the pager view cell.
  11. @objc
  12. open var textLabel: UILabel? {
  13. if let _ = _textLabel {
  14. return _textLabel
  15. }
  16. let view = UIView(frame: .zero)
  17. view.isUserInteractionEnabled = false
  18. view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
  19. let textLabel = UILabel(frame: .zero)
  20. textLabel.textColor = .white
  21. textLabel.font = UIFont.preferredFont(forTextStyle: .body)
  22. self.contentView.addSubview(view)
  23. view.addSubview(textLabel)
  24. textLabel.addObserver(self, forKeyPath: "font", options: [.old,.new], context: kvoContext)
  25. _textLabel = textLabel
  26. return textLabel
  27. }
  28. /// Returns the image view of the pager view cell. Default is nil.
  29. @objc
  30. open var imageView: UIImageView? {
  31. if let _ = _imageView {
  32. return _imageView
  33. }
  34. let imageView = UIImageView(frame: .zero)
  35. self.contentView.addSubview(imageView)
  36. _imageView = imageView
  37. return imageView
  38. }
  39. fileprivate weak var _textLabel: UILabel?
  40. fileprivate weak var _imageView: UIImageView?
  41. fileprivate let kvoContext = UnsafeMutableRawPointer(bitPattern: 0)
  42. fileprivate let selectionColor = UIColor(white: 0.2, alpha: 0.2)
  43. fileprivate weak var _selectedForegroundView: UIView?
  44. fileprivate var selectedForegroundView: UIView? {
  45. guard _selectedForegroundView == nil else {
  46. return _selectedForegroundView
  47. }
  48. guard let imageView = _imageView else {
  49. return nil
  50. }
  51. let view = UIView(frame: imageView.bounds)
  52. imageView.addSubview(view)
  53. _selectedForegroundView = view
  54. return view
  55. }
  56. open override var isHighlighted: Bool {
  57. set {
  58. super.isHighlighted = newValue
  59. if newValue {
  60. self.selectedForegroundView?.layer.backgroundColor = self.selectionColor.cgColor
  61. } else if !super.isSelected {
  62. self.selectedForegroundView?.layer.backgroundColor = UIColor.clear.cgColor
  63. }
  64. }
  65. get {
  66. return super.isHighlighted
  67. }
  68. }
  69. open override var isSelected: Bool {
  70. set {
  71. super.isSelected = newValue
  72. self.selectedForegroundView?.layer.backgroundColor = newValue ? self.selectionColor.cgColor : UIColor.clear.cgColor
  73. }
  74. get {
  75. return super.isSelected
  76. }
  77. }
  78. public override init(frame: CGRect) {
  79. super.init(frame: frame)
  80. commonInit()
  81. }
  82. public required init?(coder aDecoder: NSCoder) {
  83. super.init(coder: aDecoder)
  84. commonInit()
  85. }
  86. fileprivate func commonInit() {
  87. self.contentView.backgroundColor = UIColor.clear
  88. self.backgroundColor = UIColor.clear
  89. self.contentView.layer.shadowColor = UIColor.black.cgColor
  90. self.contentView.layer.shadowRadius = 5
  91. self.contentView.layer.shadowOpacity = 0.75
  92. self.contentView.layer.shadowOffset = .zero
  93. }
  94. deinit {
  95. if let textLabel = _textLabel {
  96. textLabel.removeObserver(self, forKeyPath: "font", context: kvoContext)
  97. }
  98. }
  99. override open func layoutSubviews() {
  100. super.layoutSubviews()
  101. if let imageView = _imageView {
  102. imageView.frame = self.contentView.bounds
  103. }
  104. if let textLabel = _textLabel {
  105. textLabel.superview!.frame = {
  106. var rect = self.contentView.bounds
  107. let height = textLabel.font.pointSize*1.5
  108. rect.size.height = height
  109. rect.origin.y = self.contentView.frame.height-height
  110. return rect
  111. }()
  112. textLabel.frame = {
  113. var rect = textLabel.superview!.bounds
  114. rect = rect.insetBy(dx: 8, dy: 0)
  115. rect.size.height -= 1
  116. rect.origin.y += 1
  117. return rect
  118. }()
  119. }
  120. if let selectedForegroundView = _selectedForegroundView {
  121. selectedForegroundView.frame = self.contentView.bounds
  122. }
  123. }
  124. open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  125. if context == kvoContext {
  126. if keyPath == "font" {
  127. self.setNeedsLayout()
  128. }
  129. } else {
  130. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  131. }
  132. }
  133. }