2

I am developing a selection dial, and it is working properly which means that the dial is rotating in the same direction as the finger is moving but an issue arises as soon the finger reaches somehow near the bottom portion, the dial start rotating in opposite direction.

enter image description here

VStack {
    Image(icons_list[i].iconNamee)
        .resizable()
        .rotationEffect(Angle(degrees: -180))
        .frame(width: UIScreen.screenWidth / 12, height: UIScreen.screenWidth / 12, alignment: .center)
        .aspectRatio(contentMode: .fit)
        .padding()
        .offset(y:(UIScreen.screenWidth / 3.75))
        .rotationEffect(Angle(degrees: (Double(i) * iconDistanceRatio) ))
        .rotationEffect(Angle(degrees: 180))
}
.rotationEffect(Angle(degrees: Double(totalRotation.width)))

.gesture(
    DragGesture()
        .onChanged { value in
            
            totalRotation.width = value.translation.width +         currentRotation.width
         
            
            if totalRotation.width < 0 {
                let val = Int(abs(totalRotation.width / iconDistanceRatio))
                if val >= (icons_list.count){
                    selectedIndex = 0
                    totalRotation = CGSize.zero
                    
                }else{
                    self.selectedIndex = val
                }
            }
            else{
                let val = (icons_list.count - 1) - Int(abs(totalRotation.width / iconDistanceRatio))
                if Int(abs(totalRotation.width / iconDistanceRatio)) >= (icons_list.count){
                    selectedIndex = 0
                    totalRotation = CGSize.zero
                    
                }else{
                    self.selectedIndex = val
                }
            }
        }
        .onEnded { value in
            currentRotation = totalRotation
            
        }
)
Shahrukh
  • 29
  • 3
  • Change ```let val = (icons_list.count - 1) - Int(abs(totalRotation.width / iconDistanceRatio))``` to ```let val = (icons_list.count /2) + Int(abs(totalRotation.width / iconDistanceRatio))``` – udi Apr 28 '22 at 05:59
  • Did the above comment work ? – udi Apr 28 '22 at 06:06
  • You must do a little more : check in which part of screen you are : top,bottom,left,right to know how to interpret drag. Top and bottom have opposite x directions, left and right have opposite y direction. Rotation must take this into account. – Ptit Xav Apr 28 '22 at 10:43
  • Check [here](https://stackoverflow.com/a/71307442/13944750) – Ptit Xav Apr 28 '22 at 10:47
  • 1
    Does this answer your question? [Rotation of view goes backwards with drag gesture](https://stackoverflow.com/questions/71307175/rotation-of-view-goes-backwards-with-drag-gesture) – Ptit Xav Apr 28 '22 at 10:49

0 Answers0