Skip to content

Commit f24aa1a

Browse files
committed
matchedgeometryeffect sample
1 parent 98bf05c commit f24aa1a

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

SwiftUIPlayground/Samples.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,11 @@ public let samples: [String: AnyView] = [
145145
"ViewThatFits": AnyView(ViewThatFitsSample()),
146146
]
147147
)),
148-
148+
"Animations": AnyView(SamplesList(
149+
title: "Animations", samples:
150+
[
151+
// https://developer.apple.com/documentation/swiftui/view/matchedgeometryeffect(id:in:properties:anchor:issource:)
152+
"matchedgeometryeffect": AnyView(MatchedGeometryEffectSample()),
153+
]
154+
)),
149155
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// MatchedGeometryEffectSample.swift
3+
// SwiftUIPlayground
4+
//
5+
// Created by Taha Tesser on 07.10.2024.
6+
//
7+
8+
import SwiftUI
9+
10+
struct MatchedGeometryEffectSample: View {
11+
@Namespace private var animation
12+
@State private var isExpanded: Bool = false
13+
14+
var body: some View {
15+
ZStack {
16+
if isExpanded {
17+
ZStack {
18+
RoundedRectangle(cornerRadius: 10)
19+
.fill(.black)
20+
.clipShape(RoundedRectangle(cornerRadius: 16))
21+
Image(systemName: "rainbow")
22+
.symbolRenderingMode(.multicolor)
23+
.resizable()
24+
.scaledToFit()
25+
.padding()
26+
.matchedGeometryEffect(id: "park", in: animation)
27+
28+
}
29+
} else {
30+
ZStack {
31+
RoundedRectangle(cornerRadius: 10)
32+
.fill(.black)
33+
.frame(height: 100)
34+
.clipShape(RoundedRectangle(cornerRadius: 16))
35+
Image(systemName: "rainbow")
36+
.symbolRenderingMode(.multicolor)
37+
.resizable()
38+
.scaledToFit()
39+
.frame(height: 80)
40+
.matchedGeometryEffect(id: "park", in: animation)
41+
}
42+
.padding()
43+
}
44+
}
45+
.onTapGesture {
46+
withAnimation(.spring()) {
47+
isExpanded.toggle()
48+
}
49+
}
50+
.ignoresSafeArea() }
51+
}
52+
53+
#Preview {
54+
MatchedGeometryEffectSample()
55+
}

0 commit comments

Comments
 (0)