A Clock UI for SwiftUI

SwiftClockUI

Clock UI for SwiftUI

This library has been tested

  • โœ… ๐Ÿ’ป macOS Catalina 10.15.3
  • โœ… ๐Ÿ“ฑ iOS 13.3
  • โœ… ๐Ÿ“ฑ iOS 14.0 Beta (some snapshots tests are failing but they are all correct ๐Ÿ˜…)

Bind a date

struct ContentView: View {
    @State private var date = Date()

    var body: some View {
        ClockView().environment(\.clockDate, $date)
    }
}

Simply set .environment(\.clockDate, $date) $date has to be a binding. If you want something constant (just for showing the time), you could pass .constant(yourDate)

  • Arms move when date are set (take hour and minute in account)
  • Move the Arms change the date (hour and minute depending on which arm youโ€™ve moved)

Change Clock style

There is 4 different clock style:

Style Picture
Classic Clock View with Classic style
Art Nouveau Clock View with Art Nouveau style
Drawing Clock View with Drawing style
Steampunk Clock View with Steampunk style

To set the style: .environment(\.clockStyle, .steampunk) for Steampunk style for instance.

struct ContentView: View {
    @State private var clockStyle: ClockStyle = .classic

    var body: some View {
        ClockView().environment(\.clockStyle, clockStyle)
    }
}

\.clockStyle is typed as enum ClockStyle which is Identifiable, CaseIterable, and has a convenient method to get the description (in English): public var description: String

Itโ€™s very useful when you want to iterate over this enum to let the user choose the clock style, for instance you can easily do something like this:

struct StylePicker: View {
    @Binding var clockStyle: ClockStyle

    var body: some View {
        Picker("Style", selection: clockStyle) {
            ForEach(ClockStyle.allCases) { style in
                Text(style.description).tag(style)
            }
        }
        .pickerStyle(SegmentedPickerStyle())
    }
}

Change elements color

You can also change the color of Clock elements. Again with changing some .environment keys.

ClockView()
    .environment(\.clockArmColors, ClockArmColors(
        minute: .red,
        hour: .blue
    ))
    .environment(\.clockBorderColor, .orange)
    .environment(\.clockIndicatorsColor, .green)

In light mode, you could expect a result like this:

Clock View with Classic style and some colors changed

App using this library

TODO

  • ๐Ÿ‘† Add a bigger zone for dragging arms, itโ€™s not easy with the mouse on macOS
    * Use the new .hover and .hoverEffect from Swift 5.2 and Xcode 11.4
  • ๐Ÿ‘พ Add a smooth animation while resizing the window on macOS

Download Details:

Author: renaudjenny

Source Code: https://github.com/renaudjenny/SwiftClockUI

#swiftui #swift #ios #mobile-apps

A Clock UI for SwiftUI
5.90 GEEK