Swiftui Draggasture是冻结的

发布于 2025-02-06 16:31:08 字数 1327 浏览 3 评论 0原文

我试图用一个手势做一个可分解的视图。

问题是,当我向左或向右移动手势对象时,应用程序将通过100%处理器使用冻结。它是两个文本视图之间的循环。

我做错了什么,我该如何正确?

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

视图:

“

debugger:

“

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2025-02-13 16:31:08

对我来说,解决方案是明确将手势的coordinatespace设置为.global

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

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

DragGesture(coordinateSpace: .global)
    .onChanged { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文