簡體   English   中英

SwiftUI DragGesture 凍結

[英]SwiftUI DragGesture is freeze

我試圖用手勢做一個可調整大小的視圖。

問題是,當我向左或向右移動手勢 object 時,應用程序將凍結,處理器使用率為 100%。 它是兩個Text視圖之間的循環。

我做錯了什么,我該如何糾正?

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)
    }
}

看法:

看法

調試器:

調試器

對我來說,解決方案是將手勢的coordinateSpace顯式設置為.global

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM