-
Notifications
You must be signed in to change notification settings - Fork 0
/
62_DrawingAndCompositingGroupExample.swift
97 lines (86 loc) · 2.81 KB
/
62_DrawingAndCompositingGroupExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import SwiftUI
struct DrawingAndCompositingGroupExample: View {
@State private var didTap = false
var body: some View {
movingExample
// defaultExample
}
private var movingExample: some View {
VStack(spacing: 50) {
if didTap { Spacer() }
demoTextButton
demoTextInCircleButton
if !didTap { Spacer() }
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.black)
}
private var demoTextButton: some View {
Button(action: performMoveAction) {
makeMovingTextView(title: "top")
.drawingGroup() // <- тут
}
}
private var demoTextInCircleButton: some View {
Button(action: performMoveAction) {
ZStack {
Circle()
.fill(.black)
.frame(width: 200, height: 200)
makeMovingTextView(title: "bot")
}
.compositingGroup() // <- тут
}
.buttonStyle(DemoButtonStyleWithShadow(didTap: didTap))
}
private func performMoveAction() {
withAnimation(.linear(duration: 1)) { didTap.toggle() }
}
private func makeMovingTextView(title: String) -> some View {
Text(didTap ? "TRUE (\(title))" : "FALSE (\(title))")
.font(.title.bold())
}
/// Пример из документации
private var defaultExample: some View {
VStack {
ZStack {
Text("ExampleText")
.foregroundColor(.black)
.padding(20)
.background(Color.red)
Text("ExampleText")
.blur(radius: 2)
}
.font(.largeTitle)
.compositingGroup()
.opacity(0.9)
}
.background(Color.white)
.drawingGroup()
}
}
/// Демо-стиль для кнопки, добавляющий обводку и тень
struct DemoButtonStyleWithShadow: ButtonStyle {
let didTap: Bool
func makeBody(configuration: Configuration) -> some View {
configuration.label
.background(
Circle().stroke(
didTap ? .blue : .red,
lineWidth: configuration.isPressed ? 10 : 2
)
)
.foregroundStyle(didTap ? .blue : .red)
.scaleEffect(configuration.isPressed ? 0.8 : 1)
.shadow(
color: .green,
radius: configuration.isPressed ? 0 : 5,
x: configuration.isPressed ? 0 : 20,
y: configuration.isPressed ? 0 : 20
)
.animation(.default, value: configuration.isPressed)
}
}
#Preview {
DrawingAndCompositingGroupExample()
}