-
Notifications
You must be signed in to change notification settings - Fork 16
/
MovingCardView.swift
136 lines (115 loc) · 3.5 KB
/
MovingCardView.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// ContentView.swift
// MovingCard
//
// Created by Ganesh on 19/08/23.
//
import SwiftUI
struct MovingCardView: View {
// Properties
@State var card: Card = .init(name: "Master Card", number: "3610 7399 4823", expire: "2023/08/22")
@State private var presentSheet:Bool = false
@State var x:CGFloat = 0
@State var y:CGFloat = 0
// body
var body: some View {
ZStack{
Image("back")
.resizable()
.scaledToFill()
.ignoresSafeArea()
VStack{
Button {
presentSheet.toggle()
} label: {
CardView(card: card)
.frame(width: 320,height: 190)
}
.buttonStyle(.glassmorphism)
.rotation3DEffect(Angle(degrees: 20), axis: (x: x, y: y, z: 0))
Spacer()
}
}
.gesture(gesture)
.sheet(isPresented: $presentSheet) {
cardList
}
}
private var gesture: _EndedGesture<_ChangedGesture<DragGesture>> {
DragGesture()
.onChanged { value in
withAnimation(.easeInOut){
self.x = value.startLocation.y - value.location.y
self.y = value.startLocation.x - value.location.x
}
}
.onEnded { _ in
withAnimation(.easeInOut){
self.x = 0
self.y = 0
}
}
}
private var cardList: some View{
List{
LabeledContent {
Text(card.name)
} label: {
Text("Card Name")
}
LabeledContent {
Text(card.number)
} label: {
Text("Card Number")
}
LabeledContent {
Text(card.expire)
} label: {
Text("Expire Date")
}
}
.listStyle(.insetGrouped)
.presentationDetents([.medium])
}
}
struct CardView: View {
let card:Card
var body: some View {
VStack{
Spacer()
Text(card.name)
.font(.system(size: 20,weight: .ultraLight,design: .rounded))
.foregroundColor(.white)
.frame(maxWidth: .infinity,alignment: .center)
Spacer()
Text(card.number)
.font(.system(size: 23,weight: .medium,design: .monospaced))
.foregroundColor(.white)
.frame(maxWidth: .infinity,alignment: .center)
Spacer()
Text(card.expire)
.font(.system(size: 17,weight: .ultraLight,design: .rounded))
.foregroundColor(.white)
.frame(maxWidth: .infinity,alignment: .trailing)
.padding(.all,20)
}
}
}
struct GlassmorphismButtonStyle:ButtonStyle{
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(.white)
.background(.ultraThinMaterial,in:RoundedRectangle(cornerRadius: 10))
.overlay(.white.opacity(0.5),in:RoundedRectangle(cornerRadius: 10).stroke(style: .init()))
}
}
extension ButtonStyle where Self == GlassmorphismButtonStyle{
static var glassmorphism:GlassmorphismButtonStyle{
.init()
}
}
struct Card{
let name: String
let number: String
let expire: String
}