swiftui-拖动手势没有在hstack上激活带有按钮的hstack

发布于 2025-02-12 09:40:55 字数 467 浏览 4 评论 0原文

我有一个带有按钮的hStack,如这样的定义:

GeometryReader { proxy in
  HStack {
    Button("test")
      .frame(width: 100, height: 50)
    Button("test")
      .frame(width: 100, height: 50)
    Button("test")
      .frame(width: 100, height: 50)
  }
  .frame(width: proxy.size.width)
  .gesture(
    .onChanged { gesture in
    // do something here
    }
  )
}

如您所见,当拖动HSTACK时,我正在尝试执行一些功能。如果用户从HSTACK中的空白空间开始拖动,则可以工作,但是,如果拖动从按钮开始,则不会激活手势。我该如何解决?

I have an HStack with buttons in it defined like this:

GeometryReader { proxy in
  HStack {
    Button("test")
      .frame(width: 100, height: 50)
    Button("test")
      .frame(width: 100, height: 50)
    Button("test")
      .frame(width: 100, height: 50)
  }
  .frame(width: proxy.size.width)
  .gesture(
    .onChanged { gesture in
    // do something here
    }
  )
}

As you can see, I am trying to perform some function when the HStack is dragged. This works if the user drags starting from an empty space in the HStack, however, if the drag starts from the button, the gesture is not activated. How can I fix this?

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

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

发布评论

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

评论(1

豆芽 2025-02-19 09:40:55

您可以使用ontapgesture而不是按钮的视图。

尝试这样的事情:

@State private var count = 0 // To check if dragging works

GeometryReader { proxy in
    HStack {
        Text("test")
            .onTapGesture { // The action gesture
                //
            }
            .foregroundColor(.blue) // Colour it like the default button
        
        
        .frame(width: 100, height: 50)
        
        Text("\(count)")
    }
    .gesture(DragGesture(minimumDistance: 1) // The dragging gesture
        .onChanged { gesture in
            count += 1
        })
}

您可能还可以在拖动时尝试禁用该按钮,然后在拖动后启用。

祝你好运!

You can use a View with an onTapGesture instead of a button.

Try something like this:

@State private var count = 0 // To check if dragging works

GeometryReader { proxy in
    HStack {
        Text("test")
            .onTapGesture { // The action gesture
                //
            }
            .foregroundColor(.blue) // Colour it like the default button
        
        
        .frame(width: 100, height: 50)
        
        Text("\(count)")
    }
    .gesture(DragGesture(minimumDistance: 1) // The dragging gesture
        .onChanged { gesture in
            count += 1
        })
}

You could possibly also try and disabling the button when dragging and then enable when you're done dragging.

Good luck!

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