Skip to content

Instantly share code, notes, and snippets.

@pixelfolio
Created July 25, 2020 10:33
Show Gist options
  • Select an option

  • Save pixelfolio/3f76b05b150a7a98b5fe09f599c901d3 to your computer and use it in GitHub Desktop.

Select an option

Save pixelfolio/3f76b05b150a7a98b5fe09f599c901d3 to your computer and use it in GitHub Desktop.
//
// ContentView.swift
//
// Created by Dominic Williams on 12/07/2020.
//
import SwiftUI
struct ContentView: View {
@State private var previousCircle: Int = 1
@State private var selectedCircle: Int = 7
@State private var circleScale: CGFloat = 1.0
@State private var delay = 1.0
var body: some View {
HStack {
ForEach(1..<8) { i in
Circle()
.frame(height: 30)
.scaleEffect(selectedCircle == i ? 1.3 : circleScale)
.animation(
Animation.easeIn(duration: 0.2)
.repeatCount(1, autoreverses: true)
.delay(Double(i) * 0.15)
)
.onTapGesture {
circleTapped(circleIndex: i)
}
}
}
}
func inBetweenCircle(circleIndex: Int) -> Bool {
let lower = min(previousCircle, selectedCircle)
let upper = max(previousCircle, selectedCircle)
let range = lower...upper
if range.contains(circleIndex) {
return true
} else {
return false
}
}
func delayForCircle(circleIndex: Int) -> Double {
// This circle is inbetween the previous and selected circles
if inBetweenCircle(circleIndex: circleIndex) {
// how far away from the previous circle am I?
let circleDistance = abs(circleIndex - previousCircle)
print("Circle \(circleIndex) is between \(previousCircle) and \(selectedCircle) and is \(circleDistance) circles away from the newly selected circle in terms of distance")
// Each circle takes a second delay so its very easy to see what is happening for now
return Double(circleDistance) * 1.0
}
return 1.0
}
func circleTapped(circleIndex: Int) {
previousCircle = selectedCircle
selectedCircle = circleIndex
}
}
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