SwiftUIKit

SwiftUIKit 提供了一系列常用的控件,用于 SwiftUI

支持

如果您觉得 SwiftUIKit 有用,并希望帮助支持其持续开发和维护,请考虑进行小额捐款,尤其是在商业产品中使用时。

Buy Me A Coffee

正是因为有像您这样的贡献者的支持,我才能继续构建、发布和维护高质量、文档完善的 Swift Package(如 SwiftUIKit),而且是免费的。

安装

Swift Package Manager (Xcode 11 及以上)

  1. 在 Xcode 中,选择 File > Add Package Dependency… 菜单项。
  2. https://github.com/Appracatappra/SwiftUIKit.git 粘贴到对话框中。
  3. 按照 Xcode 的说明完成安装。

为什么不使用 CocoaPods、Carthage 等?

支持多个依赖管理器会使维护一个库的复杂度呈指数级增长,并且耗费更多时间。

由于 Swift Package Manager 已集成到 Xcode 11(及更高版本)中,因此它是继续支持的最简单选择。

概述

SwiftUIKit 提供了一种简单的方法,可以在您的 SwiftUI View 中显示警报,并提供几个有用的新控件,包括

嵌入的声音

SwiftUIKit 中嵌入了两个默认声音

这两种声音均来自 Freeound.org,遵循 Creative Commons 0 许可。

SwiftUIKit 类

SwiftUIKit 提供了一些辅助实用程序,使您可以轻松访问存储在 Swift Package 中的资源(例如上面的声音)。

例如,以下代码将返回 diamond-click.mp3 文件的路径

let path = SwiftUIKit.pathTo(resource:"diamond-click.mp3")

默认控件设置

SwiftUIKit 中定义的几个控件都有一组静态属性来控制创建的所有控件实例,而无需指定这些属性。

例如,IconButton 定义了

/// The default background color for the `IconButton`.
static public var defaultBackgroundColor:Color = .gray
    
/// The default sound source for the `IconButton`.
static public var defaultSoundSource:SwiftUIKit.Source = .packageBundle
    
/// The default button clicked sound `IconButton`.
static public var defaultButtonSound:String = "mouse-click.mp3"
    
/// The default focused sound for the `IconButton`.
static public var defaultButtonFocusSound:String = "diamond-click.mp3"

如果您想要以通用方式设置整个 App 中使用的所有 IconButton 实例的样式,只需调整 IconButtondefaultBackgroundColor

IconButton.defaultBackgroundColor = .blue

现在,所有新创建的 IconButtons 都将具有蓝色背景。

在哪里设置样式更改

要使样式更改生效,您需要在绘制任何 Views 之前进行更改。 您可以在主应用程序上使用以下代码

import SwiftUI
import SwiftletUtilities
import LogManager
import SwiftUIKit

@main
struct PackageTesterApp: App {
    @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
    @Environment(\.scenePhase) private var scenePhase
    @Environment(\.colorScheme) var colorScheme
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: scenePhase) { oldScenePhase, newScenePhase in
            switch newScenePhase {
            case .active:
                Debug.info(subsystem: "PackageTesterApp", category: "Scene Phase", "App is active")
            case .inactive:
                Debug.info(subsystem: "PackageTesterApp", category: "Scene Phase", "App is inactive")
            case .background:
                Debug.info(subsystem: "PackageTesterApp", category: "Scene Phase", "App is in background")
            @unknown default:
                Debug.notice(subsystem: "PackageTesterApp", category: "Scene Phase", "App has entered an unexpected scene: \(oldScenePhase), \(newScenePhase)")
            }
        }
    }
}

/// Class the handle the event that would typically be handled by the Application Delegate so they can be handled in SwiftUI.
class AppDelegate: NSObject, UIApplicationDelegate {
    
    /// Handles the app finishing launching
    /// - Parameter application: The app that has started.
    func applicationDidFinishLaunching(_ application: UIApplication) {
        // Register to receive remote notifications
        UIApplication.shared.registerForRemoteNotifications()
    }
    
    /// Handle the application getting ready to launch
    /// - Parameters:
    ///   - application: The application that is going to launch.
    ///   - launchOptions: Any options being passed to the application at launch time.
    /// - Returns: Returns `True` if the application can launch.
    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        // Set any `SwiftUIKit` global style defaults here before any `Views` are drawn.
        // Set style defaults
        IconButton.defaultBackgroundColor = .blue
        return true
    }
    
    /// Handles the app receiving a remote notification
    /// - Parameters:
    ///   - application: The app receiving the notifications.
    ///   - userInfo: The info that has been sent to the App.
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        
    }
}

有了这段代码,就可以在 func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool 中进行任何样式更改,它们将应用于之后构建的所有视图。

文档

Package 包含其所有功能的完整 DocC 文档