FunnyButton_SwiftUI - 全局便捷調(diào)試的按鈕,是FunnyButton的SwiftUI
版本,只需在View上添加調(diào)試事件,即可隨時(shí)點(diǎn)擊按鈕進(jìn)行調(diào)試。
Feature:
? 位于【父View頂層】或【W(wǎng)indow層級(jí)】,不會(huì)被app內(nèi)的界面覆蓋;
? 自適應(yīng)父View區(qū)域,自動(dòng)靠邊,適配橫豎屏;
? 可執(zhí)行單個(gè)/多個(gè)調(diào)試事件;
? 兼容`iPhone`&`iPad`;
? API簡(jiǎn)單易用。
- GitHub傳送門
-
UIKit
版本:FunnyButton
Effect
-
單個(gè)調(diào)試事件
-
多個(gè)調(diào)試事件
Initialize
建議在main函數(shù)中初始化
import SwiftUI
import FunnyButton_SwiftUI
@main
struct DemoApp: App {
// 初始化funny對(duì)象用于全局狀態(tài)管理(添加、移除調(diào)試事件)
@StateObject var funny = Funny()
var body: some Scene {
WindowGroup {
ContentView()
// 將funny對(duì)象從根視圖開(kāi)始注入到環(huán)境中,使其子View都能添加/移除調(diào)試事件
.environmentObject(funny)
// 方式1:將`FunnyButton`添加在`ContentView`的頂層(不會(huì)攔截按鈕區(qū)域以外的手勢(shì)事件)
// .overlay(FunnyView())
// 方式2:將`FunnyButton`添加在自定義的`UIWindow`上(不會(huì)攔截按鈕區(qū)域以外的手勢(shì)事件)
.onAppear() {
FunnyWindow.show()
}
}
}
}
- 方式1:添加在
ContentView
的最頂部,會(huì)被通過(guò)Present
打開(kāi)的視圖覆蓋(新頁(yè)面是蓋在ContentView
的上方) - 方式2:添加在自定義的
UIWindow
,不會(huì)被通過(guò)Present
打開(kāi)的視圖覆蓋
API
- 添加單個(gè)調(diào)試事件
struct SingleActionView: View {
var body: some View {
Text("點(diǎn)擊笑臉打印日志")
.funnyAction {
print("tap me")
}
}
}
- 添加多個(gè)調(diào)試事件
struct MultipleActionsView: View {
var body: some View {
Text("點(diǎn)擊笑臉選擇日志打印")
.funnyActions {[
FunnyAction(name: "Happy") {
print("Happy")
},
FunnyAction(name: "New") {
print("New")
},
FunnyAction(name: "Yeah") {
print("Yeah")
},
]}
}
}
.funnyAction
和.funnyActions
都是通過(guò)FunnyModifier
實(shí)現(xiàn):調(diào)試事件在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
- 可改動(dòng)的UI屬性均為靜態(tài)屬性,且有默認(rèn)實(shí)現(xiàn),如需改動(dòng)建議啟動(dòng)App時(shí)配置。
public class FunnyButton: UIButton {
......
/// 普通狀態(tài)
static var normalEmoji = "??"
/// 點(diǎn)擊狀態(tài)
static var touchingEmoji = "??"
/// 毛玻璃樣式(nil為無(wú)毛玻璃)
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)
/// 初始點(diǎn)(想`靠右/靠下`的話,`x/y`的值就設(shè)置大一點(diǎn),最后會(huì)靠在安全區(qū)域的邊上)
static var startPoint: CGPoint = CGPoint(x: 600, y: 100)
/// 安全區(qū)域的邊距
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