UITableViewVibrantCell.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // UITableViewVibrantCell.swift
  3. // Pods
  4. //
  5. // Created by Jon Kent on 1/14/16.
  6. //
  7. //
  8. import UIKit
  9. open class UITableViewVibrantCell: UITableViewCell {
  10. private var vibrancyView: UIVisualEffectView = UIVisualEffectView()
  11. private var vibrancySelectedBackgroundView: UIVisualEffectView = UIVisualEffectView()
  12. private var defaultSelectedBackgroundView: UIView?
  13. open var blurEffectStyle: UIBlurEffect.Style? {
  14. didSet {
  15. updateBlur()
  16. }
  17. }
  18. // For registering with UITableView without subclassing otherwise dequeuing instance of the cell causes an exception
  19. public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  20. super.init(style: style, reuseIdentifier: reuseIdentifier)
  21. }
  22. required public init?(coder aDecoder: NSCoder) {
  23. super.init(coder: aDecoder)
  24. vibrancyView.frame = bounds
  25. vibrancyView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  26. for view in subviews {
  27. vibrancyView.contentView.addSubview(view)
  28. }
  29. addSubview(vibrancyView)
  30. let blurSelectionEffect = UIBlurEffect(style: .light)
  31. vibrancySelectedBackgroundView.effect = blurSelectionEffect
  32. defaultSelectedBackgroundView = selectedBackgroundView
  33. updateBlur()
  34. }
  35. internal func updateBlur() {
  36. // shouldn't be needed but backgroundColor is set to white on iPad:
  37. backgroundColor = UIColor.clear
  38. if let blurEffectStyle = blurEffectStyle, !UIAccessibility.isReduceTransparencyEnabled {
  39. let blurEffect = UIBlurEffect(style: blurEffectStyle)
  40. vibrancyView.effect = UIVibrancyEffect(blurEffect: blurEffect)
  41. if selectedBackgroundView != nil && selectedBackgroundView != vibrancySelectedBackgroundView {
  42. vibrancySelectedBackgroundView.contentView.addSubview(selectedBackgroundView!)
  43. selectedBackgroundView = vibrancySelectedBackgroundView
  44. }
  45. } else {
  46. vibrancyView.effect = nil
  47. selectedBackgroundView = defaultSelectedBackgroundView
  48. }
  49. }
  50. }