Created
March 30, 2022 12:46
-
-
Save SarahAlsharif/b8d855d6e1143cbf2825227b5aace7e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct WheelView: View { | |
| // Circle Radius | |
| @State var radius : Double = 150 | |
| // Direction of swipe | |
| @State var direction = Direction.left | |
| // index of the number at the bottom of the circle | |
| @State var chosenIndex = 0 | |
| // degree of circle and hue | |
| @Binding var degree : Double | |
| // @State var degree = 90.0 | |
| let array : [myVal] | |
| let circleSize : Double | |
| func moveWheel(index : Int) { | |
| withAnimation(.spring()) { | |
| if direction == .left { | |
| degree += Double((360 + (chosenIndex))/array.count) | |
| if chosenIndex == 0 { | |
| chosenIndex = array.count-1 | |
| } else { | |
| chosenIndex -= 1 | |
| } | |
| } else { | |
| degree -= Double((360 + (chosenIndex))/array.count) | |
| if chosenIndex == array.count-1 { | |
| chosenIndex = 0 | |
| } else { | |
| chosenIndex += 1 | |
| } | |
| } | |
| } | |
| } | |
| var body: some View { | |
| ZStack { | |
| let anglePerCount = Double.pi * 2.0 / Double(array.count) | |
| let drag = DragGesture() | |
| .onEnded { value in | |
| if value.startLocation.x > value.location.x + 10 { | |
| direction = .left | |
| } else if value.startLocation.x < value.location.x - 10 { | |
| direction = .right | |
| } | |
| moveWheel(index: chosenIndex) | |
| } | |
| // MARK: WHEEL STACK - BEGINNING | |
| ZStack { | |
| Circle().fill(EllipticalGradient(colors: [.orange,.yellow])) | |
| .hueRotation(Angle(degrees: degree)) | |
| ForEach(0 ..< array.count) { index in | |
| let angle = Double(index) * anglePerCount | |
| let xOffset = CGFloat(radius * cos(angle)) | |
| let yOffset = CGFloat(radius * sin(angle)) | |
| Text("\(array[index].val)") | |
| .rotationEffect(Angle(degrees: -degree)) | |
| .offset(x: xOffset, y: yOffset ) | |
| .font(Font.system(chosenIndex == index ? .title : .body, design: .monospaced)) | |
| } | |
| } | |
| .rotationEffect(Angle(degrees: degree)) | |
| .gesture(drag) | |
| .onAppear() { | |
| radius = circleSize/2 - 30 // 30 is for padding | |
| } | |
| // MARK: WHEEL STACK - END | |
| } | |
| .frame(width: circleSize, height: circleSize) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment