FunnyButton_SwiftUI – 大局便捷调试的按钮,是FunnyButton的SwiftUI版别,只需在View上增加调试事情,即可随时点击按钮进行调试。

Feature:
    ✅ 位于Window层级,不会被app内的界面掩盖;
    ✅ 自适应父View区域,自动靠边,适配横竖屏;
    ✅ 可执行单个/多个调试事情;
    ✅ 兼容iPhone&iPad;
    ✅ 仅限DEBUG环境;
    ✅ API简略易用。
  • GitHub传送门
  • UIKit版别:FunnyButton

Effect

  • 单个调试事情

FunnyButton_SwiftUI 便捷调试按钮

  • 多个调试事情

FunnyButton_SwiftUI 便捷调试按钮

Initialize

主张在main函数中初始化

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

  • 增加单个调试事情
struct SingleActionView: View {
    var body: some View {
        Text("点击笑脸打印日志")
            .funnyAction { 
                print("tap me")
            }
    }
}
  • 增加多个调试事情
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移除。

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时配置。

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:

pod 'FunnyButton_SwiftUI'

Author

Rogue24, zhoujianping24@hotmail.com