我有一个"按钮",我需要使用.offset(y: someVal)偏置到一个不同的位置.

我有我喜欢的视图布局,但命中区域似乎在应用偏移之前的区域.

这是我的代码:

    var body: some View {
        ZStack {
            ZStack {
                Circle()
                    .foregroundColor(.white)
                Image(systemName: "someIcon")
                    .foregroundColor(myColor)
            }
            .frame(width: centerBtnRadius * 2, height: centerBtnRadius * 2)
            .overlay(
                RoundedRectangle(cornerRadius: centerBtnRadius)
                    .stroke(myColor, lineWidth: 1)
            )
            .onTapGesture {
                //...
            }
            .onLongPressGesture(perform: {
                //...
            })
            .offset(y: centerBtnOffsetY)
            
            VStack {
                Spacer()
                Text(labelText)
                    .scaledToFit()
            }

        }
        .frame(width: Constants.tbItemWidth, height: Constants.tbItemHeight)
    }

现在,我已经try 将修饰符放在嵌套ZStack上的其他修饰符之前,但没有成功.它也必须在.overlay修改器之后,该修改器将我的边框定位在Circle()周围.(如果边界需要成为解决方案的一部分,我也愿意接受让它变得不同的 idea !)

我的问题是:

我如何确保我的按下手势被整个圆元素捕捉到,在我用.offset修饰符重新定位它之后??

推荐答案

您需要将偏移修改器从点击手势修改器之后移动到之前.通过这样做,轻击手势修改器将考虑"轻击区域"的偏移量.

var body: some View {
    ZStack {
        ZStack {
            Circle()
                .foregroundColor(.white)
            Image(systemName: "someIcon")
                .foregroundColor(myColor)
        }
        .frame(width: centerBtnRadius * 2, height: centerBtnRadius * 2)
        .overlay(
            RoundedRectangle(cornerRadius: centerBtnRadius)
                .stroke(myColor, lineWidth: 1)
        )
        .offset(y: centerBtnOffsetY) // Changed line
        .onTapGesture {
            //...
        }
        .onLongPressGesture(perform: {
            //...
        })
        
        VStack {
            Spacer()
            Text(labelText)
                .scaledToFit()
        }

    }
    .frame(width: Constants.tbItemWidth, height: Constants.tbItemHeight)
}

Ios相关问答推荐

如何在SwiftUI中扩展 colored颜色 超出顶部边缘

通过在SWIFT中不起作用的泛型传递协议一致性类型

在iOS 17脉冲符号效果不起作用的情况下制作UIBarButtonItem动画(没有UIImageView)

如何创建具有SPM依赖项的二进制XCFramework?

如何使用参与者允许并行读取,但阻止对SWIFT中资源的并发读取和写入?

视图未更新以在按下按钮时显示另一个视图

SwiftData共享扩展阻止应用程序打开SQLite文件

Xcode 15中的版本控制发生了什么?

使用 Objective-C 创建 iOS 框架

NSError 错误领域: "CBATTErrorDomain" 代码: 128 0x0000000282b111a0

将 URLCache 子类与 URLSession 一起使用

在 Swift 中 @MainActor 似乎被 withCheckedContinuation 而不是其他异步调用继承是巧合吗?

Swift Combine 防止初始值触发接收器并同时防止重复?

iOS SwiftUI - 使用 PhotosPicker 从图库中 Select 图像或视频

iOS 16 堆栈视图中按钮的奇怪动画

无法初始化 FirebaseApp - Flutter

禁用 UITextField 的自动更正

标题为 UIImage 的导航栏

如何在 Swift 中将新单元格插入 UITableView

什么是强属性属性