英文:
How to change circle colour only when cursor hover it, not when cursor enters the frame?
问题
我想要以下的circle
只有在光标悬停在上面时才改变颜色。但它在进入frame
时不必要地改变颜色。如何解决这个问题?
import SwiftUI
struct ContentView: View {
@State private var snapCircleHover: Bool = false
var body: some View {
VStack {
Circle()
.fill(snapCircleHover ? .yellow : .green)
.position(CGPoint(x: 100, y: 100))
.onHover { isHovering in
snapCircleHover = isHovering
}
.background(Color.black) // 修复此处的错误,将.background(.black)改为.background(Color.black)
.frame(width: 200, height: 200)
}
}
}
英文:
I want the following circle
to change colour only when cursor hover over it. But it unnecessarily changes colour once it enters the frame
. How to solve that?
import SwiftUI
struct ContentView: View {
@State private var snapCircleHover: Bool = false
var body: some View {
VStack {
Circle()
.fill(snapCircleHover ? .yellow : .green)
.position(CGPoint(x: 100, y: 100) )
.onHover { Bool in
if Bool {
snapCircleHover = true
} else {
snapCircleHover = false
}
}
.background(.black)
.frame(width: 200, height: 200)
}
}
}
答案1
得分: 0
只有在光标悬停在其上时更改圆形的颜色,添加以下内容在.onHover{..}
之后:
.contentShape(Circle())
英文:
To change the circle colour only when cursor hover over it, after .onHover{..}
, add:
.contentShape(Circle())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论