MyToggleStyle
struct MyToggleStyle:ToggleStyle {
let width: CGFloat = 50
let height:CGFloat = 30
var onColor:Color = .green
var offColor:Color = .init(UIColor.systemGray5)
var isAnimation = true
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.label
Spacer()
ZStack(alignment: configuration.isOn ? .trailing : .leading) {
Spacer()
RoundedRectangle(cornerRadius: width/2.0)
.frame(width: width, height: height)
.foregroundColor(configuration.isOn ? onColor : offColor)
RoundedRectangle(cornerRadius: (height-4)/2.0)
.frame(width: height-4 , height: height-4)
.padding(2)
.foregroundColor(.white)
.animation(isAnimation ? .easeIn(duration: 0.15) : nil)
.onTapGesture {
configuration.$isOn.wrappedValue.toggle()
}
}
}
}
}
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
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
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
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
Last updated: 2022/12/08, 06:27:06
← Date