代码优化

This commit is contained in:
xaoxuu 2023-08-08 00:12:36 +08:00
parent 320e4db560
commit 4ffd6c5617
15 changed files with 140 additions and 62 deletions

View File

@ -83,8 +83,8 @@
CDA83DB828C601E60025F0DF /* TableHeaderView.swift */,
CD9C7B1D28CB8972006190CD /* Extensions.swift */,
CD6537C028C35E1C00A5981B /* ListVC.swift */,
CDB7A1CF28C32A7400E034D8 /* AlertVC.swift */,
CD6537C228C35E6200A5981B /* ToastVC.swift */,
CDB7A1CF28C32A7400E034D8 /* AlertVC.swift */,
CD6537C428C35F2C00A5981B /* SheetVC.swift */,
CD8EEF4028BC5C7200E660EA /* Main.storyboard */,
CD8EEF4328BC5C7300E660EA /* Assets.xcassets */,

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="06J-FN-U3n">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22146" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="06J-FN-U3n">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22122"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
@ -18,10 +18,10 @@
<color key="backgroundColor" systemColor="systemGroupedBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="reuseIdentifier" id="aQS-HA-vLM">
<rect key="frame" x="20" y="49.5" width="374" height="43.5"/>
<rect key="frame" x="20" y="55.5" width="374" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="aQS-HA-vLM" id="Kt9-sG-LyY">
<rect key="frame" x="0.0" y="0.0" width="345.5" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="343.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
@ -36,7 +36,7 @@
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="ydp-D5-Zdx" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="955" y="804"/>
<point key="canvasLocation" x="1865" y="804"/>
</scene>
<!--Tab Bar Controller-->
<scene sceneID="ej2-I3-4Bd">
@ -50,9 +50,9 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tabBar>
<connections>
<segue destination="NBo-Re-tKO" kind="relationship" relationship="viewControllers" id="0Aw-nV-Vkk"/>
<segue destination="h7R-Kd-Dn5" kind="relationship" relationship="viewControllers" id="icb-EL-vph"/>
<segue destination="9SY-ag-pK6" kind="relationship" relationship="viewControllers" id="hyh-rR-Frr"/>
<segue destination="h7R-Kd-Dn5" kind="relationship" relationship="viewControllers" id="4rA-b5-Kd6"/>
<segue destination="NBo-Re-tKO" kind="relationship" relationship="viewControllers" id="4BA-vv-RD2"/>
<segue destination="9SY-ag-pK6" kind="relationship" relationship="viewControllers" id="vVd-PW-h6L"/>
</connections>
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="avc-BE-wZC" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
@ -69,10 +69,10 @@
<color key="backgroundColor" systemColor="systemGroupedBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="reuseIdentifier" id="Cfl-KC-JaI">
<rect key="frame" x="20" y="49.5" width="374" height="43.5"/>
<rect key="frame" x="20" y="55.5" width="374" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Cfl-KC-JaI" id="OCw-s2-GD5">
<rect key="frame" x="0.0" y="0.0" width="345.5" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="343.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
@ -87,7 +87,7 @@
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="VmT-Tm-s4d" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1865" y="804"/>
<point key="canvasLocation" x="954" y="804"/>
</scene>
<!--Sheet-->
<scene sceneID="DG9-RE-7gC">
@ -99,10 +99,10 @@
<color key="backgroundColor" systemColor="systemGroupedBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="reuseIdentifier" id="H4y-fE-fXP">
<rect key="frame" x="20" y="49.5" width="374" height="43.5"/>
<rect key="frame" x="20" y="55.5" width="374" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="H4y-fE-fXP" id="LOF-l1-ziH">
<rect key="frame" x="0.0" y="0.0" width="345.5" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="343.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
@ -122,10 +122,10 @@
</scenes>
<resources>
<image name="bubble.left.fill" catalog="system" width="128" height="110"/>
<image name="exclamationmark.triangle.fill" catalog="system" width="128" height="112"/>
<image name="iphone" catalog="system" width="108" height="128"/>
<image name="exclamationmark.triangle.fill" catalog="system" width="128" height="109"/>
<image name="iphone" catalog="system" width="112" height="128"/>
<systemColor name="systemGroupedBackgroundColor">
<color red="0.94901960784313721" green="0.94901960784313721" blue="0.96862745098039216" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.94901960780000005" green="0.94901960780000005" blue="0.96862745100000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>

View File

@ -86,6 +86,25 @@ class SheetVC: ListVC {
}
list.add(title: "自定义样式") { section in
section.add(title: "堆叠样式") {
Sheet { sheet in
sheet.config.stackDepthEffect = true
sheet.config.windowEdgeInset = 0
sheet.add(title: "ProHUD")
sheet.add(subTitle: "堆叠样式")
sheet.add(message: "这个效果目前有个小BUG切后台之后显示会有点问题如果有解决方案请麻烦去社区反馈一下谢谢")
sheet.add(spacing: 24)
sheet.add(action: "去反馈", style: .destructive) { sheet in
guard let url = URL(string: "https://github.com/xaoxuu/ProHUD") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}
}
sheet.add(action: "取消", style: .gray)
}
}
section.add(title: "圆角半径 & 屏幕边距") {
Sheet { sheet in
sheet.add(title: "圆角半径 & 屏幕边距")
@ -111,13 +130,13 @@ class SheetVC: ListVC {
let s2 = UISlider()
s2.minimumValue = 0
s2.maximumValue = 40
s2.value = Float(sheet.config.screenEdgeInset)
s2.value = Float(sheet.config.windowEdgeInset)
sheet.add(subview: s2).snp.makeConstraints { make in
make.height.equalTo(50)
}
if #available(iOS 14.0, *) {
s2.addAction(.init(handler: { [unowned s2] act in
sheet.config.screenEdgeInset = CGFloat(s2.value)
sheet.config.windowEdgeInset = CGFloat(s2.value)
sheet.reloadData()
}), for: .valueChanged)
} else {

View File

@ -30,7 +30,7 @@ func updateProgress(in duration: TimeInterval, callback: @escaping (_ percent: C
}
}
let isTesting: Bool = false
let isTesting: Bool = true
class TestToast: Toast {
override func push() {
@ -173,7 +173,7 @@ class ToastVC: ListVC {
var i = 0
section.add(title: "多实例共存") {
Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)) { toast in
toast.config.cardEdgeInsets = .init(top: 32, left: 20, bottom: 32, right: 20)
}
}
section.add(title: "不存在就创建,存在就更新") {
@ -209,7 +209,17 @@ class ToastVC: ListVC {
}
}
}
section.add(title: "修改内边距") {
Toast(.message("这条toast的内边距经过自定义设置与其它的有所不同。")) { toast in
toast.config.cardEdgeInsets = .init(top: 40, left: 32, bottom: 40, right: 32)
}
}
section.add(title: "修改左右外边距") {
Toast(.message("这条toast的左右外边距经过自定义设置与其它的有所不同。")) { toast in
toast.config.windowEdgeInset = 8
toast.config.cardCornerRadius = 24
}
}
section.add(title: "圆角半径") {
func foo() {
Toast { toast in

View File

@ -93,11 +93,12 @@ extension Alert: DefaultLayout {
}
if contentStack.superview == nil {
contentView.addSubview(contentStack)
let cardEdgeInsets = config.cardEdgeInsetsByDefault
contentStack.snp.remakeConstraints { make in
make.top.equalToSuperview().inset(config.cardEdgeInsets.top)
make.left.equalToSuperview().inset(config.cardEdgeInsets.left)
make.bottom.equalToSuperview().inset(config.cardEdgeInsets.bottom)
make.right.equalToSuperview().inset(config.cardEdgeInsets.right)
make.top.equalToSuperview().inset(cardEdgeInsets.top)
make.left.equalToSuperview().inset(cardEdgeInsets.left)
make.bottom.equalToSuperview().inset(cardEdgeInsets.bottom)
make.right.equalToSuperview().inset(cardEdgeInsets.right)
}
}
// card background
@ -149,9 +150,10 @@ extension Alert {
}
if imageView.superview == nil {
contentStack.insertArrangedSubview(imageView, at: 0)
let cardEdgeInsets = config.cardEdgeInsetsByDefault
imageView.snp.remakeConstraints { (mk) in
mk.top.left.greaterThanOrEqualTo(contentView).inset(config.cardEdgeInsets.top * 2)
mk.right.bottom.lessThanOrEqualTo(contentView).inset(config.cardEdgeInsets.right * 2)
mk.top.left.greaterThanOrEqualTo(contentView).inset(cardEdgeInsets.top * 2)
mk.right.bottom.lessThanOrEqualTo(contentView).inset(cardEdgeInsets.right * 2)
mk.width.equalTo(config.iconSize.width)
mk.height.equalTo(config.iconSize.height)
}
@ -177,11 +179,12 @@ extension Alert {
} else {
contentStack.insertArrangedSubview(textStack, at: 0)
}
let cardEdgeInsets = config.cardEdgeInsetsByDefault
textStack.snp.remakeConstraints { (mk) in
mk.left.greaterThanOrEqualToSuperview().inset(config.textEdgeInsets.left)
mk.right.lessThanOrEqualToSuperview().inset(config.textEdgeInsets.right)
mk.top.greaterThanOrEqualTo(contentView).inset(config.cardEdgeInsets.top + config.textEdgeInsets.top)
mk.bottom.lessThanOrEqualTo(contentView).inset(config.cardEdgeInsets.bottom + config.textEdgeInsets.bottom)
mk.top.greaterThanOrEqualTo(contentView).inset(cardEdgeInsets.top + config.textEdgeInsets.top)
mk.bottom.lessThanOrEqualTo(contentView).inset(cardEdgeInsets.bottom + config.textEdgeInsets.bottom)
}
}
if titleCount > 0 {

View File

@ -53,13 +53,12 @@ extension Alert: HUD {
// hide window
let count = window.alerts.count
if count == 0 {
self.window = nil
UIView.animateEaseOut(duration: duration) {
window.backgroundView.alpha = 0
} completion: { done in
// self.windowalert
if window.alerts.count == 0, let scene = window.windowScene {
AppContext.alertWindow[scene] = nil
}
// window使windowwindow
window.isHidden = true
}
}
}

View File

@ -36,7 +36,7 @@ open class Controller: UIViewController {
}
deinit {
consolePrint("👌", self, "init")
consolePrint("👌", self, "deinit")
}
override public func viewDidLoad() {

View File

@ -66,9 +66,10 @@ public class Configuration: NSObject {
public var cardMinHeight = CGFloat(32)
///
public var cardEdgeInsets: UIEdgeInsets = {
.init(top: 16, left: 16, bottom: 16, right: 16)
}()
public var cardEdgeInsets: UIEdgeInsets?
var cardEdgeInsetsByDefault: UIEdgeInsets {
cardEdgeInsets ?? .init(top: 16, left: 16, bottom: 16, right: 16)
}
///
public var textEdgeInsets: UIEdgeInsets = {

View File

@ -15,7 +15,7 @@ extension UIImage {
/// ()
internal var isPortrait: Bool {
var isPortrait: Bool {
if AppContext.appBounds.width < 450 {
return true
}
@ -26,3 +26,7 @@ internal var isPortrait: Bool {
}
return false
}
var isPhonePortrait: Bool {
UIDevice.current.userInterfaceIdiom == .phone && (AppContext.windowScene?.interfaceOrientation.isPortrait == true)
}

View File

@ -11,8 +11,11 @@ public extension Sheet {
class Configuration: ProHUD.Configuration {
///
public var stackDepthEffect: Bool = false
///
public var screenEdgeInset: CGFloat = 4
public var windowEdgeInset: CGFloat = 16
///
public var isFullScreen = false
@ -41,6 +44,10 @@ public extension Sheet {
customBackgroundViewMask = callback
}
override var cardEdgeInsetsByDefault: UIEdgeInsets {
cardEdgeInsets ?? .init(top: 24, left: 24, bottom: 24, right: 24)
}
override var cardMaxWidthByDefault: CGFloat { cardMaxWidth ?? 500 }
override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? (AppContext.appBounds.height - 50) }
@ -53,7 +60,7 @@ public extension Sheet {
animateDurationForBuildOut ?? 0.5
}
override var cardCornerRadiusByDefault: CGFloat { cardCornerRadius ?? 40 }
override var cardCornerRadiusByDefault: CGFloat { cardCornerRadius ?? 32 }
}

View File

@ -44,7 +44,7 @@ extension Sheet: DefaultLayout {
loadContentMaskViewIfNeeded()
// layout
let maxWidth = config.cardMaxWidthByDefault
var width = AppContext.appBounds.width - config.screenEdgeInset * 2
var width = AppContext.appBounds.width - config.windowEdgeInset * 2
if width > maxWidth {
// landscape iPhone or iPad
width = maxWidth
@ -59,7 +59,7 @@ extension Sheet: DefaultLayout {
make.centerY.equalToSuperview()
} else {
if isPortrait {
make.bottom.equalToSuperview().inset(config.screenEdgeInset)
make.bottom.equalToSuperview().inset(config.windowEdgeInset)
} else {
make.bottom.equalToSuperview().inset(AppContext.safeAreaInsets.bottom)
}
@ -78,21 +78,30 @@ extension Sheet: DefaultLayout {
// stack
if contentStack.superview == nil {
contentView.addSubview(contentStack)
let isFullScreen = config.isFullScreen
let cardEdgeInsets = config.cardEdgeInsetsByDefault
contentStack.snp.remakeConstraints { make in
let safeArea: UIEdgeInsets
if config.isFullScreen {
safeArea = AppContext.safeAreaInsets
} else {
safeArea = .zero
var safeAreaInsets = cardEdgeInsets
if isFullScreen {
safeAreaInsets.top += AppContext.safeAreaInsets.top
safeAreaInsets.bottom += AppContext.safeAreaInsets.bottom
}
make.top.equalToSuperview().offset(safeArea.top + config.cardEdgeInsets.top)
make.bottom.equalToSuperview().inset(safeArea.bottom + config.cardEdgeInsets.bottom)
make.top.equalToSuperview().inset(safeAreaInsets.top)
make.bottom.equalToSuperview().inset(safeAreaInsets.bottom)
if isPortrait {
make.left.equalToSuperview().inset(safeArea.left + config.cardEdgeInsets.left)
make.right.equalToSuperview().inset(safeArea.right + config.cardEdgeInsets.right)
if isFullScreen {
safeAreaInsets.left += AppContext.safeAreaInsets.left
safeAreaInsets.right += AppContext.safeAreaInsets.right
}
make.left.equalToSuperview().inset(safeAreaInsets.left)
make.right.equalToSuperview().inset(safeAreaInsets.right)
} else {
make.left.equalToSuperview().inset(safeArea.left + config.cardEdgeInsets.left * 2)
make.right.equalToSuperview().inset(safeArea.right + config.cardEdgeInsets.right * 2)
if isFullScreen {
safeAreaInsets.left += safeAreaInsets.left + AppContext.safeAreaInsets.left
safeAreaInsets.right += safeAreaInsets.right + AppContext.safeAreaInsets.right
}
make.left.equalToSuperview().inset(safeAreaInsets.left)
make.right.equalToSuperview().inset(safeAreaInsets.right)
}
}
}

View File

@ -67,6 +67,15 @@ extension Sheet {
func translateIn(completion: (() -> Void)?) {
UIView.animateEaseOut(duration: config.animateDurationForBuildInByDefault) {
self._translateIn()
if self.config.stackDepthEffect {
if isPhonePortrait {
AppContext.appWindow?.transform = .init(translationX: 0, y: 8).scaledBy(x: 0.9, y: 0.9)
} else {
AppContext.appWindow?.transform = .init(scaleX: 0.92, y: 0.92)
}
AppContext.appWindow?.layer.cornerRadiusWithContinuous = 16
AppContext.appWindow?.layer.masksToBounds = true
}
} completion: { done in
completion?()
}
@ -75,6 +84,10 @@ extension Sheet {
func translateOut(completion: (() -> Void)?) {
UIView.animateEaseOut(duration: config.animateDurationForBuildOutByDefault) {
self._translateOut()
if self.config.stackDepthEffect {
AppContext.appWindow?.transform = .identity
AppContext.appWindow?.layer.cornerRadius = 0
}
} completion: { done in
completion?()
}
@ -87,7 +100,7 @@ extension Sheet {
func _translateOut() {
backgroundView.alpha = 0
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.screenEdgeInset)
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.windowEdgeInset)
}
}

View File

@ -29,6 +29,12 @@ public extension Toast {
customShared = callback
}
///
public var windowEdgeInset: CGFloat?
var windowEdgeInsetByDefault: CGFloat {
windowEdgeInset ?? 16
}
override var cardMaxWidthByDefault: CGFloat {
cardMaxWidth ?? 500
}

View File

@ -95,11 +95,12 @@ extension Toast: DefaultLayout {
// stacks
if contentStack.superview != contentView {
contentView.addSubview(contentStack)
let cardEdgeInsets = config.cardEdgeInsetsByDefault
contentStack.snp.remakeConstraints { make in
make.top.equalToSuperview().inset(config.cardEdgeInsets.top)
make.left.equalToSuperview().inset(config.cardEdgeInsets.left)
make.bottom.equalToSuperview().inset(config.cardEdgeInsets.bottom)
make.right.equalToSuperview().inset(config.cardEdgeInsets.right)
make.top.equalToSuperview().inset(cardEdgeInsets.top)
make.left.equalToSuperview().inset(cardEdgeInsets.left)
make.bottom.equalToSuperview().inset(cardEdgeInsets.bottom)
make.right.equalToSuperview().inset(cardEdgeInsets.right)
}
}
contentStack.insertArrangedSubview(infoStack, at: 0)

View File

@ -63,7 +63,8 @@ class ToastWindow: Window {
let config = toast.config
// frame
let width = CGFloat.minimum(AppContext.appBounds.width - config.cardEdgeInsets.left - config.cardEdgeInsets.right, config.cardMaxWidthByDefault)
let cardEdgeInsets = config.cardEdgeInsetsByDefault
let width = CGFloat.minimum(AppContext.appBounds.width - config.windowEdgeInsetByDefault - config.windowEdgeInsetByDefault, config.cardMaxWidthByDefault)
toast.view.frame.size = CGSize(width: width, height: config.cardMaxHeightByDefault)
toast.titleLabel.sizeToFit()
toast.bodyLabel.sizeToFit()
@ -113,6 +114,8 @@ class ToastWindow: Window {
toast.view.removeFromSuperview()
toast.removeFromParent()
toast.navEvents[.onViewDidDisappear]?(toast)
// window使windowwindow
window.isHidden = true
}
}
@ -128,7 +131,8 @@ fileprivate extension ToastWindow {
let config = window.toast.config
var y = window.frame.origin.y
if i == 0 {
y = max(AppContext.appWindow?.safeAreaInsets.top ?? config.margin, config.margin)
let topLayoutMargins = AppContext.appWindow?.layoutMargins.top ?? config.margin
y = max(topLayoutMargins - config.margin, config.margin)
} else {
if i - 1 < windows.count && i > 0 {
y = config.margin + windows[i-1].frame.maxY
@ -147,6 +151,7 @@ fileprivate extension ToastWindow {
updateToastsLayoutWorkItem?.cancel()
updateToastsLayoutWorkItem = DispatchWorkItem {
setToastWindowsLayout(windows: windows)
updateToastsLayoutWorkItem = nil
}
DispatchQueue.main.asyncAfter(deadline: .now()+0.001, execute: updateToastsLayoutWorkItem!)
}
@ -175,7 +180,8 @@ fileprivate extension Toast {
height = CGFloat.maximum(v.frame.maxY, height)
}
//
height += config.cardEdgeInsets.top + config.cardEdgeInsets.bottom
let cardEdgeInsets = config.cardEdgeInsetsByDefault
height += cardEdgeInsets.top + cardEdgeInsets.bottom
return height
}
}