如何只在鼠标悬停时更改圆形颜色,而不是在鼠标进入框架时更改?

huangapple go评论111阅读模式
英文:

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()) 

huangapple
  • 本文由 发表于 2023年5月28日 21:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定