Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active July 13, 2020 15:12
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 chriseidhof/1f7d5c41e132fcf7865977289083ba73 to your computer and use it in GitHub Desktop.
Save chriseidhof/1f7d5c41e132fcf7865977289083ba73 to your computer and use it in GitHub Desktop.
matchedGeometryEffect experiments
//
// ContentView.swift
// Effects
//
// Created by Chris Eidhof on 13.07.20.
//
import SwiftUI
struct MyModifier: AnimatableModifier {
var animatableData: Int = 0
func body(content: Content) -> some View {
content.overlay(Text("\(animatableData)"))
}
}
struct ContentView: View {
@Namespace var ns
@State var toggle = false
var body: some View {
VStack {
Toggle("Toggle", isOn: $toggle.animation(.easeInOut(duration: 1)))
Rectangle().fill(Color.red).frame(height: 200).overlay(
Group {
if toggle {
Circle()
.fill(Color.gray)
.modifier(MyModifier(animatableData: 0))
.matchedGeometryEffect(id: "circle", in: ns)
.frame(width: 100, height: 100)
}
}
)
Rectangle().fill(Color.green).frame(height: 200).overlay(
Group {
if !toggle {
Circle()
.fill(Color.white)
.modifier(MyModifier(animatableData: 100))
.matchedGeometryEffect(id: "circle", in: ns)
.frame(width: 50, height: 50)
}
}
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// ContentView.swift
// Effects
//
// Created by Chris Eidhof on 13.07.20.
//
import SwiftUI
struct ContentView: View {
@Namespace var ns
@State var toggle = false
var body: some View {
VStack {
Toggle("Toggle", isOn: $toggle.animation(.easeInOut(duration: 1)))
Rectangle().fill(Color.red).frame(height: 200).overlay(
Group {
if toggle {
Circle()
.fill(Color.black)
.matchedGeometryEffect(id: "circle", in: ns)
.frame(width: 100, height: 100)
}
}
)
Rectangle().fill(Color.green).frame(height: 200).overlay(
Group {
if !toggle {
Circle()
.fill(Color.white)
.matchedGeometryEffect(id: "circle", in: ns)
.frame(width: 50, height: 50)
}
}
)
}
}
}
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