I am using the following approach to add rounded corners to a view on x number of corners:
Round Specific Corners SwiftUI
extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape( RoundedCorner(radius: radius, corners: corners) )
}
}
struct RoundedCorner: Shape {
var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
return Path(path.cgPath)
}
}
This works very well. Unfortunately when I animate this view to another Frame without any rounded corners there is no animation for the cornerRadius. All other animations work fine.
To Illustrate this, the following shows the corner radius animation with the standard .cornerRadius modifier and with the custom .cornerRadius modifier using the extension above:
struct ContentView: View {
@State var radius: CGFloat = 50
var body: some View {
VStack {
Button {
withAnimation(.easeInOut(duration: 2)) {
if radius == 50 {
radius = 0
} else {
radius = 50
}
}
} label: {
Text("Change Corner Radius")
}
Color.red
.frame(width: 100, height: 100)
.cornerRadius(radius, corners: [.topLeft, .bottomRight])
Color.blue
.frame(width: 100, height: 100)
.cornerRadius(radius)
}
}
}