swiftui - 为什么没有在旋转的矩形上触发 SwiftUI onTapGuesture{} 修饰符
问题描述
如果我创建一个 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{} 也会发生同样的想法 - 它只是在鼠标悬停在形状框架矩形上时触发。
解决方案
rotation
不会更改接受手势的原始布局框架,因此您必须使用 显式执行此操作.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
}
使用 Xcode 11.4 / iOS 13.4 测试
注意:请注意,hittesting 仅适用于不透明部分(以防您的某些颜色清晰)
推荐阅读
- java - 字段列表中的 NativeQuery 未知列
- php - 在 Azure 中将 PHP 7 与自定义 .ini 文件一起使用时出错
- python - 普通列表和 dict.items() 的区别
- twitter-bootstrap - Bootstrap 4中的弹出底部导航栏
- javascript - 在 vue.js 项目中风格化谷歌地图
- java - 零坐标的Libgdx 3d问题
- c++ - 如何检查是否在编译时调用了模板化方法?
- ruby-on-rails - 避免 Heroku 清理一些资产
- c# - MongoDB,删除数组c#
- go - Goroutine 从未在 Linux 上执行,但在 Windows 上正常工作