BlockButton.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // BlockButton.swift
  3. //
  4. //
  5. // Created by Cem Olcay on 12/08/15.
  6. //
  7. //
  8. #if os(iOS) || os(tvOS)
  9. import UIKit
  10. public typealias BlockButtonAction = (_ sender: BlockButton) -> Void
  11. ///Make sure you use "[weak self] (sender) in" if you are using the keyword self inside the closure or there might be a memory leak
  12. open class BlockButton: UIButton {
  13. // MARK: Propeties
  14. open var highlightLayer: CALayer?
  15. open var action: BlockButtonAction?
  16. // MARK: Init
  17. public init() {
  18. super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
  19. defaultInit()
  20. }
  21. public init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, action: BlockButtonAction?) {
  22. super.init (frame: CGRect(x: x, y: y, width: w, height: h))
  23. self.action = action
  24. defaultInit()
  25. }
  26. public init(action: @escaping BlockButtonAction) {
  27. super.init(frame: CGRect.zero)
  28. self.action = action
  29. defaultInit()
  30. }
  31. public override init(frame: CGRect) {
  32. super.init(frame: frame)
  33. defaultInit()
  34. }
  35. public init(frame: CGRect, action: @escaping BlockButtonAction) {
  36. super.init(frame: frame)
  37. self.action = action
  38. defaultInit()
  39. }
  40. public required init?(coder aDecoder: NSCoder) {
  41. super.init(coder: aDecoder)
  42. defaultInit()
  43. }
  44. private func defaultInit() {
  45. addTarget(self, action: #selector(BlockButton.didPressed(_:)), for: UIControl.Event.touchUpInside)
  46. addTarget(self, action: #selector(BlockButton.highlight), for: [UIControl.Event.touchDown, UIControl.Event.touchDragEnter])
  47. addTarget(self, action: #selector(BlockButton.unhighlight), for: [
  48. UIControl.Event.touchUpInside,
  49. UIControl.Event.touchUpOutside,
  50. UIControl.Event.touchCancel,
  51. UIControl.Event.touchDragExit
  52. ])
  53. setTitleColor(UIColor.black, for: UIControl.State.normal)
  54. setTitleColor(UIColor.blue, for: UIControl.State.selected)
  55. }
  56. open func addAction(_ action: @escaping BlockButtonAction) {
  57. self.action = action
  58. }
  59. // MARK: Action
  60. @objc open func didPressed(_ sender: BlockButton) {
  61. action?(sender)
  62. }
  63. // MARK: Highlight
  64. @objc open func highlight() {
  65. if action == nil {
  66. return
  67. }
  68. let highlightLayer = CALayer()
  69. highlightLayer.frame = layer.bounds
  70. highlightLayer.backgroundColor = UIColor.black.cgColor
  71. highlightLayer.opacity = 0.5
  72. var maskImage: UIImage? = nil
  73. UIGraphicsBeginImageContextWithOptions(layer.bounds.size, false, 0)
  74. if let context = UIGraphicsGetCurrentContext() {
  75. layer.render(in: context)
  76. maskImage = UIGraphicsGetImageFromCurrentImageContext()
  77. }
  78. UIGraphicsEndImageContext()
  79. let maskLayer = CALayer()
  80. maskLayer.contents = maskImage?.cgImage
  81. maskLayer.frame = highlightLayer.frame
  82. highlightLayer.mask = maskLayer
  83. layer.addSublayer(highlightLayer)
  84. self.highlightLayer = highlightLayer
  85. }
  86. @objc open func unhighlight() {
  87. if action == nil {
  88. return
  89. }
  90. highlightLayer?.removeFromSuperlayer()
  91. highlightLayer = nil
  92. }
  93. }
  94. #endif