swiftuionhover

How to change circle colour only when cursor hover it, not when cursor enters the frame?


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

Solution

  • To change the circle colour only when cursor hover over it, after .onHover{..}, add:

    .contentShape(Circle())