Last active
March 28, 2020 14:21
-
-
Save alladinian/5049e684be85b55238c86ff934eab638 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
struct CurveEditorView: View { | |
// 1 | |
private let initialPoint0: CGSize = .init(width: 0.4, height: 0.3) | |
private let initialPoint1: CGSize = .init(width: 0.6, height: 0.6) | |
// 2 | |
@State private var offsetPoint0: CGSize = .zero | |
@State private var offsetPoint1: CGSize = .zero | |
private var curvePoint0: RelativePoint { | |
(initialPoint0 + offsetPoint0).toPoint | |
} | |
private var curvePoint1: RelativePoint { | |
(initialPoint1 + offsetPoint1).toPoint | |
} | |
// 3 | |
@Binding var controlPoint1: RelativePoint | |
@Binding var controlPoint2: RelativePoint | |
var body: some View { | |
let primaryColor = Color.blue | |
let secondaryColor = primaryColor.opacity(0.7) | |
return GeometryReader { reader in | |
Color.white | |
// 4 | |
CurveShape(cp0: self.curvePoint0, cp1: self.curvePoint1) | |
.stroke(primaryColor, lineWidth: 4) | |
// 5 | |
Path { p in | |
p.move(to: CGPoint(x: 0, y: 1 * reader.size.height)) | |
p.addLine(to: self.curvePoint0 * reader.size) | |
}.stroke(secondaryColor, lineWidth: 2) | |
Path { p in | |
p.move(to: CGPoint(x: 1 * reader.size.width, y: 0)) | |
p.addLine(to: self.curvePoint1 * reader.size) | |
}.stroke(secondaryColor, lineWidth: 2) | |
// 6 | |
ControlPointHandle() | |
.offset(self.initialPoint0 * reader.size) | |
.foregroundColor(primaryColor) | |
.draggable(onChanged: { (size) in | |
self.offsetPoint0 = size / reader.size | |
self.controlPoint1 = self.curvePoint0 | |
}) | |
ControlPointHandle() | |
.offset(self.initialPoint1 * reader.size) | |
.foregroundColor(primaryColor) | |
.draggable(onChanged: { (size) in | |
self.offsetPoint1 = size / reader.size | |
self.controlPoint2 = self.curvePoint1 | |
}) | |
} | |
.aspectRatio(contentMode: .fit) | |
.onAppear { | |
self.controlPoint1 = self.curvePoint0 | |
self.controlPoint2 = self.curvePoint1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment