FunnyButton_SwiftUI 便捷調試按鈕

語言: CN / TW / HK

FunnyButton_SwiftUI - 全局便捷調試的按鈕,是FunnyButtonSwiftUI版本,只需在View上添加調試事件,即可隨時點擊按鈕進行調試。

Feature:
    ✅ 位於Window層級,不會被app內的界面覆蓋;
    ✅ 自適應父View區域,自動靠邊,適配橫豎屏;
    ✅ 可執行單個/多個調試事件;
    ✅ 兼容iPhone&iPad;
    ✅ 僅限DEBUG環境;
    ✅ API簡單易用。

Effect

  • 單個調試事件

single_action.gif

  • 多個調試事件

multiple_actions.gif

Initialize

建議在main函數中初始化 ```swift import SwiftUI import FunnyButton_SwiftUI

@main struct DemoApp: App { // 初始化funny對象用於全局狀態管理(添加、移除調試事件) @StateObject var funny = Funny()

var body: some Scene {
    WindowGroup {
        ContentView()
            // 將funny對象從根視圖開始注入到環境中,使其子View都能添加/移除調試事件
            .environmentObject(funny)
            // 添加`FunnyButton`的容器視圖在最頂部(不會攔截按鈕區域以外的手勢事件)
            .overlay(FunnyView())
    }
}

} ```

API

  • 添加單個調試事件 swift struct SingleActionView: View { var body: some View { Text("點擊笑臉打印日誌") .funnyAction { print("tap me") } } }

  • 添加多個調試事件 swift struct MultipleActionsView: View { var body: some View { Text("點擊笑臉選擇日誌打印") .funnyActions {[ FunnyAction(name: "Happy") { print("Happy") }, FunnyAction(name: "New") { print("New") }, FunnyAction(name: "Yeah") { print("Yeah") }, ]} } }

.funnyAction.funnyActions都是通過FunnyModifier實現:調試事件在onAppear添加,在onDisappear移除。 ```swift public struct FunnyModifier: ViewModifier { @EnvironmentObject var funny: Funny

var getActions: () -> [FunnyAction]

public func body(content: Content) -> some View {
    content
        .onAppear() {
            funny.getActions = getActions
        }
        .onDisappear() {
            funny.getActions = nil
        }
}

} ```

Custom button UI

FunnyButton.swift - 可改動的UI屬性均為靜態屬性,且有默認實現,如需改動建議啟動App時配置。

```swift public class FunnyButton: UIButton { ......

/// 普通狀態
static var normalEmoji = "😛"

/// 點擊狀態
static var touchingEmoji = "😝"

/// 毛玻璃樣式(nil為無毛玻璃)
static var effect: UIVisualEffect? = {
    if #available(iOS 13, *) {
        return UIBlurEffect(style: .systemThinMaterial)
    }
    return UIBlurEffect(style: .prominent)
}()

/// 背景色
static var bgColor: UIColor? = UIColor(red: 200.0 / 255.0, green: 100.0 / 255.0, blue: 100.0 / 255.0, alpha: 0.2)

/// 初始點(想`靠右/靠下`的話,`x/y`的值就設置大一點,最後會靠在安全區域的邊上)
static var startPoint: CGPoint = CGPoint(x: 600, y: 100)

/// 安全區域的邊距
static var safeMargin: CGFloat = 12

......

} ```

Installation

FunnyButton_SwiftUI is available through CocoaPods. To install it, simply add the following line to your Podfile:

ruby pod 'FunnyButton_SwiftUI'

Author

Rogue24, [email protected]