简体   繁体   中英

SwiftUI: How to apply .clipShape to .hoverEffect?

I'm using the .hoverEffect modifier. It adds an hover effect to my button when users have their mouse/trackpad pointer on it:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button("Hello world") {
            // Action…
        }
        .padding()
        .background(Color.blue)
        .foregroundColor(.white)
        .hoverEffect(.lift) // ← Hover effect
        .clipShape(Capsule())
    }
}

However, when I apply a clip shape to the button (eg RoundedRectangle or Capsule ), the view is clipped but the hover effect isn't.

具有悬停效果的按钮

How can I have a hover effect that matches the shape of the button?

Use .contentShape(Capsule()) to change the shape of the hover effect to match your clipShape . Note that this modifier also affects the hit testing area, which is probably desirable in most examples similar to your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM