简体   繁体   中英

SwiftUI DragGesture is freeze

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:

看法

Debugger:

调试器

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

DragGesture(coordinateSpace: .global)
    .onChanged { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM