Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MojtabaHs/76bcd62ba62fdb6638877cf57a738574 to your computer and use it in GitHub Desktop.
Save MojtabaHs/76bcd62ba62fdb6638877cf57a738574 to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var topLeft: CGFloat = 0
@State var topRight: CGFloat = 50
@State var bottomLeft: CGFloat = 50
@State var bottomRight: CGFloat = 0
var body: some View {
VStack {
HStack(spacing: 32) {
Slider(value: $topLeft, in: 0...50)
Slider(value: $topRight, in: 0...50)
}
Text("Round me")
.frame(width: 200, height: 100)
.background(Color.red)
.cornerRadius(topLeft, corners: .topLeft)
.cornerRadius(topRight, corners: .topRight)
.cornerRadius(bottomLeft, corners: .bottomLeft)
.cornerRadius(bottomRight, corners: .bottomRight)
HStack(spacing: 32) {
Slider(value: $bottomLeft, in: 0...50)
Slider(value: $bottomRight, in: 0...50)
}
}.padding()
}
}
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment