简体   繁体   English

SwiftUI DragGesture 冻结

[英]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.问题是,当我向左或向右移动手势 object 时,应用程序将冻结,处理器使用率为 100%。 It is the loop between two Text views.它是两个Text视图之间的循环。

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 :对我来说,解决方案是将手势的coordinateSpace显式设置为.global

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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