Created
January 31, 2021 06:34
-
-
Save SatoTakeshiX/c7ac3d6c82258aae693a997b86036b69 to your computer and use it in GitHub Desktop.
SuperEllipse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by satoutakeshi on 2021/01/30 in MIT License | |
// | |
import SwiftUI | |
struct SuperEllipse: View { | |
var body: some View { | |
Image(systemName: "moon") | |
.resizable() | |
.frame(width: 100, height: 100) | |
.background(Color.yellow) | |
.clipShape(SuperEllipseShape(rate: 0.75)) | |
} | |
} | |
struct SuperEllipseShape: Shape { | |
let rate: CGFloat | |
func path(in rect: CGRect) -> Path { | |
let handleX: CGFloat = rect.size.width * rate / 2 | |
let handleY: CGFloat = rect.size.height * rate / 2 | |
let left = CGPoint(x: rect.minX, y: rect.midY) | |
let top = CGPoint(x: rect.midX, y: rect.minY) | |
let right = CGPoint(x: rect.maxX, y: rect.midY) | |
let bottom = CGPoint(x: rect.midX, y: rect.maxY) | |
var path = Path() | |
path.move(to: left) | |
path.addCurve(to: top, | |
control1: CGPoint(x: left.x, y: left.y - handleY), | |
control2: CGPoint(x: top.x - handleX, y: top.y)) | |
path.addCurve( | |
to: right, | |
control1: CGPoint(x: top.x + handleX, y: top.y), | |
control2: CGPoint(x: right.x, y: right.y - handleY) | |
) | |
path.addCurve( | |
to: bottom, | |
control1: CGPoint(x: right.x, y: right.y + handleY), | |
control2: CGPoint(x: bottom.x + handleX, y: bottom.y) | |
) | |
path.addCurve( | |
to: left, | |
control1: CGPoint(x: bottom.x - handleX, y: bottom.y), | |
control2: CGPoint(x: left.x, y: left.y + handleY) | |
) | |
return path | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
SuperEllipse() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see this post for the detail
https://blog.personal-factory.com/2021/01/30/how-to-create-clubhouse-super-ellipse-by-swiftui/