prty grl
prty grl

Reputation: 25

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

Upvotes: 0

Views: 154

Answers (1)

sumida
sumida

Reputation: 370

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

.contentShape(Circle()) 

Upvotes: 0

Related Questions