Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created July 3, 2023 23:22
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 sturdysturge/de94fe9cbc01e62483a5f680248c2aeb to your computer and use it in GitHub Desktop.
Save sturdysturge/de94fe9cbc01e62483a5f680248c2aeb to your computer and use it in GitHub Desktop.
import SwiftUI
struct AdaptiveView: View {
let colours: [Color] = [.red, .orange, .yellow, .green, .blue, .indigo, .purple]
let namespace: Namespace.ID
@Binding var selectedIndex: Int
let index: Int
let isSource: Bool
var size: Double {
selectedIndex == index ? .infinity : 50
}
var body: some View {
ZStack {
Text("\(index)")
.frame(maxWidth: size, maxHeight: size)
.padding()
.background(colours[index % colours.count])
.foregroundColor(.primary)
.matchedGeometryEffect(id: index, in: namespace, isSource: isSource)
.lineLimit(1)
.minimumScaleFactor(0.5)
.onTapGesture {
withAnimation(.linear(duration: 0.5)) {
if selectedIndex == index {
selectedIndex = -1
} else {
selectedIndex = index
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment