PPCheckedViewCell.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is based on third party code, see below for the original author
  2. // and original license.
  3. // Modifications are (c) by Threema GmbH and licensed under the AGPLv3.
  4. // Copyright (c) 2016 Pavel Pantus <pantusp@gmail.com>
  5. // See Resources/License.html for original license
  6. import UIKit
  7. import ThreemaFramework
  8. /**
  9. Protocol that provides default implementation for `reuseIdentifier` method.
  10. */
  11. protocol PPReusableView: class {
  12. static var reuseIdentifier: String { get }
  13. }
  14. extension PPReusableView {
  15. static var reuseIdentifier: String {
  16. return NSStringFromClass(self)
  17. }
  18. }
  19. /**
  20. Cell with a checkmark in the bottom right corner.
  21. */
  22. class PPCheckedViewCell: UICollectionViewCell {
  23. public let checked = UIImageView(image: StyleKit.check.withRenderingMode(.alwaysOriginal))
  24. public let unchecked = UIImageView(image: StyleKit.uncheck.withRenderingMode(.alwaysOriginal))
  25. func setupCheckmark() {
  26. checked.isHidden = true
  27. unchecked.isHidden = true
  28. checked.frame = CGRect.init(x: 0.0, y: 0.0, width: 22.0, height: 22.0)
  29. unchecked.frame = CGRect.init(x: 0.0, y: 0.0, width: 22.0, height: 22.0)
  30. checked.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin]
  31. unchecked.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin]
  32. addSubview(checked)
  33. addSubview(unchecked)
  34. }
  35. override func layoutSubviews() {
  36. super.layoutSubviews()
  37. let height = checked.frame.height
  38. let width = checked.frame.width
  39. if (backgroundView != nil) {
  40. checked.frame = CGRect(x: backgroundView!.frame.width - (1.4 * width),
  41. y: backgroundView!.frame.height - (1.4 * height),
  42. width: width,
  43. height: height)
  44. unchecked.frame = CGRect(x: backgroundView!.frame.width - (1.4 * width),
  45. y: backgroundView!.frame.height - (1.4 * height),
  46. width: width,
  47. height: height)
  48. /***** BEGIN THREEMA MODIFICATION: accessibilityIgnoresInvertColors *********/
  49. if #available(iOS 11.0, *) {
  50. self.accessibilityIgnoresInvertColors = true
  51. }
  52. /***** END THREEMA MODIFICATION: accessibilityIgnoresInvertColors *********/
  53. }
  54. }
  55. public func set(selected: Bool) {
  56. checked.isHidden = !selected
  57. unchecked.isHidden = selected
  58. }
  59. }
  60. extension PPCheckedViewCell: PPReusableView {}