Skip to content

Instantly share code, notes, and snippets.

@xtabbas
Created September 4, 2019 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtabbas/be9abf3f592fc69bbe5e238a9ba0379d to your computer and use it in GitHub Desktop.
Save xtabbas/be9abf3f592fc69bbe5e238a9ba0379d to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// paths
//
// Created by Touq on 9/1/19.
// Copyright © 2019 Touq. All rights reserved.
//
import SwiftUI
struct Route: Shape {
let active: Bool
let skewed: Bool
var baseCamp: Double
var center: Double
var point: CGPoint
var animatableData: AnimatablePair<Double, Double> {
get { AnimatablePair(center, baseCamp) }
set {
// point = CGPoint(x: newValue.first, y: newValue.second)
center = newValue.first
baseCamp = newValue.second
}
}
func path(in rect: CGRect) -> Path {
var path = Path()
let radius: Double = 10
let ending: Double = 80
let middle: Double = (ending*4) + radius
let starting: Double = (ending*8) + radius
let startCamp: Double = starting
let vultures: Double = active ? middle : starting
let curve1: Double = active ? ending*4 : starting + radius
let curve2 = active ? ending*8 : starting + radius
path.addArc(center: CGPoint(x: center, y: baseCamp), radius: CGFloat(radius), startAngle: Angle(degrees: 89), endAngle: Angle(degrees: 90), clockwise: true)
path.addCurve(to: CGPoint(x:center, y: curve1), control1: CGPoint(x: skewed ? center+60 : center, y: active ? ending*2 : starting), control2: CGPoint(x: skewed ? center-60 : center, y: active ? ending*3 : starting))
path.move(to: CGPoint(x: center, y: active ? (ending*4) + (radius * 2) : starting + radius))
path.addArc(center: CGPoint(x: center, y: vultures), radius: CGFloat(radius), startAngle: Angle(degrees: 89), endAngle: Angle(degrees: 90), clockwise: true)
path.addCurve(to: CGPoint(x:center, y: curve2), control1: CGPoint(x: skewed ? center+60 : center, y: active ? ending*6 : starting), control2: CGPoint(x: skewed ? center-60 : center, y: active ? ending*7 : starting))
path.move(to: CGPoint(x: center, y: active ? (ending*8) + (radius * 2) : starting + radius))
path.addArc(center: CGPoint(x: center, y: startCamp), radius: CGFloat(radius), startAngle: Angle(degrees: 89), endAngle: Angle(degrees: 90), clockwise: true)
return path
}
}
struct ContentView: View {
@State private var active = false
@State private var skwed = false
@State private var baseCamp: Double = (80*8) + 10
@State private var center: Double = 180
var body: some View {
GeometryReader { proxy in
VStack {
HStack {
Button(action: {
self.active.toggle()
if (self.active) {
self.baseCamp = (80) + 10
} else {
self.baseCamp = (80*8) + 10
}
}, label: {
Text("Active")
})
Button(action: {
self.skwed.toggle()
}, label: {
Text("Skewed")
})
}
Route(
active: self.active,
skewed: self.skwed,
baseCamp: self.baseCamp,
center: self.center,
point: CGPoint(x: self.center, y: self.baseCamp)
)
.stroke(Color.black,lineWidth: 5)
.foregroundColor(.blue)
.animation(Animation.easeInOut(duration: 1.0))
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment