Last active
July 2, 2020 15:59
-
-
Save valvoline/a8f27433a398a8201c02e2e646262c88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomSlider.swift | |
// Custom SliderView using SwiftUI | |
// | |
// Created by Costantino Pistagna on 02/07/2020. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@State var sliderValue: Float = 0.0 | |
var body: some View { | |
ZStack { | |
Color.init(white: 0.97).edgesIgnoringSafeArea(.all) | |
VStack { | |
Text(String(format: "%.2f", sliderValue)).fontWeight(.semibold).font(.title2).foregroundColor(.blue) | |
.shadow(color: Color.init(white: 1), radius: 4, x: -2, y: -2) | |
.shadow(color: Color.init(white: 0.9), radius: 4, x: 2, y: 2) | |
AnyView(SliderView(value: $sliderValue) | |
.shadow(color: Color.init(white: 1), radius: 5, x: -5, y: -5) | |
.shadow(color: Color.init(white: 0.9), radius: 5, x: 5, y: 5)) | |
.padding(.all, 30) | |
Slider(value: $sliderValue).padding(.all, 30) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct SliderView: UIViewRepresentable { | |
var value: Binding<Float> | |
var minimumTrackTintColor = Color(white: 0.93) | |
var maximumTrackTintColor = Color(white: 0.96) | |
var thumbImage: UIImage? | |
var barHeight: Float = 10 | |
func makeUIView(context: Context) -> some UIView { | |
let aView = SASlider() { (newValue) in | |
value.wrappedValue = newValue | |
} | |
aView.minimumTrackTintColor = UIColor(minimumTrackTintColor) | |
aView.maximumTrackTintColor = UIColor(maximumTrackTintColor) | |
if var thumbImage = thumbImage, thumbImage.size.width == thumbImage.size.height { | |
if thumbImage.size.width > 30 { | |
let newSize = CGSize(width: 30.0, height: 30.0) | |
UIGraphicsBeginImageContextWithOptions(newSize, false, 0) | |
defer { UIGraphicsEndImageContext() } | |
thumbImage.draw(in: CGRect(origin: .zero, size: newSize)) | |
thumbImage = UIGraphicsGetImageFromCurrentImageContext() ?? thumbImage | |
} | |
for state: UIControl.State in [.normal, .selected, .application, .reserved] { | |
aView.setThumbImage(thumbImage, for: state) | |
} | |
} | |
return aView | |
} | |
func updateUIView(_ uiView: UIViewType, context: Context) { | |
if let slider = uiView as? UISlider { | |
slider.value = value.wrappedValue | |
} | |
} | |
private class SASlider:UISlider { | |
var barHeight:CGFloat = 10.0 | |
private var completionHandler: ((Float)->Void)? | |
convenience init(customHeight: Float? = nil, _ completion:((Float)->Void)? = nil) { | |
self.init() | |
barHeight = CGFloat(customHeight ?? 10.0) | |
self.completionHandler = completion | |
self.addTarget(self, action: #selector(sliderDidChangeValue(sender:)), for: .valueChanged) | |
} | |
@objc func sliderDidChangeValue(sender: UISlider) { | |
completionHandler?(sender.value) | |
} | |
override open func trackRect(forBounds bounds: CGRect) -> CGRect { | |
let defaultBounds = super.trackRect(forBounds: bounds) | |
return CGRect( | |
x: defaultBounds.origin.x, | |
y: defaultBounds.origin.y + defaultBounds.size.height/2 - barHeight/2, | |
width: defaultBounds.size.width, | |
height: barHeight | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment