宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
//  EKAttributes+BackgroundStyle.swift
//  SwiftEntryKit
//
//  Created by Daniel Huri on 4/21/18.
//  Copyright (c) 2018 huri000@gmail.com. All rights reserved.
//
 
import UIKit
 
public extension EKAttributes {
    
    /** The background style property */
    enum BackgroundStyle: Equatable {
        
        /** Blur style for light and dark modes */
        public struct BlurStyle: Equatable {
            
            public static var extra: BlurStyle {
                return BlurStyle(light: .extraLight, dark: .dark)
            }
            
            public static var standard: BlurStyle {
                return BlurStyle(light: .light, dark: .dark)
            }
            
            @available(iOS 10.0, *)
            public static var prominent: BlurStyle {
                return BlurStyle(light: .prominent, dark: .prominent)
            }
            
            public static var dark: BlurStyle {
                return BlurStyle(light: .dark, dark: .dark)
            }
            
            let light: UIBlurEffect.Style
            let dark: UIBlurEffect.Style
            
            public init(style: UIBlurEffect.Style) {
                self.light = style
                self.dark = style
            }
            
            public init(light: UIBlurEffect.Style, dark: UIBlurEffect.Style) {
                self.light = light
                self.dark = dark
            }
            
            /** Computes a proper `UIBlurEffect.Style` instance */
            public func blurStyle(for traits: UITraitCollection,
                                  mode: EKAttributes.DisplayMode) -> UIBlurEffect.Style {
                switch mode {
                case .inferred:
                    if #available(iOS 13, *) {
                        switch traits.userInterfaceStyle {
                        case .light, .unspecified:
                            return light
                        case .dark:
                            return dark
                        @unknown default:
                            return light
                        }
                    } else {
                        return light
                    }
                case .light:
                    return light
                case .dark:
                    return dark
                }
            }
            
            public func blurEffect(for traits: UITraitCollection,
                                   mode: EKAttributes.DisplayMode) -> UIBlurEffect {
                return UIBlurEffect(style: blurStyle(for: traits, mode: mode))
            }
        }
        
        /** Gradient background style */
        public struct Gradient {
            public var colors: [EKColor]
            public var startPoint: CGPoint
            public var endPoint: CGPoint
            
            public init(colors: [EKColor],
                        startPoint: CGPoint,
                        endPoint: CGPoint) {
                self.colors = colors
                self.startPoint = startPoint
                self.endPoint = endPoint
            }
        }
        
        /** Visual Effect (Blurred) background style */
        case visualEffect(style: BlurStyle)
        
        /** Color background style */
        case color(color: EKColor)
        
        /** Gradient background style */
        case gradient(gradient: Gradient)
        
        /** Image background style */
        case image(image: UIImage)
        
        /** Clear background style */
        case clear
        
        /** == operator overload */
        public static func == (lhs: EKAttributes.BackgroundStyle,
                               rhs: EKAttributes.BackgroundStyle) -> Bool {
            switch (lhs, rhs) {
            case (visualEffect(style: let leftStyle),
                  visualEffect(style: let rightStyle)):
                return leftStyle == rightStyle
            case (color(color: let leftColor),
                  color(color: let rightColor)):
                return leftColor == rightColor
            case (image(image: let leftImage),
                  image(image: let rightImage)):
                return leftImage == rightImage
            case (gradient(gradient: let leftGradient),
                  gradient(gradient: let rightGradient)):
                for (leftColor, rightColor) in zip(leftGradient.colors, rightGradient.colors) {
                    guard leftColor == rightColor else {
                        return false
                    }
                }
                return leftGradient.startPoint == rightGradient.startPoint &&
                       leftGradient.endPoint == rightGradient.endPoint
            case (clear, clear):
                return true
            default:
                return false
            }
        }
    }
}