-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.swift
110 lines (101 loc) · 3.24 KB
/
Container.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
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Container.swift
// SwiftUIBasics
//
// Created by A. Zheng (github.com/aheze) on 2/2/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//
import SwiftUI
struct Container: ViewModifier {
let image: String
let title: String
@State var showingImage = true
@State var showingResult = false
init(
image: String,
title: String,
showingImage: Bool,
showingResult: Bool
) {
self.image = image
self.title = title
self._showingImage = State(initialValue: showingImage)
self._showingResult = State(initialValue: showingResult)
}
func body(content: Content) -> some View {
HStack {
Button {
withAnimation {
showingImage.toggle()
}
} label: {
LinearGradient(
colors: [.blue, .green],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
.opacity(showingImage ? 0 : 1)
.overlay(
VStack(alignment: .leading) {
Text(title)
.font(.system(size: 60, design: .monospaced))
.foregroundColor(.purple)
.padding()
Image(image)
.resizable()
.aspectRatio(contentMode: .fit)
.opacity(showingImage ? 1 : 0)
}
)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .systemBackground))
.cornerRadius(24)
}
Button {
withAnimation {
showingResult.toggle()
}
} label: {
LinearGradient(
colors: [.red, .yellow],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
.opacity(showingResult ? 0 : 1)
.overlay(
content
.frame(width: 360)
.scaleEffect(1.5)
.opacity(showingResult ? 1 : 0)
)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .systemBackground))
.cornerRadius(24)
}
.buttonStyle(.plain)
}
.padding()
.background(Color(uiColor: .secondarySystemBackground))
}
}
struct Slide<Content: View>: View {
@ViewBuilder let content: Content
var body: some View {
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .systemBackground))
.cornerRadius(24)
}
}
extension View {
func addContainer(image: String, title: String, showingImage: Bool = true, showingResult: Bool = true) -> some View {
modifier(
Container(
image: image,
title: title,
showingImage: showingImage,
showingResult: showingResult
)
)
}
}