TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Circular slider to continue counting after 360 degrees

Forums > SwiftUI

Hi All

I aim to create a circular slider which you slide to increase a value - in the current code 60 secs for 360 degrees. Unfortunately it stops after 360 degrees or goes back to zero again. I am searching for a solution that I can slide several times and the value continues to increase. Any idea or hint very much appreciated.

Thank you so much in advance Torsten

import SwiftUI

struct ContentView_orig: View {
    var body: some View {

        NavigationView{

            SetTimerView()
                .preferredColorScheme(.dark)
                .navigationTitle("SixTimes")
        }
    }
}

struct SetTimerView : View {

    @State var size = UIScreen.main.bounds.width - 100
    @State var progress : CGFloat = 0
    @State var angle : Double = 0

    var body: some View{

        VStack{

            ZStack{

                Circle()
                    .stroke(Color("stroke"), style: StrokeStyle(lineWidth: 55, lineCap: .butt, lineJoin: .round))
                    .frame(width: size, height: size)

                // progress....
                Circle()
                    .trim(from: 0, to: progress)
                    .stroke(Color(hue: 0.352, saturation: 0.997, brightness: 1.0),style: StrokeStyle(lineWidth: 55, lineCap: .butt, lineJoin: .round))
                    .frame(width: size, height: size)
                    .rotationEffect(.init(degrees: -90))

                // Static little circle at 0 degrees / starting position
                Circle()
//                    .fill(Color("stroke"))
                    .fill(Color.green)
                    .frame(width: 55, height: 55)
                    .offset(x: size / 2)
                    .rotationEffect(.init(degrees: -90))

                // Drag Circle...
                Circle()
                    .fill(Color.white)
                    .frame(width: 55, height: 55)
                    .offset(x: size / 2)
                    .rotationEffect(.init(degrees: angle))
                // adding gesture...
                    .gesture(DragGesture().onChanged(onDrag(value:)))
                    .rotationEffect(.init(degrees: -90))

                // Here the time is shown in HH:MM:SS
                Text("Time (s): " + String(format: "%.0f", progress * 60))
                    .font(.title)
                    .fontWeight(.medium)
                    .foregroundColor(Color.red)
            }
        }
    }

    func onDrag(value: DragGesture.Value){

        // calculating radians...

        let vector = CGVector(dx: value.location.x, dy: value.location.y)

        // since atan2 will give from -180 to 180...
        // eliminating drag gesture size
        // size = 55 => Radius = 27.5...
        let radians = atan2(vector.dy - 27.5, vector.dx - 27.5)

        // converting to angle...
        var angle = radians * 180 / .pi

        // simple technique for 0 to 360...
        // eg = 360 - 176 = 184...
        if angle < 0{angle = 360 + angle}

        // makes the sliding of the progress circle a bit smoother
        withAnimation(Animation.linear(duration: 0.15)){

            // progress...
            let progress = angle / 360
            self.progress = progress
            self.angle = Double(angle)
        }
    }
}

struct ContentView_orig_Previews: PreviewProvider {
    static var previews: some View {
        ContentView_orig()
    }
}

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.