4

I tried to do a resizable view with a gesture.

The problem is that when I move my gesture object left or right the app will be freeze with 100% processor usage. It is the loop between two Text views.

What did I do wrong, and how do I make this correct?

struct TestView2 : View {
    @State private var width : CGFloat = 400.0
        
    var body : some View {
        VStack {
            ZStack(alignment: .bottomTrailing) {
                    
                Text("\(width)")
                    .frame(width: width)
                Text(":")
                    .frame(width: 10, height: 30)
                    .background(.bar)
                    .gesture(
                        DragGesture()
                            .onChanged { value in  
                                width = max(100, width + value.translation.width)
                                print(width)
                            }
                    )
            }
            .frame(width: width, height: 30, alignment: .topLeading)
            .border(.gray, width: 1)
            .background(.green)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
    }
}

View:

view

Debugger:

debugger

ZGski
  • 2,398
  • 1
  • 21
  • 34
Nico
  • 51
  • 4
  • How are you testing this. What Xcode version, what IOS/MacOS Version are you targeting. This is working just fine for me. Xcode 13.3 IOS, IOS 15.4 Iphone 11 Simulator – burnsi Jun 11 '22 at 14:22
  • @burnsi I use Xcode 13.4.1 on macOS Monterey 12.4 and test this case for macOS. – Nico Jun 11 '22 at 14:29
  • and It still happens with Xcode 14.3 on macOS Ventura 13.3.1 – Marcin Apr 12 '23 at 19:57

1 Answers1

2

For me, the solution was to explicitly set the gesture's coordinateSpace to .global:

DragGesture(coordinateSpace: .global)
    .onChanged { ... }
ZGski
  • 2,398
  • 1
  • 21
  • 34