2

I have an View which I want to be able to drag, but only by 25 points in every direction.

Is this possible in SwiftUI ?

Right now I have something like this:

.offset(x: viewState.width, y: viewState.height)
    .gesture(
        DragGesture()
           .onChanged { value in
               self.viewState = ((value.translation.width > -25 && 
                                  value.translation.width < 25) ||
                                  (value.translation.height > -25 &&
                                  value.translation.height < 25)) ?
                                  value.translation :
                                  CGSize.zero
               }
           .onEnded { _ in
              self.viewState = CGSize.zero
           }
    )

But I am still able to drag it further than this if I drag slow.
Is it any way to drag it just a little bit and thats it ?

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • Are you using `viewState` to change the position or transform of the view that hosts the drag gesture? – rob mayoff Aug 30 '19 at 16:38
  • @robmayoff: change the position, forgot to show also this line: .offset(x: viewState.width, y: viewState.height) – Adrian Aug 30 '19 at 16:51
  • I fail to understand how this is a duplicate....I want to drag and disabled dragging while a distance is reached...like 25 points away. The other answer no way is trying to achieve this, so I am confused. – Adrian Aug 30 '19 at 18:51
  • Did you try creating the `DragGesture` with `coordinateSpace: .global` as the other answer suggests? Because I believe that is the answer here also, for the same reason described there. If `coordinateSpace: .global` doesn't fix your problem, post another comment and I'll reopen this question. – rob mayoff Aug 30 '19 at 20:41
  • You are right, I tried that, but I have removed my current code when trying, now it works if I just add .global with my current code. Sorry for the misunderstanding – Adrian Aug 31 '19 at 09:55
  • Well after further testing if I drag very slow, I can drag it as much as I can, so the .global setting is not really working – Adrian Aug 31 '19 at 11:09
  • You're more likely to get help if you post a complete, runnable example. – rob mayoff Sep 13 '19 at 04:07
  • This might help. https://stackoverflow.com/questions/59823840/limit-drag-to-circular-bounds-in-swiftui – Pulkit Vaid Apr 12 '20 at 06:31

0 Answers0