SwiftyUIX
SwiftyUIX 是一个功能强大且多才多艺的库,专为 iOS 和 macOS 开发者设计,提供一系列有用的扩展和预定义视图。通过 SwiftyUIX,您可以增强开发工作流程,提高代码可读性,并通过使用其便捷实用程序来加速应用开发。
安装
CocoaPods
要将 SwiftyUIX 集成到您的 Xcode 项目中,使用 CocoaPods,请将以下行添加到您的 Podfile 中
pod 'SwiftyUIX'
然后,运行以下命令:
$ pod install
Swift 包管理器 (SPM)
您还可以使用 Swift 包管理器将 SwiftyUIX 添加到您的项目。在 Xcode 中,转到文件 -> Swift 包 -> 添加包依赖... 并输入此仓库的 URL
https://github.com/Mokshsuthar/SwiftyUIX.git
按照提示完成安装。
用法
视图
BlurView
BlurView
是 SwiftUI 视图,用于创建视觉模糊效果,主要用于 iOS。
功能
- 视觉模糊效果:
BlurView
是一个UIViewRepresentable
,它将一个UIVisualEffectView
封装起来,根据指定的UIBlurEffect.Style
应用于其内容的模糊效果。 - 可自定义样式: 您可以在初始化期间提供不同的
UIBlurEffect.Style
来自定义模糊效果。默认样式是.systemChromeMaterial
,代表标准的系统模糊外观。
使用示例
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
// Your content here...
// Apply a blur effect to the content
BlurView(style: .systemUltraThinMaterial)
}
}
}
要求
- iOS 13.0 或更高版本
HTMLWebView
HTMLWebView
是 SwiftUI 视图,用于显示来自指定文件路径的 HTML 内容的 WKWebView
。
功能
- 加载 HTML 内容:
HTMLWebView
是一个UIViewRepresentable
,它从给定的文件路径加载并在WKWebView
中显示 HTML 内容。 - 透明背景:
WKWebView
及其滚动视图具有透明背景,允许与其他 SwiftUI 视图无缝集成。 - 可选滚动事件处理: 您可以选择通过
scrollViewDidScroll
闭包参数从网页的滚动视图中接收滚动事件。
使用示例
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
// Your other SwiftUI views here...
// Display the HTML content from a specified file path
HTMLWebView(filePath: Bundle.main.path(forResource: "sample", ofType: "html"))
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
}
}
要求
HTMLWebView
在 iOS 上的要求是 iOS 13.0 或更高版本。HTMLWebView
在 macOS 上的要求是 macOS 11.0 或更高版本。
TransparentBackground
TransparentBackground
是 SwiftUI 的 UIViewRepresentable
,它使显示的表单或全屏覆盖物的背景透明,主要用于 iOS。
使用注意事项
直接设置父视图父视图的背景颜色(如在此结构中所做)可能会产生意外的副作用,并且可能不完全可靠地适用于所有 SwiftUI 版本或实现。在将其用于生产之前,务必彻底测试此代码并考虑潜在的边缘情况。
使用示例
import SwiftUI
struct ContentView: View {
@State private var isSheetPresented = false
var body: some View {
Button("Present Sheet") {
isSheetPresented.toggle()
}
.sheet(isPresented: $isSheetPresented, content: {
// Your sheet content here...
YourView()
.background(TransparentBackground()) // Apply the transparent background to the presented sheet
})
}
}
要求
- iOS 13.0 或更高版本
扩展
swiftui 视图扩展
-
设备尺寸
screenWidth
:检索设备屏幕宽度,类型为CGFloat
。screenHeight
:检索设备屏幕高度,类型为CGFloat
。topSafeAreaHeight
:检索顶部安全区(如果有缺口)的高度,类型为CGFloat
。bottomSafeAreaHeight
:检索底部安全区的高度,类型为CGFloat
。
-
屏幕圆角
screenCornerRadius(minimum:)
:计算并返回设备显示器的圆角半径。可设置最小值。
-
Home Indicator
hideHomeIndicator()
:隐藏iOS 16.0或更高版本设备上的主页指示符。兼容早期iOS版本。
-
触觉反馈
playHapticFeedback(_:)
:使用指定的feedbackType
播放触觉反馈。
-
视图框架操作
fullframe(alignment:)
:使用可选的对齐方式让视图占据父视图的全部空间。fullWidth(height:alignment:)
:设置具有可用全宽和可选高度的框架。fullHeight(width:alignment:)
:设置具有可用全高和可选宽度的框架。squareFrame(size:alignment:)
:为具有指定大小和对齐方式的视图创建正方形框架。
-
忽略安全区域
ignoreSafeArea_C()
:忽略iOS 14及以上版本设备的安全区域内边距。
-
视图遮罩
cornerRadius(_:)
:为视图添加连续的圆角。
-
滚动位置检测
getScrollPosition(key:handler:)
:检测视图的滚动位置,并使用偏移量调用处理程序。
-
系统图标图像视图
systemImage(_:)
:创建具有系统图标的图像视图。
- 视图边框
border(lineWidth:cornerRadius:color:)
:向视图添加指定线宽、圆角和颜色的边框。
SwiftUI 图像扩展
一组易于操作的 SwiftUI 图像视图扩展。
功能
-
带有宽高比的方形框架
squareFrameWithApectRatio(value:contentMode:)
:根据指定的尺寸调整大小,同时保持其宽高比。通过contentMode
参数可以控制图片如何填充框架。
-
保持宽高比调整大小
resizeWithApectRatio(contentMode:)
:调整图片大小,同时保持其宽高比。通过contentMode
参数可以控制图片如何适应其新框架。
使用示例
import SwiftUI
struct ContentView: View {
var body: some View {
Image("exampleImage")
.squareFrameWithApectRatio(value: 100, contentMode: .fill)
.border(lineWidth: 2, cornerRadius: 10, color: .blue)
}
}
线程扩展
一组对 Swift 中的线程进行管理的有用扩展。
功能
-
在主线程上执行
OnMainThread(_:)
:如果当前线程已经是主线程,则在主线程上执行提供的闭包。否则,异步将其调度到主线程执行。
-
在后台线程上执行
OnBackGroudThread(_:)
:使用全局后台队列在后台线程上执行给定的闭包。
-
在主线程上延时执行
runAfter(_:completion:)
:使用DispatchQueue.main.asyncAfter
在主线程上将给定的闭包延迟指定时间后运行(按秒)。
-
自定义线程创建
startNewThread(name:qos:execute:)
:创建并管理具有特定配置的自定义线程。此扩展允许您使用自定义名称和服务质量(QoS)启动新线程。
使用示例
import Foundation
// Execute a closure on the main thread after a delay of 2 seconds
Thread.runAfter(2) {
print("This will be executed after 2 seconds on the main thread.")
}
// Execute a closure on the main thread
Thread.OnMainThread {
print("This will be executed on the main thread.")
}
// Execute a closure on a background thread
Thread.OnBackGroudThread {
print("This will be executed on a background thread.")
}
// Create and start a custom thread
Thread.startNewThread(name: "CustomThread", qos: .userInitiated) {
print("This is a custom thread with name 'CustomThread' and QoS 'userInitiated'.")
}
日期扩展
一组用于在 Swift 中处理 Date 对象的有用扩展。
功能
-
可读时间
getReadableTime()
:将 Date 对象格式化为具有 AM/PM 指示器的 12 小时格式的人类可读时间字符串。例如输出:“03:30 PM”或“11:45 AM”。
-
可读日期
getReadableDate()
:将 Date 对象格式化为“MM/DD/YYYY”格式的人类可读日期字符串。例如输出:“07/22/2023”或“01/05/2024”。
-
可读日期和时间
getReadableDateTime()
:将 Date 对象格式化为人类可读的日期和时间字符串。例如输出:“07/22/2023 03:30 PM”或“01/05/2024 11:45 AM”。
-
时间戳生成
TimeStemp(format:)
:根据给定的格式提供唯一的唯一标识符。默认格式为“yyyyMMdd'T'HHmmssSSS”。
-
月份和日期名称
getMonthName()
:从日期中获取完整的月份名称,例如“January”、“February”等。getShortMonthName()
:从日期中获取简短的月份名称,例如“Jan”、“Feb”等。getDayName()
:从日期中获取完整的日期名称,例如“Sunday”、“Monday”等。getShortDayName()
:从日期中获取简短的日期名称,例如“Sun”、“Mon”等。
-
日期组件检索
getDateComponent(_:calendar:)
:使用提供的日历来获取Date对象中特定组件的值(例如,天、月、年、小时、分钟)。如果没有提供日历,则默认为当前日历。
使用示例
import Foundation
let currentDate = Date()
let readableTime = currentDate.getReadableTime()
print("Readable Time: \(readableTime)")
let readableDate = currentDate.getReadableDate()
print("Readable Date: \(readableDate)")
let readableDateTime = currentDate.getReadableDateTime()
print("Readable Date and Time: \(readableDateTime)")
let timestamp = currentDate.TimeStemp()
print("Timestamp: \(timestamp)")
let monthName = currentDate.getMonthName()
print("Month Name: \(monthName)")
let shortMonthName = currentDate.getShortMonthName()
print("Short Month Name: \(shortMonthName)")
let dayName = currentDate.getDayName()
print("Day Name: \(dayName)")
let shortDayName = currentDate.getShortDayName()
print("Short Day Name: \(shortDayName)")
let day = currentDate.getDateComponent(.day)
print("Day: \(day)")
let month = currentDate.getDateComponent(.month)
print("Month: \(month)")
let year = currentDate.getDateComponent(.year)
print("Year: \(year)")
数据扩展
Swift中处理数据对象的便捷扩展。
功能
- 可读数据大小
getReadableDataSize()
:将Data对象的大小转换为可读的格式,显示单位如KB、MB、GB等。
使用示例
import Foundation
let testData = Data(repeating: 0, count: 1024) // 1 KB
let readableSize = testData.getReadableDataSize()
print("Readable Size: \(readableSize)") // Output: "1 KB"
颜色扩展
功能
-
从十六进制字符串初始化
init(hexString:)
:从有效的十六进制颜色字符串(例如,“#RRGGBB”或“#AARRGGBB”)初始化Color对象。
-
转换为十六进制字符串
toHex()
:将Color对象转换为它的十六进制表示形式。对于macOS,此函数仅在macOS 11.0及更高版本上可用,对于iOS,需要iOS 14.0及更高版本。
-
从
UIColor
(iOS)或NSColor
(macOS)转换为十六进制代码toHexCode()
:将UIColor
对象(iOS)或NSColor
对象(macOS)转换为它的十六进制表示形式。
使用示例
import SwiftUI
let redColor = Color(hexString: "#FF0000")
let hexCode = redColor.toHex()
print("Hex Code: \(hexCode ?? "Unknown")") // Output: "Hex Code: #FF0000"
#if os(iOS)
if let uiColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).toHexCode() {
print("UI Color Hex Code: \(uiColor)") // Output: "UI Color Hex Code: #7F7F7F"
}
#elseif os(macOS)
if let nsColor = NSColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).toHexCode() {
print("NS Color Hex Code: \(nsColor)") // Output: "NS Color Hex Code: #7F7F7F"
}
#endif
许可证
本项目的许可证为MIT许可证。根据您的需求,请随意使用和修改。