美文网首页
FunnyButton_SwiftUI 便捷调试按钮

FunnyButton_SwiftUI 便捷调试按钮

作者: 健了个平_24 | 来源:发表于2023-01-19 01:32 被阅读0次

    FunnyButton_SwiftUI - 全局便捷调试的按钮,是FunnyButtonSwiftUI版本,只需在View上添加调试事件,即可随时点击按钮进行调试。

    Feature:
        ✅ 位于【父View顶层】或【Window层级】,不会被app内的界面覆盖;
        ✅ 自适应父View区域,自动靠边,适配横竖屏;
        ✅ 可执行单个/多个调试事件;
        ✅ 兼容`iPhone`&`iPad`;
        ✅ API简单易用。
    

    Effect

    • 单个调试事件


    • 多个调试事件


    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)
                    // 方式1:将`FunnyButton`添加在`ContentView`的顶层(不会拦截按钮区域以外的手势事件)
    //                .overlay(FunnyView())
                    // 方式2:将`FunnyButton`添加在自定义的`UIWindow`上(不会拦截按钮区域以外的手势事件)
                    .onAppear() {
                        FunnyWindow.show()
                    }
            }
        }
    }
    
    • 方式1:添加在ContentView的最顶部,会被通过Present打开的视图覆盖(新页面是盖在ContentView的上方)
    • 方式2:添加在自定义的UIWindow,不会被通过Present打开的视图覆盖

    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

    相关文章

      网友评论

          本文标题:FunnyButton_SwiftUI 便捷调试按钮

          本文链接:https://www.haomeiwen.com/subject/senfhdtx.html