-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSlider.swift
72 lines (57 loc) · 1.76 KB
/
CustomSlider.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
//
// CustomSlider.swift
// PhotoGallery
//
// Created by Arslan Ilyas on 14/07/2024.
//
import SwiftUI
import UIKit
struct CustomSlider: UIViewRepresentable {
@Binding var value: Double
class Coordinator: NSObject {
var customSlider: CustomSlider
init(customSlider: CustomSlider) {
self.customSlider = customSlider
}
@objc func valueChanged(_ sender: UISlider) {
customSlider.value = Double(sender.value)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(customSlider: self)
}
func makeUIView(context: Context) -> UISlider {
let slider = UISlider()
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = Float(value)
// Customizing the slider thumb image
if let thumbImage = UIImage(named: "thumbImage") {
slider.setThumbImage(thumbImage, for: .normal)
}
slider.minimumTrackTintColor = .purple
slider.maximumTrackTintColor = .green
slider.addTarget(context.coordinator, action: #selector(Coordinator.valueChanged(_:)), for: .valueChanged)
return slider
}
func updateUIView(_ uiView: UISlider, context: Context) {
uiView.value = Float(value)
}
}
// Example SwiftUI view using CustomSlider
struct TempView: View {
@State private var sliderValue: Double = 50.0
var body: some View {
VStack {
CustomSlider(value: $sliderValue)
.frame(height: 50)
Text("Slider value: \(sliderValue)")
}
.padding()
}
}
struct TempView_Previews: PreviewProvider {
static var previews: some View {
TempView()
}
}