SwiftUI - DragGesture and .offset drops a lot of frames

Jul 24, 2022 7:22 AM
Jul 24, 2022 6:57 AM

How to drag a view that I know so far is using DragGesture and offset modifier.

It looks working well in Xcode’s preview function, it runs quite smoothly. However, we will get to notice that does not achieve on the device.

On the device, it moves with so many frame drops.

Implementations are here

      minimumDistance: 0,
      coordinateSpace: .local
    .onChanged({ value in
      position = value.translation

It seems updating position property inside .onChanged triggers so many update. I can relate to this. State triggers update with calling body property.

Use animation

From some considerations, I got an idea that uses a short animation for each update.

.onChanged({ value in
  withAnimation(.interactiveSpring()) {
    position = value.translation

Is this workaround? Maybe it’s a kind of better way including design wise

I’m not sure this approach or workaround we could say is a better way. Since using animation for each, dragging behaves occurs a bit sliding. But this is good actually, recent iOS uses this kind of animations even the user dragging (except sheet UI)