ExtensionUiView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import Foundation
  2. import UIKit
  3. extension UIView {
  4. @IBInspectable
  5. var cornerRadius: CGFloat {
  6. get {
  7. return layer.cornerRadius
  8. }
  9. set {
  10. layer.cornerRadius = newValue
  11. }
  12. }
  13. @IBInspectable
  14. var borderWidth: CGFloat {
  15. get {
  16. return layer.borderWidth
  17. }
  18. set {
  19. layer.borderWidth = newValue
  20. }
  21. }
  22. @IBInspectable
  23. var borderColor: UIColor? {
  24. get {
  25. if let color = layer.borderColor {
  26. return UIColor(cgColor: color)
  27. }
  28. return nil
  29. }
  30. set {
  31. if let color = newValue {
  32. layer.borderColor = color.cgColor
  33. } else {
  34. layer.borderColor = nil
  35. }
  36. }
  37. }
  38. @IBInspectable
  39. var shadowRadius: CGFloat {
  40. get {
  41. return layer.shadowRadius
  42. }
  43. set {
  44. layer.shadowRadius = newValue
  45. }
  46. }
  47. @IBInspectable
  48. var shadowOpacity: Float {
  49. get {
  50. return layer.shadowOpacity
  51. }
  52. set {
  53. layer.shadowOpacity = newValue
  54. }
  55. }
  56. @IBInspectable
  57. var shadowOffset: CGSize {
  58. get {
  59. return layer.shadowOffset
  60. }
  61. set {
  62. layer.shadowOffset = newValue
  63. }
  64. }
  65. @IBInspectable
  66. var shadowColor: UIColor? {
  67. get {
  68. if let color = layer.shadowColor {
  69. return UIColor(cgColor: color)
  70. }
  71. return nil
  72. }
  73. set {
  74. if let color = newValue {
  75. layer.shadowColor = color.cgColor
  76. } else {
  77. layer.shadowColor = nil
  78. }
  79. }
  80. }
  81. @IBInspectable
  82. var bothTopBorderMask:Bool {
  83. get{
  84. return self.bothTopBorderMask
  85. }
  86. set{
  87. if newValue{
  88. if #available(iOS 11.0, *) {
  89. self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
  90. } else {
  91. // Fallback on earlier versions
  92. }
  93. }
  94. }
  95. }
  96. @IBInspectable
  97. var bothTopLeftBottomRightBorderMask:Bool {
  98. get{
  99. return bothTopBorderMask
  100. }
  101. set{
  102. if newValue{
  103. if #available(iOS 11.0, *) {
  104. self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]
  105. } else {
  106. // Fallback on earlier versions
  107. }
  108. }
  109. }
  110. }
  111. func copyView() -> AnyObject{
  112. return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self))! as AnyObject
  113. }
  114. func clearConstraints() {
  115. for subview in self.subviews {
  116. subview.clearConstraints()
  117. }
  118. self.removeConstraints(self.constraints)
  119. }
  120. func addDashedLine(color: UIColor = UIColor(red: 207/255.0, green: 207/255.0, blue: 207/255.0, alpha: 0.5)) {
  121. let _ = layer.sublayers?.filter({ $0.name == "DashedTopLine" }).map({ $0.removeFromSuperlayer() })
  122. self.backgroundColor = UIColor.clear
  123. let cgColor = color.cgColor
  124. let shapeLayer: CAShapeLayer = CAShapeLayer()
  125. let shapeRect = CGRect(x: 0, y: 0, width: 0, height: 0)
  126. shapeLayer.name = "DashedTopLine"
  127. shapeLayer.bounds = shapeRect
  128. shapeLayer.position = CGPoint(x: 0, y: 0)
  129. shapeLayer.fillColor = UIColor.clear.cgColor
  130. shapeLayer.strokeColor = cgColor
  131. shapeLayer.lineWidth = 2
  132. shapeLayer.lineJoin = CAShapeLayerLineJoin.round
  133. shapeLayer.lineDashPattern = [2, 2]
  134. let path: CGMutablePath = CGMutablePath()
  135. path.move(to: CGPoint(x: 0, y: 0))
  136. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  137. shapeLayer.path = path
  138. self.layer.addSublayer(shapeLayer)
  139. }
  140. }
  141. extension UIView {
  142. // In order to create computed properties for extensions, we need a key to
  143. // store and access the stored property
  144. fileprivate struct AssociatedObjectKeys {
  145. static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer"
  146. static var longPressGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer2"
  147. }
  148. fileprivate typealias Action = (() -> Void)?
  149. // Set our computed property type to a closure
  150. fileprivate var tapGestureRecognizerAction: Action? {
  151. set {
  152. if let newValue = newValue {
  153. // Computed properties get stored as associated objects
  154. objc_setAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
  155. }
  156. }
  157. get {
  158. let tapGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer) as? Action
  159. return tapGestureRecognizerActionInstance
  160. }
  161. }
  162. // This is the meat of the sauce, here we create the tap gesture recognizer and
  163. // store the closure the user passed to us in the associated object we declared above
  164. public func addTapGestureRecognizer(action: (() -> Void)?) {
  165. self.isUserInteractionEnabled = true
  166. self.tapGestureRecognizerAction = action
  167. let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
  168. self.addGestureRecognizer(tapGestureRecognizer)
  169. }
  170. // Every time the user taps on the UIImageView, this function gets called,
  171. // which triggers the closure we stored
  172. @objc fileprivate func handleTapGesture(sender: UITapGestureRecognizer) {
  173. if let action = self.tapGestureRecognizerAction {
  174. action?()
  175. } else {
  176. print("no action")
  177. }
  178. }
  179. }
  180. extension UIView {
  181. // Set our computed property type to a closure
  182. fileprivate var longPressGestureRecognizerAction: Action? {
  183. set {
  184. if let newValue = newValue {
  185. // Computed properties get stored as associated objects
  186. objc_setAssociatedObject(self, &AssociatedObjectKeys.longPressGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
  187. }
  188. }
  189. get {
  190. let longPressGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.longPressGestureRecognizer) as? Action
  191. return longPressGestureRecognizerActionInstance
  192. }
  193. }
  194. // This is the meat of the sauce, here we create the tap gesture recognizer and
  195. // store the closure the user passed to us in the associated object we declared above
  196. public func addLongPressGestureRecognizer(action: (() -> Void)?) {
  197. self.isUserInteractionEnabled = true
  198. self.longPressGestureRecognizerAction = action
  199. let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleTapGesture2))
  200. self.addGestureRecognizer(longPressGestureRecognizer)
  201. }
  202. // Every time the user taps on the UIImageView, this function gets called,
  203. // which triggers the closure we stored
  204. @objc fileprivate func handleTapGesture2(sender: UILongPressGestureRecognizer) {
  205. if let action = self.longPressGestureRecognizerAction,
  206. sender.state == .began{
  207. action?()
  208. } else {
  209. print("no action")
  210. }
  211. }
  212. }
  213. extension UIView{
  214. //Get Parent View Controller from any view
  215. func parentViewController() -> UIViewController {
  216. var responder: UIResponder? = self
  217. while !(responder is UIViewController) {
  218. responder = responder?.next
  219. if nil == responder {
  220. break
  221. }
  222. }
  223. return (responder as? UIViewController)!
  224. }
  225. }