提交
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// BaseControlButton.swift
|
||||
// TUICallKit
|
||||
//
|
||||
// Created by vincepzhang on 2023/2/14.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import SnapKit
|
||||
|
||||
typealias ButtonActionBlock = (_ sender: UIButton) -> Void
|
||||
|
||||
class BaseControlButton: UIView {
|
||||
|
||||
var buttonActionBlock: ButtonActionBlock?
|
||||
var imageSize: CGSize
|
||||
|
||||
let titleLabel: UILabel = {
|
||||
let titleLabel = UILabel(frame: CGRect.zero)
|
||||
titleLabel.font = UIFont.systemFont(ofSize: 12.0)
|
||||
titleLabel.textAlignment = .center
|
||||
titleLabel.numberOfLines = 2
|
||||
titleLabel.lineBreakMode = .byTruncatingTail
|
||||
return titleLabel
|
||||
}()
|
||||
|
||||
let button: UIButton = {
|
||||
let button = UIButton(type: .system)
|
||||
return button
|
||||
}()
|
||||
|
||||
static func create(frame: CGRect, title: String, imageSize: CGSize, buttonAction: @escaping ButtonActionBlock) -> BaseControlButton {
|
||||
let controlButton = BaseControlButton(frame: frame, imageSize: imageSize)
|
||||
controlButton.titleLabel.text = title
|
||||
controlButton.buttonActionBlock = buttonAction
|
||||
return controlButton
|
||||
}
|
||||
|
||||
init(frame: CGRect, imageSize: CGSize) {
|
||||
self.imageSize = imageSize
|
||||
super.init(frame: frame)
|
||||
backgroundColor = UIColor.clear
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
// MARK: UI Specification Processing
|
||||
private var isViewReady: Bool = false
|
||||
override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
if isViewReady { return }
|
||||
constructViewHierarchy()
|
||||
activateConstraints()
|
||||
bindInteraction()
|
||||
isViewReady = true
|
||||
}
|
||||
|
||||
func constructViewHierarchy() {
|
||||
addSubview(button)
|
||||
addSubview(titleLabel)
|
||||
}
|
||||
|
||||
func activateConstraints() {
|
||||
button.snp.makeConstraints { make in
|
||||
make.top.equalTo(self)
|
||||
make.centerX.equalTo(self)
|
||||
make.size.equalTo(imageSize)
|
||||
}
|
||||
titleLabel.snp.makeConstraints { make in
|
||||
make.centerX.equalTo(self)
|
||||
make.top.equalTo(button.snp.bottom).offset(10)
|
||||
make.width.equalTo(100.scaleWidth())
|
||||
}
|
||||
}
|
||||
|
||||
func bindInteraction() {
|
||||
button.addTarget(self, action: #selector(buttonActionEvent(sender: )), for: .touchUpInside)
|
||||
}
|
||||
|
||||
// MARK: Update Info
|
||||
func updateImage(image: UIImage) {
|
||||
button.setBackgroundImage(image, for: .normal)
|
||||
}
|
||||
|
||||
func updateTitle(title: String) {
|
||||
titleLabel.text = title
|
||||
}
|
||||
|
||||
func updateTitleColor(titleColor: UIColor) {
|
||||
titleLabel.textColor = titleColor
|
||||
}
|
||||
|
||||
// MARK: Event Action
|
||||
@objc func buttonActionEvent(sender: UIButton) {
|
||||
guard let buttonActionBlock = buttonActionBlock else { return }
|
||||
buttonActionBlock(sender)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// UIButtonCommon.swift
|
||||
// TUICallKit
|
||||
//
|
||||
// Created by vincepzhang on 2023/2/10.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum TUIButtonEdgeInsetsStyle {
|
||||
case TUIButtonEdgeInsetsStyleTop
|
||||
case TUIButtonEdgeInsetsStyleLeft
|
||||
case TUIButtonEdgeInsetsStyleBottom
|
||||
case TUIButtonEdgeInsetsStyleRight
|
||||
}
|
||||
|
||||
extension UIButton {
|
||||
|
||||
func layoutButtonWithEdgeInsetsStyle(style: TUIButtonEdgeInsetsStyle, space: CGFloat) {
|
||||
let imageWidth = imageView?.frame.size.width ?? 0.0
|
||||
let imageHeight = imageView?.frame.size.height ?? 0.0
|
||||
let labelWidth = titleLabel?.intrinsicContentSize.width ?? 0.0
|
||||
let labelHeight = titleLabel?.intrinsicContentSize.height ?? 0.0
|
||||
var imageEdgeInsets: UIEdgeInsets = .zero
|
||||
var labelEdgeInsets: UIEdgeInsets = .zero
|
||||
switch style {
|
||||
case .TUIButtonEdgeInsetsStyleTop:
|
||||
imageEdgeInsets = UIEdgeInsets(top: -labelHeight - space / 2.0, left: 0, bottom: 0, right: -labelHeight)
|
||||
labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: -labelHeight - space / 2.0, right: 0)
|
||||
break
|
||||
case .TUIButtonEdgeInsetsStyleLeft:
|
||||
imageEdgeInsets = UIEdgeInsets(top: 0, left: -space / 2.0, bottom: 0, right: space / 2.0)
|
||||
labelEdgeInsets = UIEdgeInsets(top: 0, left: space / 2.0, bottom: 0 , right: -space / 2.0)
|
||||
break
|
||||
case .TUIButtonEdgeInsetsStyleBottom:
|
||||
imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight - space / 2.0, right: -labelWidth)
|
||||
labelEdgeInsets = UIEdgeInsets(top: -imageHeight - space / 2.0, left: -imageWidth, bottom: 0 , right: 0)
|
||||
break
|
||||
case .TUIButtonEdgeInsetsStyleRight:
|
||||
imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth+space / 2.0, bottom: 0, right: -labelWidth - space / 2.0)
|
||||
labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth - space / 2.0, bottom: 0 , right: imageWidth + space / 2.0)
|
||||
break
|
||||
}
|
||||
self.titleEdgeInsets = labelEdgeInsets
|
||||
self.imageEdgeInsets = imageEdgeInsets
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// UIColorCommon.swift
|
||||
// TUICallKitSwift
|
||||
//
|
||||
// Created by vincepzhang on 2023/1/3.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIColor {
|
||||
|
||||
static func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
|
||||
var colorImage: UIImage
|
||||
var rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
|
||||
UIGraphicsBeginImageContext(rect.size)
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return UIImage() }
|
||||
context.setFillColor(color.cgColor)
|
||||
context.fill(rect)
|
||||
guard let cImage = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage() }
|
||||
colorImage = cImage
|
||||
UIGraphicsEndImageContext()
|
||||
return colorImage
|
||||
}
|
||||
|
||||
static func t_colorWithHexString(color: String) -> UIColor {
|
||||
return UIColor.t_colorWithHexString(color: color, alpha: 1)
|
||||
}
|
||||
|
||||
static func t_colorWithHexString(color: String, alpha: CGFloat) -> UIColor {
|
||||
var colorString: String = color.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
|
||||
|
||||
if colorString.count < 6 {
|
||||
return UIColor.clear
|
||||
}
|
||||
|
||||
if colorString.hasPrefix("0X") {
|
||||
colorString = colorString.substring(from: colorString.index(colorString.startIndex, offsetBy: 2))
|
||||
}
|
||||
|
||||
if colorString.hasPrefix("#") {
|
||||
colorString = colorString.substring(from: colorString.index(colorString.startIndex, offsetBy: 1))
|
||||
}
|
||||
|
||||
if colorString.count < 6 {
|
||||
return UIColor.clear
|
||||
}
|
||||
|
||||
let rString = String(colorString.prefix(2))
|
||||
colorString.removeFirst(2)
|
||||
let gString = String(colorString.prefix(2))
|
||||
colorString.removeFirst(2)
|
||||
let bString = String(colorString.prefix(2))
|
||||
|
||||
var r = Int32()
|
||||
var g = Int32()
|
||||
var b = Int32()
|
||||
Scanner(string: rString).scanHexInt32(&r)
|
||||
Scanner(string: gString).scanHexInt32(&g)
|
||||
Scanner(string: bString).scanHexInt32(&b)
|
||||
|
||||
return UIColor(red: CGFloat(r) / 255.0 , green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
|
||||
}
|
||||
|
||||
static func t_colorWithAlphaHexString(color: String) -> UIColor {
|
||||
var colorString: String = color.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
|
||||
|
||||
if colorString.count < 6 {
|
||||
return UIColor.clear
|
||||
}
|
||||
|
||||
if colorString.hasPrefix("0X") {
|
||||
colorString = colorString.substring(from: colorString.index(colorString.startIndex, offsetBy: 2))
|
||||
}
|
||||
|
||||
if colorString.hasPrefix("#") {
|
||||
colorString = colorString.substring(from: colorString.index(colorString.startIndex, offsetBy: 1))
|
||||
}
|
||||
|
||||
if colorString.count < 6 {
|
||||
return UIColor.clear
|
||||
}
|
||||
|
||||
let rString = String(colorString.prefix(2))
|
||||
colorString.removeFirst(2)
|
||||
let gString = String(colorString.prefix(2))
|
||||
colorString.removeFirst(2)
|
||||
let bString = String(colorString.prefix(2))
|
||||
colorString.removeFirst(2)
|
||||
let alphaString = String(colorString.prefix(2))
|
||||
|
||||
var r: Float = Float()
|
||||
var g: Float = Float()
|
||||
var b: Float = Float()
|
||||
var alpha: Float = Float()
|
||||
Scanner(string: rString).scanHexFloat(&r)
|
||||
Scanner(string: gString).scanHexFloat(&g)
|
||||
Scanner(string: bString).scanHexFloat(&b)
|
||||
Scanner(string: alphaString).scanHexFloat(&alpha)
|
||||
|
||||
return UIColor(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: CGFloat(alpha))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// UIViewControllerCommon.swift
|
||||
// TUICallKit
|
||||
//
|
||||
// Created by vincepzhang on 2023/1/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
|
||||
extension UIViewController {
|
||||
|
||||
static func getCurrentViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
|
||||
if let nav = base as? UINavigationController {
|
||||
return getCurrentViewController(base: nav.visibleViewController)
|
||||
}
|
||||
if let tab = base as? UITabBarController {
|
||||
return getCurrentViewController(base: tab.selectedViewController)
|
||||
}
|
||||
if let presented = base?.presentedViewController {
|
||||
return getCurrentViewController(base: presented)
|
||||
}
|
||||
return base
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import UIKit
|
||||
|
||||
extension UIWindow {
|
||||
public func t_makeKeyAndVisible() {
|
||||
if #available(iOS 13.0, *) {
|
||||
for windowScene in UIApplication.shared.connectedScenes {
|
||||
if windowScene.activationState == UIScene.ActivationState.foregroundActive ||
|
||||
windowScene.activationState == UIScene.ActivationState.background {
|
||||
self.windowScene = windowScene as? UIWindowScene
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
self.makeKeyAndVisible()
|
||||
}
|
||||
|
||||
static func getKeyWindow() -> UIWindow? {
|
||||
var keyWindow: UIWindow?
|
||||
if #available(iOS 13, *) {
|
||||
keyWindow = UIApplication.shared.connectedScenes
|
||||
.filter({ $0.activationState == .foregroundActive })
|
||||
.compactMap { $0 as? UIWindowScene }
|
||||
.flatMap { $0.windows }
|
||||
.first(where: { $0.isKeyWindow })
|
||||
} else {
|
||||
keyWindow = UIApplication.shared.keyWindow
|
||||
}
|
||||
return keyWindow
|
||||
}
|
||||
|
||||
static func getTopFullscreenWindow() -> UIWindow? {
|
||||
let topWindow = UIApplication.shared.windows
|
||||
.filter { !$0.isHidden && $0.bounds.equalTo(UIScreen.main.bounds) }
|
||||
.max(by: { $0.windowLevel.rawValue < $1.windowLevel.rawValue })
|
||||
|
||||
return topWindow
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user