Getting view geometry during a DragGesture?

With a UIGestureRecognizer we can always access the `view` that owns the recognizer. Therefore we can determine where the touch is *relative* to that view's bounds, as well as relative to any other view via `convert(_:to:)`.


`DragGesture.Value` exposes: `location`, and `translation` however these are relative to the touched view and there is no way to access that view's geometry during the callback. Moreover, you can't access any other view's geometry either (I actually don't need the touched view's geometry, but the containing view's).


I'm already using `GeometryReader` and so I tried saving the needed geometry while building my view. However, using `@State var size: CGSize` and then assigning it in the reader's block resulted in an infinite loop.

Replies

It seems obvious now that `@State` was the wrong idea. A wrapper of some other type was needed instead. I was able to get around this issue by using the following:


@propertyDelegate
class ReferenceValue {
  var value: Value
  init(initialValue: Value) {
    self.value = initialValue
  }
}


and then


@ReferenceValue var width: CGFloat = 0 // Save this from the GeometryProxy during layout


I would be curious to know whether there is a more idiomatic way to do this though.