BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Please help with animation

Forums > SwiftUI

I am trying to use a swipe gesture to rotate an image on screen. With the code I have, the first swipe works properly (lets say left). If I swipe that direction again (left again), it works properly. As long as I continue to swipe in that direction, it works. If I swipe a different direction (this time right), it partially animates and then bounces back. When I swipe that direction again (right again), it works properly and will continue to work as long as I stay in that direction.

Any time I change direction of swipe, I get the bounce back.

I am sure I am missing a basic principle of animation but just can't quite get it. After several hours of play, I am just banging my head trying to get this straight.

Here is the code:

struct HomeView: View {
    @State private var isRotating: Bool = false

    @State private var angle: Angle = Angle(degrees: 0)
    @State private var x: Double = 0.0
    @State private var y: Double = 0.0
    @State private var z: Double = 0.0

    var body: some View {
        Image(systemName: "trash")
            .font(.system(size: 96))

            .rotation3DEffect(
                angle,
                axis: (x: x, y: y, z: z),
                anchor: .center,
                anchorZ: 0,
                perspective: 1)

            .animation(.easeOut(duration: 1), value: isRotating)

            .gesture(DragGesture(minimumDistance: 3.0, coordinateSpace: .local)
                .onEnded { value in
                    print(value.translation)
                    switch(value.translation.width, value.translation.height) {
                    case (...0, -30...30):
                        print("left swipe")
                        x = 0.0
                        y = -1.0
                        z = 0.0
                    case (0..., -30...30):
                        print("right swipe")
                        x = 0.0
                        y = 1.0
                        z = 0.0
                    case (-100...100, ...0):
                        print("up swipe")
                        x = 1.0
                        y = 0.0
                        z = 0.0
                    case (-100...100, 0...):
                        print("down swipe")
                        x = -1.0
                        y = 0.0
                        z = 0.0
                    default:
                        print("Do Nothing")
                    }
                    angle += Angle(degrees: 360)
                    isRotating.toggle()
                }
            )
    }
}

   

Does anyone have any idea on this one?

   

Hello @Icemonster13

I have made some changes to your logic. Now it works once when I use left/right or up/down

for the transition from x to y-axis (or vice versa) an additional query must be made. I haven't found this yet, but I hope this helps a bit

struct HomeView: View { @State private var isRotating: Bool = false

@State private var angle = 0.0 //: Angle = Angle(degrees: 0) @State private var x: Double = 0.0 @State private var y: Double = 0.0 @State private var z: Double = 0.0

var body: some View { Image(systemName: "trash") .font(.system(size: 96))

  .rotation3DEffect(
    .degrees(angle),
    axis: (x: x, y: y, z: z))/*,
    anchor: .center,
    anchorZ: 0,
    perspective: 1)

*/ .animation(.easeOut(duration: 10), value: isRotating)

  .gesture(DragGesture(minimumDistance: 3.0, coordinateSpace: .local)
    .onEnded { value in
      print(value.translation)
      switch(value.translation.width, value.translation.height) {
        case (...0, -30...30):
          print("left swipe")
          //x = 0.0
          y = -1.0
          //z = 0.0
          angle += 360
        case (0..., -30...30):
          print("right swipe")
          //x = 0.0
          y = -1.0
          //z = 0.0
          angle -= 360
      case (-100...100, ...0):
          print("up swipe")
          x = -1.0
         // y = 0.0
          //z = 0.0
           angle -= 360
        case (-100...100, 0...):
          print("down swipe")
          x = -1.0
         // y = 0
          //z = 0
          angle += 360
          //y = 0.0
          //z = 0.0
        default:
          print("Do Nothing")
      }
      //angle += Angle(degrees: 360)
      //angle += 360
      isRotating.toggle()
    }
  )

} }

Sorry but somehow I never get the examples transferred so nicely

   

Thank you @Marcel. This got me much closer and I wasn't too far off.

   

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.