-
Notifications
You must be signed in to change notification settings - Fork 2
/
Stepper1.swift
186 lines (155 loc) · 5.63 KB
/
Stepper1.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// SwiftUIView.swift
// VisionOS Prototypes
//
// Created by Oleg Frolov on 14/03/2024.
//
import SwiftUI
import RealityKit
struct Stepper1: View
{
enum Side
{
case Left
case Right
case Middle
}
// Settings
private let angleOffset: Double = 20.0
private let lowOpacity: Double = 0.2
private let buttonWidth: Double = 288.0
private let buttonHeight: Double = 96.0
private let sideTriggerArea: Double = 96.0
private let anchorXOffset: Double = 48.0
private let popScale: Double = 1.5
// Animation
private let animDuration: Double = 0.5
// States
@State private var isPressed: Bool = false;
@State private var currentAndleOffset: Double = 0.0
@State private var pressedSide: Side = .Middle
@State private var buttonAnchor: UnitPoint3D = UnitPoint3D(x: 0.5, y: 0.5, z: 0.0)
@State private var decimalCounter: Int = 0
@State private var easing: Animation = .easeOut
@State private var counterScale: Double = 1.0
@State private var rotateIconPlus: Double = 0.0
@State private var rotateIconMinus: Double = 0.0
@State private var buttonScaler: Double = 1.0
var body: some View
{
VStack
{
HStack(spacing: 0)
{
HStack(spacing: 0)
{
Image("minus")
.opacity(decimalCounter <= 0 ? lowOpacity : 1.0)
.foregroundStyle(.secondary)
.scaleEffect(pressedSide == .Left ? counterScale : 1.0)
.rotationEffect(.degrees(rotateIconMinus))
Spacer()
Text("\(String(decimalCounter, radix: 2))")
.animation(.none)
.frame(width: 128)
.foregroundStyle(.secondary)
Spacer()
Image("plus")
.foregroundStyle(.secondary)
.scaleEffect(pressedSide == .Right ? counterScale : 1.0)
.rotationEffect(.degrees(rotateIconPlus))
}
.font(.system(size: 32, design: .monospaced))
.fontWeight(.regular)
.padding(36.0)
}
}
.background(isPressed ? .white.opacity(0.1) : .clear)
.glassBackgroundEffect()
.frame(width: buttonWidth, height: buttonHeight)
.rotation3DEffect(.degrees(currentAndleOffset), axis: (0.0, 1.0, 0.0), anchor: buttonAnchor)
.offset(z: anchorXOffset * 2)
.scaleEffect(buttonScaler)
.gesture(DragGesture(minimumDistance: 0)
.onChanged { clickData in
if (!isPressed)
{
pressedSide = getSideThatWasPressed(clickData: clickData)
let normXOffset = anchorXOffset / buttonWidth
if (pressedSide == .Left)
{
rotateIconMinus -= 180.0
currentAndleOffset = -angleOffset
buttonAnchor = UnitPoint3D(x: 1.0 - normXOffset, y: 0.5, z: normXOffset)
}
if (pressedSide == .Right)
{
rotateIconPlus += 180.0
currentAndleOffset = angleOffset
buttonAnchor = UnitPoint3D(x: normXOffset, y: 0.5, z: normXOffset)
}
if (pressedSide == .Middle)
{
buttonScaler = 0.9
}
easing = Animation.snappy(duration: animDuration/2)
counterScale = popScale
print("Down")
// print(clickData)
// print(pressedSide)
isPressed = true
}
}
.onEnded
{ _ in
if (isPressed)
{
isPressed = false
currentAndleOffset = 0.0
if (pressedSide == .Left)
{
if ((decimalCounter - 1) < 0)
{
decimalCounter = 0
}
else
{
decimalCounter -= 1
}
}
if (pressedSide == .Right)
{
decimalCounter += 1
}
easing = Animation.spring(duration: animDuration * 2)
counterScale = 1.0
buttonScaler = 1.0
print("Up")
}
})
.onAppear()
{
}
.animation(easing, value: currentAndleOffset)
.animation(easing, value: counterScale)
}
func getSideThatWasPressed(clickData: DragGesture.Value) -> Side
{
let xPos: Double = clickData.location.x;
if (xPos <= sideTriggerArea)
{
return .Left
}
else if (xPos >= (buttonWidth - sideTriggerArea))
{
return .Right
}
else
{
return .Middle
}
}
}
#Preview {
Stepper1()
}