onTapGesture修改器在swiftui中从第二次开始工作



我写了一个快速打开Xcode项目的工具

当我第一次点击时,我的onTapGesture修改器不起作用。它在第二次运行时如预期,以此类推。可能是我的应用程序窗口未激活吗?

源代码:https://github.com/DKalachniuk/XcodeProjects文件ProjectIcon.swift

struct ProjectIcon: View {
let project: Project
@EnvironmentObject var preferences: Preferences
var body: some View {
ZStack {
Rectangle()
.foregroundColor(Color.clear)
.background(Color(self.project.color.color))
.cornerRadius(8)
Text(String(self.project.name.first ?? "-").capitalized)
.font(.system(size: 14, weight: Font.Weight.semibold))
.padding(EdgeInsets(top: 0, leading: 4, bottom: 0, trailing: 4))
.foregroundColor(Color.white)
}
.frame(width: 24, height: 24)
.onTapGesture {
let newColor = CodableColorPicker.shared.pickRandomColor()
self.preferences.changeProjectsColor(self.project, newColor: newColor)
}
}
}

更新:关于这个bug的有趣观察。如果我点击窗口上的某个地方,然后点击项目图标,它会立即工作

添加以下内容(使用Xcode 12/macOS 10.15.5测试(

.frame(width: 24, height: 24)
.contentShape(RoundedRectangle(cornerRadius: 8))     // << here !!
.onTapGesture {

注意:顺便说一句,查看pickRandomColor()的逻辑,它不考虑存储在首选项中的颜色,以防万一

最新更新