SwiftUI:拖动逻辑错误还是这是一个错误? [英] SwiftUI: Error in dragging logic or is this a bug?

查看:70
本文介绍了SwiftUI:拖动逻辑错误还是这是一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SwiftUI处于beta测试阶段,因此也许是一个错误,但是我在其他YouTube视频中看到了类似的效果,所以事实并非如此,测试非常简单.我正在创建一个可以水平拖动的圆圈.

SwiftUI is in beta, so maybe this is a bug, but I've seen something like this working in others YouTube videos so perhaps it's not, the test is pretty simple. I'm creating a circle I can drag around on horizontally.

import SwiftUI

struct ContentView : View {
    @State private var location = CGPoint.zero
    var body: some View {
        return Circle()
            .fill(Color.blue)
            .frame(width: 300, height: 300)
            .gesture(
                DragGesture(minimumDistance: 10)
                    .onChanged { value in
                        print("value.location")
                        print(value.location)
                        self.location = CGPoint(
                            x: value.location.x,
                            y: 0)
                }
        )
            .offset(x: self.location.x, y: self.location.y)
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

这会导致以下行为:

据我所知,DragGesture onChanged回调中的value.location值不应在这样的数字之间波动.查看日志,到处都是数字.我想念什么吗?

As far as I can tell, the value.location value in the DragGesture onChanged callback shouldn't be fluctuating between numbers like this. Looking at the logs the numbers are all over the place. Am I missing something?

推荐答案

DragGesture的默认CoordinateSpace.local,它是Circle内部的坐标空间.移动Circle会发生什么?由于手指不动,因此Circle的几何形状中的手指位置突然改变,这导致Circle再次移动.重复广告恶心.

DragGesture's default CoordinateSpace is .local, which is the coordinate space inside your Circle. What happens when you move the Circle? Since your finger doesn't move, the location of your finger in the Circle's geometry suddenly changes, which causes the Circle to move again. Repeat ad nauseum.

尝试使用CoordinateSpace.global:

DragGesture(minimumDistance: 10, coordinateSpace: .global)

您可能还希望使用value.translation而不是value.location来避免手指放下时的初始跳动.

You'll probably also want to use value.translation instead of value.location to avoid the initial jump when you put your finger down.

这篇关于SwiftUI:拖动逻辑错误还是这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆