UIView+Extension.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // UIView+Extension.swift
  3. //
  4. //
  5. // Created by LXF on 16/3/29.
  6. // Copyright © 2016年 LXF. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension UIView {
  11. //MARK: - storyboard上添加属性 可以直接配置 省得每次都要代码实现
  12. // border 宽度
  13. @IBInspectable var borderWidth: CGFloat {
  14. get {
  15. return self.layer.borderWidth
  16. }
  17. set {
  18. self.layer.borderWidth = newValue
  19. }
  20. }
  21. // border的颜色
  22. @IBInspectable var borderColor: UIColor {
  23. get {
  24. return UIColor(cgColor: self.layer.borderColor!)
  25. }
  26. set {
  27. self.layer.borderColor = newValue.cgColor
  28. }
  29. }
  30. // 圆角
  31. @IBInspectable var cornerRadius: CGFloat {
  32. get {
  33. return self.layer.cornerRadius
  34. }
  35. set {
  36. self.layer.cornerRadius = newValue
  37. self.layer.masksToBounds = newValue > 0
  38. }
  39. }
  40. func setAnchorConstraintsFullSizeTo(view: UIView, padding: CGFloat = 0) {
  41. self.translatesAutoresizingMaskIntoConstraints = false
  42. self.topAnchor.constraint(equalTo: view.topAnchor, constant: padding).isActive = true
  43. self.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -padding).isActive = true
  44. self.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding).isActive = true
  45. self.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding).isActive = true
  46. }
  47. func createLineView(_ frame:CGRect,_ color:UIColor) -> UIView {
  48. self.frame = frame
  49. self.backgroundColor = color
  50. return self
  51. }
  52. /// 裁剪 view 的圆角
  53. func clipRectCorner(direction: UIRectCorner, cornerRadius: CGFloat) {
  54. let cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
  55. let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: direction, cornerRadii: cornerSize)
  56. let maskLayer = CAShapeLayer()
  57. maskLayer.frame = bounds
  58. maskLayer.path = maskPath.cgPath
  59. layer.addSublayer(maskLayer)
  60. layer.mask = maskLayer
  61. }
  62. public var x: CGFloat{
  63. get{
  64. return self.frame.origin.x
  65. }
  66. set{
  67. var r = self.frame
  68. r.origin.x = newValue
  69. self.frame = r
  70. }
  71. }
  72. public var y: CGFloat{
  73. get{
  74. return self.frame.origin.y
  75. }
  76. set{
  77. var r = self.frame
  78. r.origin.y = newValue
  79. self.frame = r
  80. }
  81. }
  82. /// 右边界的x值
  83. public var rightX: CGFloat{
  84. get{
  85. return self.x + self.width
  86. }
  87. set{
  88. var r = self.frame
  89. r.origin.x = newValue - frame.size.width
  90. self.frame = r
  91. }
  92. }
  93. /// 下边界的y值
  94. public var bottomY: CGFloat{
  95. get{
  96. return self.y + self.height
  97. }
  98. set{
  99. var r = self.frame
  100. r.origin.y = newValue - frame.size.height
  101. self.frame = r
  102. }
  103. }
  104. public var centerX : CGFloat{
  105. get{
  106. return self.center.x
  107. }
  108. set{
  109. self.center = CGPoint(x: newValue, y: self.center.y)
  110. }
  111. }
  112. public var centerY : CGFloat{
  113. get{
  114. return self.center.y
  115. }
  116. set{
  117. self.center = CGPoint(x: self.center.x, y: newValue)
  118. }
  119. }
  120. // MARK: - UIView 圆角
  121. /// 切圆角
  122. ///
  123. /// - Parameter cornerRadius: 圆角半径
  124. func roundedCorners(cornerRadius: CGFloat) {
  125. roundedCorners(cornerRadius: cornerRadius, borderWidth: 0, borderColor: nil)
  126. }
  127. /// 圆角边框设置
  128. ///
  129. /// - Parameters:
  130. /// - cornerRadius: 圆角半径
  131. /// - borderWidth: 边款宽度
  132. /// - borderColor: 边款颜色
  133. func roundedCorners(cornerRadius: CGFloat?, borderWidth: CGFloat?, borderColor: UIColor?) {
  134. self.layer.cornerRadius = cornerRadius!
  135. self.layer.borderWidth = borderWidth!
  136. self.layer.borderColor = borderColor?.cgColor
  137. self.layer.masksToBounds = true
  138. }
  139. /// 设置指定角的圆角
  140. ///
  141. /// - Parameters:
  142. /// - cornerRadius: 圆角半径
  143. /// - rectCorner: 指定切圆角的角
  144. func roundedCorners(cornerRadius: CGFloat?, rectCorner: UIRectCorner?) {
  145. let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: rectCorner!, cornerRadii: CGSize(width: cornerRadius!, height: cornerRadius!))
  146. let layer = CAShapeLayer()
  147. layer.frame = self.bounds
  148. layer.path = path.cgPath
  149. self.layer.mask = layer
  150. }
  151. /// EZSwiftExtensions
  152. public func setCornerRadius(radius: CGFloat) {
  153. self.layer.cornerRadius = radius
  154. self.layer.masksToBounds = true
  155. }
  156. //画线
  157. private func drawBorder(rect:CGRect,color:UIColor){
  158. let line = UIBezierPath(rect: rect)
  159. let lineShape = CAShapeLayer()
  160. lineShape.path = line.cgPath
  161. lineShape.fillColor = color.cgColor
  162. self.layer.addSublayer(lineShape)
  163. }
  164. //设置右边框
  165. public func rightBorder(width:CGFloat,borderColor:UIColor){
  166. let rect = CGRect(x: 0, y: self.frame.size.width - width, width: width, height: self.frame.size.height)
  167. drawBorder(rect: rect, color: borderColor)
  168. }
  169. //设置左边框
  170. public func leftBorder(width:CGFloat,borderColor:UIColor){
  171. let rect = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
  172. drawBorder(rect: rect, color: borderColor)
  173. }
  174. //设置上边框
  175. public func topBorder(width:CGFloat,borderColor:UIColor){
  176. let rect = CGRect(x: 0, y: 0, width: self.frame.size.width, height: width)
  177. drawBorder(rect: rect, color: borderColor)
  178. }
  179. //设置底边框
  180. public func buttomBorder(width:CGFloat,borderColor:UIColor){
  181. let rect = CGRect(x: 0, y: self.frame.size.height-width, width: self.frame.size.width, height: width)
  182. drawBorder(rect: rect, color: borderColor)
  183. }
  184. }
  185. // MARK: Gesture Extensions
  186. extension UIView {
  187. /// EZSwiftExtensions
  188. public func addShadow(offset: CGSize, radius: CGFloat, color: UIColor, opacity: Float, cornerRadius: CGFloat? = nil) {
  189. self.layer.shadowOffset = offset
  190. self.layer.shadowRadius = radius
  191. self.layer.shadowOpacity = opacity
  192. self.layer.shadowColor = color.cgColor
  193. if let r = cornerRadius {
  194. self.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: r).cgPath
  195. }
  196. }
  197. /// http://stackoverflow.com/questions/4660371/how-to-add-a-touch-event-to-a-uiview/32182866#32182866
  198. /// EZSwiftExtensions
  199. public func addTapGesture(tapNumber: Int = 1, target: AnyObject, action: Selector) {
  200. let tap = UITapGestureRecognizer(target: target, action: action)
  201. tap.numberOfTapsRequired = tapNumber
  202. addGestureRecognizer(tap)
  203. isUserInteractionEnabled = true
  204. }
  205. /// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
  206. public func addTapGesture(tapNumber: Int = 1, action: ((UITapGestureRecognizer) -> Void)?) {
  207. let tap = BlockTap(tapCount: tapNumber, fingerCount: 1, action: action)
  208. addGestureRecognizer(tap)
  209. isUserInteractionEnabled = true
  210. }
  211. /// EZSwiftExtensions
  212. public func addSwipeGesture(direction: UISwipeGestureRecognizer.Direction, numberOfTouches: Int = 1, target: AnyObject, action: Selector) {
  213. let swipe = UISwipeGestureRecognizer(target: target, action: action)
  214. swipe.direction = direction
  215. #if os(iOS)
  216. swipe.numberOfTouchesRequired = numberOfTouches
  217. #endif
  218. addGestureRecognizer(swipe)
  219. isUserInteractionEnabled = true
  220. }
  221. /// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
  222. public func addSwipeGesture(direction: UISwipeGestureRecognizer.Direction, numberOfTouches: Int = 1, action: ((UISwipeGestureRecognizer) -> Void)?) {
  223. let swipe = BlockSwipe(direction: direction, fingerCount: numberOfTouches, action: action)
  224. addGestureRecognizer(swipe)
  225. isUserInteractionEnabled = true
  226. }
  227. /// EZSwiftExtensions
  228. public func addPanGesture(target: AnyObject, action: Selector) {
  229. let pan = UIPanGestureRecognizer(target: target, action: action)
  230. addGestureRecognizer(pan)
  231. isUserInteractionEnabled = true
  232. }
  233. /// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
  234. public func addPanGesture(action: ((UIPanGestureRecognizer) -> Void)?) {
  235. let pan = BlockPan(action: action)
  236. addGestureRecognizer(pan)
  237. isUserInteractionEnabled = true
  238. }
  239. public func removeSubviews() {
  240. for subview in subviews {
  241. subview.removeFromSuperview()
  242. }
  243. }
  244. }