| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- //
- // UIView+Extension.swift
- // AiKeyboard
- //
- // Created by Destiny on 2025/4/23.
- //
- import Foundation
- import UIKit
- import Toast_Swift
- // MARK: - UIView 有关 Frame 的扩展
- public extension UIView {
- // MARK: 3.1、x 的位置
- /// x 的位置
- var kb_x: CGFloat {
- get {
- return frame.origin.x
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.origin.x = newValue
- frame = tempFrame
- }
- }
- // MARK: 3.2、y 的位置
- /// y 的位置
- var kb_y: CGFloat {
- get {
- return frame.origin.y
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.origin.y = newValue
- frame = tempFrame
- }
- }
-
- // MARK: 3.3、height: 视图的高度
- /// height: 视图的高度
- var kb_height: CGFloat {
- get {
- return frame.size.height
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.size.height = newValue
- frame = tempFrame
- }
- }
-
- // MARK: 3.4、width: 视图的宽度
- /// width: 视图的宽度
- var kb_width: CGFloat {
- get {
- return frame.size.width
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.size.width = newValue
- frame = tempFrame
- }
- }
-
- // MARK: 3.5、size: 视图的zize
- /// size: 视图的zize
- var kb_size: CGSize {
- get {
- return frame.size
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.size = newValue
- frame = tempFrame
- }
- }
-
- // MARK: 3.6、centerX: 视图的X中间位置
- /// centerX: 视图的X中间位置
- var kb_centerX: CGFloat {
- get {
- return center.x
- }
- set(newValue) {
- var tempCenter: CGPoint = center
- tempCenter.x = newValue
- center = tempCenter
- }
- }
-
- // MARK: 3.7、centerY: 视图的Y中间位置
- /// centerY: 视图Y的中间位置
- var kb_centerY: CGFloat {
- get {
- return center.y
- }
- set(newValue) {
- var tempCenter: CGPoint = center
- tempCenter.y = newValue
- center = tempCenter
- }
- }
-
- // MARK: 3.9、top 上端横坐标(y)
- /// top 上端横坐标(y)
- var kb_top: CGFloat {
- get {
- return frame.origin.y
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.origin.y = newValue
- frame = tempFrame
- }
- }
-
- // MARK: 3.10、left 左端横坐标(x)
- /// left 左端横坐标(x)
- var kb_left: CGFloat {
- get {
- return frame.origin.x
- }
- set(newValue) {
- var tempFrame: CGRect = frame
- tempFrame.origin.x = newValue
- frame = tempFrame
- }
- }
-
- // MARK: 3.11、bottom 底端纵坐标 (y + height)
- /// bottom 底端纵坐标 (y + height)
- var kb_bottom: CGFloat {
- get {
- return frame.origin.y + frame.size.height
- }
- set(newValue) {
- frame.origin.y = newValue - frame.size.height
- }
- }
-
- // MARK: 3.12、right 底端纵坐标 (x + width)
- /// right 底端纵坐标 (x + width)
- var kb_right: CGFloat {
- get {
- return frame.origin.x + frame.size.width
- }
- set(newValue) {
- frame.origin.x = newValue - frame.size.width
- }
- }
-
- // MARK: 3.13、origin 点
- /// origin 点
- var kb_origin: CGPoint {
- get {
- return frame.origin
- }
- set(newValue) {
- var tempOrigin: CGPoint = frame.origin
- tempOrigin = newValue
- frame.origin = tempOrigin
- }
- }
- }
- extension UIView {
-
- //添加4个不同大小的圆角
- func addFourCorner(topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat){
- let cornerRadii = UIView.CornerRadii.init(topLeft: topLeft, topRight: topRight, bottomLeft: bottomLeft, bottomRight: bottomRight)
- let path = createPathWithRoundedRect(bounds: self.bounds, cornerRadii:cornerRadii)
- let shapLayer = CAShapeLayer()
- shapLayer.frame = self.bounds
- shapLayer.path = path
- self.layer.mask = shapLayer
- }
-
- //各圆角大小
- struct CornerRadii {
- var topLeft :CGFloat = 0
- var topRight :CGFloat = 0
- var bottomLeft :CGFloat = 0
- var bottomRight :CGFloat = 0
- }
-
- //切圆角函数绘制线条
- func createPathWithRoundedRect (bounds:CGRect,cornerRadii:CornerRadii) -> CGPath {
- let minX = bounds.minX
- let minY = bounds.minY
- let maxX = bounds.maxX
- let maxY = bounds.maxY
-
- //获取四个圆心
- let topLeftCenterX = minX + cornerRadii.topLeft
- let topLeftCenterY = minY + cornerRadii.topLeft
-
- let topRightCenterX = maxX - cornerRadii.topRight
- let topRightCenterY = minY + cornerRadii.topRight
-
- let bottomLeftCenterX = minX + cornerRadii.bottomLeft
- let bottomLeftCenterY = maxY - cornerRadii.bottomLeft
-
- let bottomRightCenterX = maxX - cornerRadii.bottomRight
- let bottomRightCenterY = maxY - cornerRadii.bottomRight
-
- //虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针
- let path :CGMutablePath = CGMutablePath();
- //顶 左
- path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: false)
- //顶右
- path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: CGFloat.pi * 3 / 2, endAngle: 0, clockwise: false)
- //底右
- path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: false)
- //底左
- path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: false)
- path.closeSubpath();
- return path;
- }
- }
- // MARK: - Toast吐司🍞
- extension UIView {
-
- // MARK: 普通消息
- func toast(text: String) {
-
- var style = ToastStyle()
- style.messageFont = .systemFont(ofSize: 14)
- style.backgroundColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.85)
- style.cornerRadius = 8
- style.verticalPadding = 14
- style.horizontalPadding = 44
- self.makeToast(text, duration: 1.5, position: .center, style: style)
- }
- }
|