-
-
Save sturdysturge/de94fe9cbc01e62483a5f680248c2aeb to your computer and use it in GitHub Desktop.
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
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