|
| 1 | +// |
| 2 | +// ClockDialView.swift |
| 3 | +// Clock |
| 4 | +// |
| 5 | +// Created by Liu Chuan on 2020/11/03. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | + |
| 11 | +/// 时钟表盘视图 |
| 12 | +struct ClockDialView: View { |
| 13 | + |
| 14 | + /* |
| 15 | + 90度:15分钟,5分钟= 90/3,1分钟= 90/3/5 = 6° |
| 16 | + */ |
| 17 | + |
| 18 | + /// 当前时间 |
| 19 | + @Binding var currentTime: Time |
| 20 | + |
| 21 | + /// 是否为暗黑模式 |
| 22 | + @Binding var isDark: Bool |
| 23 | + |
| 24 | + /// 秒针的颜色(默认:Color.primary) |
| 25 | + @State var secondColor: Color = Color.red |
| 26 | + |
| 27 | + /// 表盘颜色(默认为白色) |
| 28 | + @State var dialColor: Color = Color("Color") |
| 29 | + |
| 30 | + /// 秒针的高度 |
| 31 | + private let secHeight: CGFloat = screenW / 2 - 20 |
| 32 | + |
| 33 | + |
| 34 | + var body: some View { |
| 35 | + |
| 36 | + ZStack { |
| 37 | + Circle() |
| 38 | + .fill(dialColor) |
| 39 | + .frame(width: screenW - 50, height: screenW - 50) |
| 40 | + |
| 41 | + Text("ROLEX") |
| 42 | + .foregroundColor(Color.primary) |
| 43 | + .font(.system(size: 25)) |
| 44 | + .offset(x: 0, y: -90) |
| 45 | + |
| 46 | + // 工作日 |
| 47 | + Text("\(getWeekdayWithNumber()) \(getday())") |
| 48 | + .fontWeight(.bold) |
| 49 | + .background(isDark ? Color.white : Color.black) |
| 50 | + .foregroundColor(isDark ? Color.black : Color.white) |
| 51 | + .offset(x: 70) |
| 52 | + |
| 53 | + // 表盘上的刻度 |
| 54 | + ForEach(0 ..< 60, id: \.self) { i in |
| 55 | + Rectangle() |
| 56 | + .fill(Color.primary) |
| 57 | + // 每小时:60 / 12 = 5 |
| 58 | + // (i % 5) == 0 : 小时刻度 |
| 59 | + .frame(width: (i % 5) == 0 ? 15 : 5, height: (i % 5) == 0 ? 15 : 5) |
| 60 | + .cornerRadius(i % 5 == 0 ? 15 / 2 : 0) |
| 61 | + .offset(y: (screenW - 110) / 2) |
| 62 | + .rotationEffect(.init(degrees: Double(i) * 6)) |
| 63 | + |
| 64 | + // 数字 |
| 65 | + if i % 5 == 0 { |
| 66 | + HourTexts(index: i, marginRatio: 3 / 8) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Hour |
| 71 | + Rectangle() |
| 72 | + .fill(Color.primary) |
| 73 | + .frame(width: 6.5, height: secHeight / 3) |
| 74 | + .offset(y: -secHeight / 3 / 2) |
| 75 | + .rotationEffect(.init(degrees: (Double(currentTime.hour) + (Double(currentTime.min) / 60)) * 30)) |
| 76 | + |
| 77 | + // Min |
| 78 | + Rectangle() |
| 79 | + .fill(Color.primary) |
| 80 | + .frame(width: 6, height: secHeight / 2) |
| 81 | + .offset(y: -secHeight / 2 / 2) |
| 82 | + .rotationEffect(.init(degrees: Double(currentTime.min) * 6)) |
| 83 | + |
| 84 | + // Sec |
| 85 | + Rectangle() |
| 86 | + .fill(secondColor) |
| 87 | + .frame(width: 2, height: secHeight) |
| 88 | + .offset(x: 0, y: -50) |
| 89 | + |
| 90 | + // 秒针上的2个圆 |
| 91 | + .overlay( |
| 92 | + Circle() |
| 93 | + .foregroundColor(secondColor) |
| 94 | + .frame(width: 12, height: 12) |
| 95 | + .offset(x: 0, y: -100) |
| 96 | + ) |
| 97 | + .overlay( |
| 98 | + Circle() |
| 99 | + .foregroundColor(secondColor) |
| 100 | + .frame(width: 10, height: 10) |
| 101 | + .offset(x: 0, y: secHeight / 2 - 50) |
| 102 | + ) |
| 103 | + // 旋转1分钟的6度 |
| 104 | + .rotationEffect(.init(degrees: Double(currentTime.sec) * 6)) |
| 105 | + |
| 106 | + // 中心圆 |
| 107 | + Circle() |
| 108 | + .fill(Color.primary) |
| 109 | + .frame(width: 15, height: 15) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// MARK: - Get date method |
| 115 | +extension ClockDialView { |
| 116 | + |
| 117 | + /// 根据数字获取工作日 |
| 118 | + /// - Parameter number: 数字 |
| 119 | + func getWeekdayWithNumber() -> String { |
| 120 | + |
| 121 | + /* |
| 122 | + 星期一,Mon.(全称Monday) |
| 123 | + 星期二,Tues.(全称Tuesday) |
| 124 | + 星期三,Wed.(全称Wednesday) |
| 125 | + 星期四,Thur.(全称Thursday) |
| 126 | + 星期五,Fri.(全称Friday) |
| 127 | + 星期六,Sat.(全程Saturday) |
| 128 | + 星期日 Sun.(全称Sunday) |
| 129 | + */ |
| 130 | + |
| 131 | + /// 创建日历对象 |
| 132 | + var calender = Calendar.current |
| 133 | + //默认情况下,日历未设置语言环境。 |
| 134 | + //如果您希望收到本地化的答案,请务必首先将locale属性设置为。Locale.autoupdatingCurrent |
| 135 | + calender.locale = Locale.autoupdatingCurrent |
| 136 | + |
| 137 | + /// 返回日期的周 |
| 138 | + let number = calender.component(.weekday, from: Date()) |
| 139 | + // 使用本地化字符串 |
| 140 | + switch number { |
| 141 | + case 1: |
| 142 | + return NSLocalizedString("Sun", comment: "") |
| 143 | + case 2: |
| 144 | + return NSLocalizedString("Mon", comment: "") |
| 145 | + case 3: |
| 146 | + return NSLocalizedString("Tues", comment: "") |
| 147 | + case 4: |
| 148 | + return NSLocalizedString("Wed", comment: "") |
| 149 | + case 5: |
| 150 | + return NSLocalizedString("Thur", comment: "") |
| 151 | + case 6: |
| 152 | + return NSLocalizedString("Fri", comment: "") |
| 153 | + case 7: |
| 154 | + return NSLocalizedString("Sat", comment: "") |
| 155 | + default: |
| 156 | + return "" |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// 获取当前几日 |
| 161 | + func getday() -> String { |
| 162 | + /// 创建日历对象 |
| 163 | + var calender = Calendar.current |
| 164 | + //默认情况下,日历未设置语言环境。 |
| 165 | + //如果您希望收到本地化的答案,请务必首先将locale属性设置为。Locale.autoupdatingCurrent |
| 166 | + calender.locale = Locale.autoupdatingCurrent |
| 167 | + // 获取多少号 |
| 168 | + let day = calender.component(.day, from: Date()) |
| 169 | + return "\(day)" |
| 170 | + } |
| 171 | + |
| 172 | +} |
| 173 | + |
0 commit comments