UIView+Extension.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // UIView+Extension.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/23.
  6. //
  7. import Foundation
  8. import UIKit
  9. import Toast_Swift
  10. // MARK: - UIView 有关 Frame 的扩展
  11. public extension UIView {
  12. // MARK: 3.1、x 的位置
  13. /// x 的位置
  14. var kb_x: CGFloat {
  15. get {
  16. return frame.origin.x
  17. }
  18. set(newValue) {
  19. var tempFrame: CGRect = frame
  20. tempFrame.origin.x = newValue
  21. frame = tempFrame
  22. }
  23. }
  24. // MARK: 3.2、y 的位置
  25. /// y 的位置
  26. var kb_y: CGFloat {
  27. get {
  28. return frame.origin.y
  29. }
  30. set(newValue) {
  31. var tempFrame: CGRect = frame
  32. tempFrame.origin.y = newValue
  33. frame = tempFrame
  34. }
  35. }
  36. // MARK: 3.3、height: 视图的高度
  37. /// height: 视图的高度
  38. var kb_height: CGFloat {
  39. get {
  40. return frame.size.height
  41. }
  42. set(newValue) {
  43. var tempFrame: CGRect = frame
  44. tempFrame.size.height = newValue
  45. frame = tempFrame
  46. }
  47. }
  48. // MARK: 3.4、width: 视图的宽度
  49. /// width: 视图的宽度
  50. var kb_width: CGFloat {
  51. get {
  52. return frame.size.width
  53. }
  54. set(newValue) {
  55. var tempFrame: CGRect = frame
  56. tempFrame.size.width = newValue
  57. frame = tempFrame
  58. }
  59. }
  60. // MARK: 3.5、size: 视图的zize
  61. /// size: 视图的zize
  62. var kb_size: CGSize {
  63. get {
  64. return frame.size
  65. }
  66. set(newValue) {
  67. var tempFrame: CGRect = frame
  68. tempFrame.size = newValue
  69. frame = tempFrame
  70. }
  71. }
  72. // MARK: 3.6、centerX: 视图的X中间位置
  73. /// centerX: 视图的X中间位置
  74. var kb_centerX: CGFloat {
  75. get {
  76. return center.x
  77. }
  78. set(newValue) {
  79. var tempCenter: CGPoint = center
  80. tempCenter.x = newValue
  81. center = tempCenter
  82. }
  83. }
  84. // MARK: 3.7、centerY: 视图的Y中间位置
  85. /// centerY: 视图Y的中间位置
  86. var kb_centerY: CGFloat {
  87. get {
  88. return center.y
  89. }
  90. set(newValue) {
  91. var tempCenter: CGPoint = center
  92. tempCenter.y = newValue
  93. center = tempCenter
  94. }
  95. }
  96. // MARK: 3.9、top 上端横坐标(y)
  97. /// top 上端横坐标(y)
  98. var kb_top: CGFloat {
  99. get {
  100. return frame.origin.y
  101. }
  102. set(newValue) {
  103. var tempFrame: CGRect = frame
  104. tempFrame.origin.y = newValue
  105. frame = tempFrame
  106. }
  107. }
  108. // MARK: 3.10、left 左端横坐标(x)
  109. /// left 左端横坐标(x)
  110. var kb_left: CGFloat {
  111. get {
  112. return frame.origin.x
  113. }
  114. set(newValue) {
  115. var tempFrame: CGRect = frame
  116. tempFrame.origin.x = newValue
  117. frame = tempFrame
  118. }
  119. }
  120. // MARK: 3.11、bottom 底端纵坐标 (y + height)
  121. /// bottom 底端纵坐标 (y + height)
  122. var kb_bottom: CGFloat {
  123. get {
  124. return frame.origin.y + frame.size.height
  125. }
  126. set(newValue) {
  127. frame.origin.y = newValue - frame.size.height
  128. }
  129. }
  130. // MARK: 3.12、right 底端纵坐标 (x + width)
  131. /// right 底端纵坐标 (x + width)
  132. var kb_right: CGFloat {
  133. get {
  134. return frame.origin.x + frame.size.width
  135. }
  136. set(newValue) {
  137. frame.origin.x = newValue - frame.size.width
  138. }
  139. }
  140. // MARK: 3.13、origin 点
  141. /// origin 点
  142. var kb_origin: CGPoint {
  143. get {
  144. return frame.origin
  145. }
  146. set(newValue) {
  147. var tempOrigin: CGPoint = frame.origin
  148. tempOrigin = newValue
  149. frame.origin = tempOrigin
  150. }
  151. }
  152. }
  153. extension UIView {
  154. //添加4个不同大小的圆角
  155. func addFourCorner(topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat){
  156. let cornerRadii = UIView.CornerRadii.init(topLeft: topLeft, topRight: topRight, bottomLeft: bottomLeft, bottomRight: bottomRight)
  157. let path = createPathWithRoundedRect(bounds: self.bounds, cornerRadii:cornerRadii)
  158. let shapLayer = CAShapeLayer()
  159. shapLayer.frame = self.bounds
  160. shapLayer.path = path
  161. self.layer.mask = shapLayer
  162. }
  163. //各圆角大小
  164. struct CornerRadii {
  165. var topLeft :CGFloat = 0
  166. var topRight :CGFloat = 0
  167. var bottomLeft :CGFloat = 0
  168. var bottomRight :CGFloat = 0
  169. }
  170. //切圆角函数绘制线条
  171. func createPathWithRoundedRect (bounds:CGRect,cornerRadii:CornerRadii) -> CGPath {
  172. let minX = bounds.minX
  173. let minY = bounds.minY
  174. let maxX = bounds.maxX
  175. let maxY = bounds.maxY
  176. //获取四个圆心
  177. let topLeftCenterX = minX + cornerRadii.topLeft
  178. let topLeftCenterY = minY + cornerRadii.topLeft
  179. let topRightCenterX = maxX - cornerRadii.topRight
  180. let topRightCenterY = minY + cornerRadii.topRight
  181. let bottomLeftCenterX = minX + cornerRadii.bottomLeft
  182. let bottomLeftCenterY = maxY - cornerRadii.bottomLeft
  183. let bottomRightCenterX = maxX - cornerRadii.bottomRight
  184. let bottomRightCenterY = maxY - cornerRadii.bottomRight
  185. //虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针
  186. let path :CGMutablePath = CGMutablePath();
  187. //顶 左
  188. path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: false)
  189. //顶右
  190. path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: CGFloat.pi * 3 / 2, endAngle: 0, clockwise: false)
  191. //底右
  192. path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: false)
  193. //底左
  194. path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: false)
  195. path.closeSubpath();
  196. return path;
  197. }
  198. }
  199. // MARK: - Toast吐司🍞
  200. extension UIView {
  201. // MARK: 普通消息
  202. func toast(text: String) {
  203. var style = ToastStyle()
  204. style.messageFont = .systemFont(ofSize: 14)
  205. style.backgroundColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.85)
  206. style.cornerRadius = 8
  207. style.verticalPadding = 14
  208. style.horizontalPadding = 44
  209. self.makeToast(text, duration: 1.5, position: .center, style: style)
  210. }
  211. }