Skip to content

Instantly share code, notes, and snippets.

@arashkashi
Created June 11, 2021 07:34
Show Gist options
  • Save arashkashi/647de5ed5e9dbd57f058faa936b98740 to your computer and use it in GitHub Desktop.
Save arashkashi/647de5ed5e9dbd57f058faa936b98740 to your computer and use it in GitHub Desktop.
decide which sides of a rectangle you would like to be rounded #swiftui
//
// ContentView.swift
// testswiftUI
//
// Created by Arash on 2021-06-10.
//
import SwiftUI
struct ContentView: View {
@State private var startx: Double = 0
@State private var starty: Double = 0
@State private var endx: Double = 0
@State private var endy: Double = 0
var body: some View {
Text("asdf")
.frame(width: 200, height: 200)
.background(RoundedCorners(tl: 30, tr: 0, bl: 8, br: 0))
}
}
extension LinearGradient {
static func yTypeII() -> LinearGradient {
return LinearGradient(gradient: Gradient(colors: [.yellow, Color.init(red: 127/255.0 , green: 141/255.0, blue: 193/255.0)]),
startPoint: .leading,
endPoint: .trailing)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct RoundedCorners: View {
var gradient = LinearGradient.yTypeII()
var tl: CGFloat = 0.0
var tr: CGFloat = 0.0
var bl: CGFloat = 0.0
var br: CGFloat = 0.0
var body: some View {
GeometryReader { geometry in
Path { path in
let w = geometry.size.width
let h = geometry.size.height
// Make sure we do not exceed the size of the rectangle
let tr = min(min(self.tr, h/2), w/2)
let tl = min(min(self.tl, h/2), w/2)
let bl = min(min(self.bl, h/2), w/2)
let br = min(min(self.br, h/2), w/2)
path.move(to: CGPoint(x: w / 2.0, y: 0))
path.addLine(to: CGPoint(x: w - tr, y: 0))
path.addArc(center: CGPoint(x: w - tr, y: tr), radius: tr, startAngle: Angle(degrees: -90), endAngle: Angle(degrees: 0), clockwise: false)
path.addLine(to: CGPoint(x: w, y: h - br))
path.addArc(center: CGPoint(x: w - br, y: h - br), radius: br, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 90), clockwise: false)
path.addLine(to: CGPoint(x: bl, y: h))
path.addArc(center: CGPoint(x: bl, y: h - bl), radius: bl, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 180), clockwise: false)
path.addLine(to: CGPoint(x: 0, y: tl))
path.addArc(center: CGPoint(x: tl, y: tl), radius: tl, startAngle: Angle(degrees: 180), endAngle: Angle(degrees: 270), clockwise: false)
}
.fill(self.gradient)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment