IBView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //
  2. // IBView.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/3/17.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. @IBDesignable
  10. class IBView: UIView {
  11. @IBInspectable var 圆角偏移量:CGFloat = 0.0
  12. @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0)
  13. @IBInspectable var maskToBounds:Bool = false
  14. @IBInspectable var borderColor:UIColor = UIColor.clear
  15. @IBInspectable var borderWidth:CGFloat = 0.0
  16. @IBInspectable var shadowOffset: CGSize = CGSize.zero {
  17. didSet {
  18. self.layer.shadowOffset = shadowOffset
  19. }
  20. }
  21. @IBInspectable var shadowColor: UIColor = UIColor.clear {
  22. didSet {
  23. self.layer.shadowColor = shadowColor.cgColor
  24. }
  25. }
  26. @IBInspectable var shadowRadius: CGFloat = 0 {
  27. didSet {
  28. self.layer.shadowRadius = shadowRadius
  29. }
  30. }
  31. @IBInspectable var shadowOpacity: Float = 0 {
  32. didSet {
  33. self.layer.shadowOpacity = shadowOpacity
  34. }
  35. }
  36. @IBInspectable var 渐变颜色: UIColor = UIColor.white {
  37. didSet {
  38. let layer = CAGradientLayer()
  39. layer.colors = [渐变颜色.cgColor, UIColorFromHex(rgbValue: 0xF5594B,alpha: 0.5).cgColor]
  40. layer.frame = self.bounds
  41. layer.locations = [0.1]
  42. layer.startPoint = CGPoint(x: 0, y: 0)
  43. layer.endPoint = CGPoint(x: 0, y: 1)
  44. self.layer.addSublayer(layer)
  45. }
  46. }
  47. static func refreshIBEffect(view:UIView){ /**< 刷新 IBInspectable 的效果 */
  48. guard let 圆角倍数:CGPoint = view.value(forKey:"圆角倍数") as? CGPoint,
  49. let 圆角偏移量:CGFloat = view.value(forKey:"圆角偏移量") as? CGFloat,
  50. let maskToBounds:Bool = view.value(forKey:"maskToBounds") as? Bool,
  51. let borderColor:UIColor = view.value(forKey:"borderColor") as? UIColor,
  52. let borderWidth:Double = view.value(forKey:"borderWidth") as? Double else { return }
  53. let cornerRadiusRatio: CGPoint = 圆角倍数
  54. let cornerRadiusOffset: CGFloat = 圆角偏移量
  55. view.layer.cornerRadius = cornerRadiusRatio.x * view.frame.size.width
  56. + cornerRadiusRatio.y * view.frame.size.height
  57. + cornerRadiusOffset
  58. view.layer.masksToBounds = maskToBounds
  59. view.layer.borderColor = borderColor.cgColor
  60. view.layer.borderWidth = CGFloat(borderWidth)
  61. view.layer.sublayers?.forEach({
  62. if $0.isMember(of: CAGradientLayer.self) {
  63. $0.frame = view.frame
  64. }
  65. })
  66. }
  67. override func layoutSubviews() {
  68. super.layoutSubviews()
  69. IBView.refreshIBEffect(view: self)
  70. }
  71. }
  72. @IBDesignable
  73. class IBLabel:UILabel{
  74. @IBInspectable var 圆角偏移量:CGFloat = 0.0
  75. @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0)
  76. @IBInspectable var maskToBounds:Bool = false
  77. @IBInspectable var borderColor:UIColor = UIColor.clear
  78. @IBInspectable var borderWidth:CGFloat = 0.0
  79. private var padding = UIEdgeInsets.zero
  80. @IBInspectable var paddingLeft: CGFloat {
  81. get { return padding.left }
  82. set { padding.left = newValue }
  83. }
  84. @IBInspectable var paddingRight: CGFloat {
  85. get { return padding.right }
  86. set { padding.right = newValue }
  87. }
  88. @IBInspectable var paddingTop: CGFloat {
  89. get { return padding.top }
  90. set { padding.top = newValue }
  91. }
  92. @IBInspectable var paddingBottom: CGFloat {
  93. get { return padding.bottom }
  94. set { padding.bottom = newValue }
  95. }
  96. override func drawText(in rect: CGRect) {
  97. super.drawText(in: (CGRect.inset(rect))(by: padding)) //UIEdgeInsetsInsetRect(rect, padding))
  98. }
  99. override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
  100. let insets = self.padding
  101. var rect = super.textRect(forBounds: (CGRect.inset(bounds))(by: insets), limitedToNumberOfLines: numberOfLines)
  102. rect.origin.x -= insets.left
  103. rect.origin.y -= insets.top
  104. rect.size.width += (insets.left + insets.right)
  105. rect.size.height += (insets.top + insets.bottom)
  106. return rect
  107. }
  108. override func layoutSubviews() {
  109. super.layoutSubviews()
  110. IBView.refreshIBEffect(view: self)
  111. }
  112. }
  113. @IBDesignable
  114. class IBImageView: UIImageView {
  115. @IBInspectable var 圆角偏移量:CGFloat = 0.0
  116. @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0)
  117. @IBInspectable var maskToBounds:Bool = false
  118. @IBInspectable var borderColor:UIColor = UIColor.clear
  119. @IBInspectable var borderWidth:CGFloat = 0.0
  120. override func layoutSubviews() {
  121. super.layoutSubviews()
  122. IBView.refreshIBEffect(view: self)
  123. }
  124. }
  125. fileprivate var backGroundColorString = "backGroundColorString"
  126. @IBDesignable
  127. extension UIView {
  128. var x: CGFloat {
  129. get { return frame.origin.x }
  130. set(newValue) {
  131. var tmpFrame : CGRect = frame
  132. tmpFrame.origin.x = newValue
  133. frame = tmpFrame
  134. }
  135. }
  136. var y: CGFloat {
  137. get { return frame.origin.y }
  138. set(newValue) {
  139. var tmpFrame : CGRect = frame
  140. tmpFrame.origin.y = newValue
  141. frame = tmpFrame
  142. }
  143. }
  144. var height: CGFloat {
  145. get { return frame.size.height }
  146. set(newValue) {
  147. var tmpFrame : CGRect = frame
  148. tmpFrame.size.height = newValue
  149. frame = tmpFrame
  150. }
  151. }
  152. var width: CGFloat {
  153. get { return frame.size.width }
  154. set(newValue) {
  155. var tmpFrame: CGRect = frame
  156. tmpFrame.size.width = newValue
  157. frame = tmpFrame
  158. }
  159. }
  160. var left: CGFloat {
  161. get { return x }
  162. set(newValue) { x = newValue }
  163. }
  164. var right: CGFloat {
  165. get { return x + width }
  166. set(newVal) { x = newVal - width }
  167. }
  168. var top: CGFloat {
  169. get { return y }
  170. set(newVal) { y = newVal }
  171. }
  172. var bottom: CGFloat {
  173. get { return y + height }
  174. set(newVal) { y = newVal - height }
  175. }
  176. var centerX: CGFloat {
  177. get { return center.x }
  178. set(newVal) { center = CGPoint(x: newVal, y: center.y) }
  179. }
  180. var centerY: CGFloat {
  181. get { return center.y }
  182. set(newVal) { center = CGPoint(x: center.x, y: newVal) }
  183. }
  184. var middleX: CGFloat {
  185. get { return width / 2 }
  186. }
  187. var middleY: CGFloat {
  188. get { return height / 2 }
  189. }
  190. var middlePoint: CGPoint {
  191. get { return CGPoint(x: middleX, y: middleY) }
  192. }
  193. /// SlideSegmentView调用
  194. var bx : CGFloat { return self.x + self.width }
  195. /// 是否参与颜色修改
  196. @IBInspectable var colorFlag: Bool {
  197. get {
  198. return objc_getAssociatedObject(self, &backGroundColorString) as! Bool
  199. }
  200. set {
  201. objc_setAssociatedObject(self, &backGroundColorString, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  202. }
  203. }
  204. }
  205. fileprivate var backGroundColorAnim = "backGroundColorAnim"
  206. fileprivate var backGroundColorAnimColor = "backGroundColorAnimColor"
  207. extension UIView {
  208. @IBInspectable var showTapAnim: Bool {
  209. get {
  210. return objc_getAssociatedObject(self, &backGroundColorAnim) as! Bool
  211. }
  212. set {
  213. objc_setAssociatedObject(self, &backGroundColorAnim, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  214. if newValue {
  215. allCanTap(sender: self)
  216. let tap = UITapGestureRecognizer(target: self, action: #selector(tapAnimation))
  217. self.addGestureRecognizer(tap)
  218. }
  219. }
  220. }
  221. @IBInspectable var tapAnimColor: UIColor {
  222. get {
  223. return objc_getAssociatedObject(self, &backGroundColorAnimColor) as! UIColor
  224. }
  225. set {
  226. objc_setAssociatedObject(self, &backGroundColorAnimColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  227. }
  228. }
  229. /// 设置该view所有的子级view都可以点击
  230. ///
  231. /// - Parameter sender: 需要设置的view
  232. private func allCanTap(sender: UIView) {
  233. if let sender = sender as? UIStackView {
  234. sender.arrangedSubviews.forEach({
  235. $0.isUserInteractionEnabled = true
  236. allCanTap(sender: $0)
  237. })
  238. } else {
  239. sender.subviews.forEach({
  240. $0.isUserInteractionEnabled = true
  241. allCanTap(sender: $0)
  242. })
  243. }
  244. }
  245. @objc private func tapAnimation() {
  246. let tmpView = UIView(frame: self.bounds)
  247. tmpView.backgroundColor = tapAnimColor.withAlphaComponent(0.3)
  248. tmpView.alpha = 0
  249. self.addSubview(tmpView)
  250. UIView.animate(withDuration: 0.20, delay: 0, options: .curveEaseIn, animations: {
  251. tmpView.alpha = 0.8
  252. }) { (finished) in
  253. UIView.animate(withDuration: 0.20, delay: 0.1, options: .curveEaseOut, animations: {
  254. tmpView.alpha = 0
  255. }, completion: { (finished) in
  256. tmpView.removeFromSuperview()
  257. })
  258. }
  259. }
  260. }
  261. extension UIColor {
  262. /**
  263. Create a ligher color
  264. */
  265. func lighter(by percentage: CGFloat = 30.0) -> UIColor {
  266. return self.adjustBrightness(by: abs(percentage))
  267. }
  268. /**
  269. Create a darker color
  270. */
  271. func darker(by percentage: CGFloat = 30.0) -> UIColor {
  272. return self.adjustBrightness(by: -abs(percentage))
  273. }
  274. /**
  275. Try to increase brightness or decrease saturation
  276. */
  277. func adjustBrightness(by percentage: CGFloat = 30.0) -> UIColor {
  278. var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
  279. if self.getHue(&h, saturation: &s, brightness: &b, alpha: &a) {
  280. if b < 1.0 {
  281. let newB: CGFloat = max(min(b + (percentage/100.0)*b, 1.0), 0,0)
  282. return UIColor(hue: h, saturation: s, brightness: newB, alpha: a)
  283. } else {
  284. let newS: CGFloat = min(max(s - (percentage/100.0)*s, 0.0), 1.0)
  285. return UIColor(hue: h, saturation: newS, brightness: b, alpha: a)
  286. }
  287. }
  288. return self
  289. }
  290. }