为什么旋转矩形上未触发SwiftUI onTapGuesture{}修饰符

为什么旋转矩形上未触发SwiftUI onTapGuesture{}修饰符,swiftui,shapes,Swiftui,Shapes,如果我创建一个100宽20高的矩形,然后将其旋转90度,如果我单击顶部或底部附近的矩形,则不会调用onTapGuesture{}修改器 contentShape()似乎与旋转的矩形不对应 let fixView = Rectangle() .rotation(self.fixture.currentAngle) .fill(self.fixture.color) .overlay(Rectangle().rotation(s

如果我创建一个100宽20高的矩形,然后将其旋转90度,如果我单击顶部或底部附近的矩形,则不会调用onTapGuesture{}修改器

contentShape()似乎与旋转的矩形不对应

let fixView = Rectangle()
            .rotation(self.fixture.currentAngle)
            .fill(self.fixture.color)
            .overlay(Rectangle().rotation(self.fixture.currentAngle).stroke(self.fixture.borderColor, lineWidth: self.fixture.borderWidth).frame(width: overlayWidth, height: overlayHeight))
            .frame(width: width, height: height)
            .position(x: x, y: y )

            .onTapGesture {
                GlobalData.shared.selectedFixture = self.fixture.object
        }

我发现.onHover{}也会有同样的想法-它只是在鼠标位于形状框架矩形上方时触发。

旋转不会更改接受手势的原始布局框架,因此必须使用
.contentShape
明确执行此操作,如下所示

Rectangle()
    .rotation(self.fixture.currentAngle)
    .fill(self.fixture.color)
    .overlay(Rectangle().rotation(self.fixture.currentAngle).stroke(self.fixture.borderColor, lineWidth: self.fixture.borderWidth).frame(width: overlayWidth, height: overlayHeight))
    .frame(width: width, height: height)
    // << here !! NB to not put this line after .position()
    .contentShape(Rectangle().rotation(self.fixture.currentAngle))  
    .position(x: x, y: y )

    .onTapGesture {
        GlobalData.shared.selectedFixture = self.fixture.object
}
Rectangle()
.旋转(自固定装置.当前角度)
.填充(自固定装置.颜色)
.overlay(矩形().rotation(self.fixture.currentAngle).stroke(self.fixture.borderColor,线宽:self.fixture.borderWidth).边框(宽度:overlayWidth,高度:overlayHeight))
.框架(宽度:宽度,高度:高度)

//旋转
不会改变接受手势的原始布局框架,因此您必须使用
.contentShape
明确执行此操作,如下所示

Rectangle()
    .rotation(self.fixture.currentAngle)
    .fill(self.fixture.color)
    .overlay(Rectangle().rotation(self.fixture.currentAngle).stroke(self.fixture.borderColor, lineWidth: self.fixture.borderWidth).frame(width: overlayWidth, height: overlayHeight))
    .frame(width: width, height: height)
    // << here !! NB to not put this line after .position()
    .contentShape(Rectangle().rotation(self.fixture.currentAngle))  
    .position(x: x, y: y )

    .onTapGesture {
        GlobalData.shared.selectedFixture = self.fixture.object
}
Rectangle()
.旋转(自固定装置.当前角度)
.填充(自固定装置.颜色)
.overlay(矩形().rotation(self.fixture.currentAngle).stroke(self.fixture.borderColor,线宽:self.fixture.borderWidth).边框(宽度:overlayWidth,高度:overlayHeight))
.框架(宽度:宽度,高度:高度)

//不为我工作。请注意,ZStack中有许多形状-如果我添加contentShape(),则无论我在容器视图上单击的位置如何,视图中的顶部形状似乎始终处于选中状态固定-我将修改的“contentShape()”移动到“.position()”修改器之前。“.position()”修饰符返回一个与父对象大小相同的矩形。如何让onHover{}处理该形状-它似乎忽略了contentShape()@DuncanGroenewald,我尝试了onHover,但根本没有处理它。所以,现在什么都不能说。不是为我工作。请注意,ZStack中有许多形状-如果我添加contentShape(),则无论我在容器视图上单击的位置如何,视图中的顶部形状似乎始终处于选中状态固定-我将修改的“contentShape()”移动到“.position()”修改器之前。“.position()”修饰符返回一个与父对象大小相同的矩形。如何让onHover{}处理该形状-它似乎忽略了contentShape()@DuncanGroenewald,我尝试了onHover,但根本没有处理它。所以,现在我什么都不能说。