Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevAndArtist/03bafe475d0544054d6f5fdb5ec2c976 to your computer and use it in GitHub Desktop.
Save DevAndArtist/03bafe475d0544054d6f5fdb5ec2c976 to your computer and use it in GitHub Desktop.
import SwiftUI
struct HeroAnimationTest: View {
let indices = 0 ..< 3
@State
var showsOtherView = false
@Namespace
var namespaceID: Namespace.ID
@State
var id: String?
var body: some View {
ZStack
.init {
Button("transition") {
withAnimation(.linear(duration: 1.0)) {
showsOtherView.toggle()
}
}
HStack
.init {
ForEach(indices, id: \.self) { index in
Color
.red
.overlay(
Group {
if showsOtherView == false || id != "\(index)" {
Color
.clear
.overlay(Text("\(index)"))
.matchedGeometryEffect(id: "\(index)", in: namespaceID)
}
}
)
.frame(width: 40, height: 40)
.onTapGesture {
id = "\(index)"
}
}
}
.offset(y: -50)
if showsOtherView {
OtherView
.init(indices: indices, id: $id, namespaceID: namespaceID)
.offset(y: 50)
}
}
}
}
struct OtherView: View {
let indices: Range<Int>
@Binding
var id: String?
let namespaceID: Namespace.ID
var body: some View {
HStack
.init(spacing: 20) {
ForEach(indices, id: \.self) { index in
Color
.green
.overlay(Text("\(index)"))
.frame(width: 40, height: 40)
.matchedGeometryEffect(
id: id == "\(index)" ? "\(index)" : "\(index)_",
in: namespaceID
)
.onTapGesture {
id = "\(index)"
}
}
}
.offset(y: 50)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment