Created
November 10, 2020 06:00
-
-
Save DevAndArtist/03bafe475d0544054d6f5fdb5ec2c976 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 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