Skip to content
TheChatArchiveThe Chat Archive
Hugging Facezephyr-7b-beta· Hugging FaceHugging Face Arena Human Preference 55K

Write a swiftui view that displays an analogue clock, with hours minutes and second hands

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4-1106-preview’s answer.
  1. User

    Write a swiftui view that displays an analogue clock, with hours minutes and second hands

  2. Hugging Face Assistant

    To create an analogue clock using SwiftUI, you can follow these steps:

    1. Create a new SwiftUI project in Xcode.
    2. Add a new file to your project by going to File > New > File. Choose SwiftUI > View, and name it ClockView.
    3. Open ClockView.swift and add the following code to create the clock:
    import SwiftUI
    
    struct ClockView: View {
        let center: CGPoint = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
        let radius: CGFloat = 100
    
        var body: some View {
            GeometryReader { geometry in
                ZStack {
                    Circle()
                        .stroke(Color.gray, lineWidth: 2)
    
                    Circle()
                        .trim(from: 0, to: CGFloat(Date().hour % 12 == 0 ? 12 : Date().hour % 12))
                        .stroke(Color.red, style: StrokeStyle(lineWidth: 6))
                        .rotationEffect(Angle(degrees: Double(Date().minute) * 6))
                        .animation(Animation.linear(duration: 1.0))
    
                    Circle()
                        .trim(from: 0, to: CGFloat(Date().second % 30))
                        .stroke(Color.blue, style: StrokeStyle(lineWidth: 4))
                        .rotationEffect(Angle(degrees: Double(Date().second) * 6))
                        .animation(Animation.linear(duration: 1.0))
    
                    Text("\(Date().hour):\(Date().minute):\(Date().second)")
                        .font(.title)
                        .foregroundColor(.white)
                        .position(center)
                }
                .coordinateSpace(name: "ClockSpace")
            }
            .frame(width: 200, height: 200)
        }
    }
    
    struct ClockView_Previews: PreviewProvider {
        static var previews: some View {
            ClockView()
        }
    }
    

    Let's go through the code step-by-step:

    1. We declare the ClockView struct and define a few constants:
    • center: the center point of the clock.
    • radius: the radius of the clock.
    1. In the body property, we create a GeometryReader to get the size of the view.

    2. We create a ZStack (short for "stack") that contains the following views:

    • A gray circle that represents the clock's outer ring.
    • A red circle that represents the hour hand. The trim property is used to define the length of the hand based on the current time, and the rotationEffect and animation properties are used to animate the hand's rotation.
    • A blue circle that represents the second hand. The trim property is used to define the length of the hand based on the current time, and the rotationEffect and animation properties are used to animate the hand's rotation.
    1. We also add a label that displays the current time, positioned in the center of the view.

    2. We create a ClockSpace coordinate space using the coordinateSpace property of the view. This is used to position the Text label in the center of the view, even if its size changes.

    3. Finally, we create a preview of the clock using the ClockView_Previews struct.

    This code should create an analogue clock that displays the current time in real-time.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.