A fun Swift UIControl for picking a size.
Add pod 'SizeSlideButton'
to your Podfile.
Add the SizeSlideButton.swift
class to your project.
let condensedFrame = CGRect(x: 280, y: 70, width: 32, height: 32) //Width and Height should be equal
let fancyControl = SizeSlideButton(condensedFrame: condensedFrame)
fancyControl.trackColor = UIColor(red: 38/255, green: 50/255, blue: 56/255, alpha: 1)
fancyControl.handle.color = UIColor.white
fancyControl.addTarget(self, action: #selector(newSizeSelected), for: .touchDragFinished)
fancyControl.addTarget(self, action: #selector(sizeSliderTapped), for: .touchUpInside)
self.view.addSubview(fancyControl)
func newSizeSelected(_ sender: SizeSlideButton){
//Do something once a size is selected and the control let go
// You can now use the senders .value for a value between 0 and 1
// Or use the handle's height (handle.height) as a multiplier for size
print("Value: \(sender.value)")
print("Size multiplier: \(sender.handle.height)")
// Ex: self.paintbrush.brushSize = kDefaultBrushSize * sender.handle.height
}
func sizeSliderTapped(_ sender: SizeSlideButton){
//Do something when the button is tapped
print("Tip Tap")
}
Output:
SizeSlideButton can also be created right from the Interface Builder and allows basic properties to be edited.
init(condensedFrame: CGRect)
will set the frame of where the condensed frame should lie (width and height should be equal)init(frame: CGRect)
will set the frame of where the frame should lie when fully expandedcurrentSize
to obtain the size the person has selectedhandlePadding
sets the padding around the handle (default is the left side's radius). Works best as <= leftSideRadiushandle.color
is the slider's handle colorhandle.height
/handle.width
gets the sizes of the handle and can be best used as a multiplier against a constant default sizetrackColor
is the color of the trackvalue
is a value between 0 and 1.0 of where the handle was positioned relative on the trackleftSideRadius
andrightSideRadius
return the radii of the left and right sides of the frame when expandedcurrentState
returns if the control is condensed or expandedanimationType
specifies the animation type used when letting go of the control
touchUpInside
is called when a tap on the control is releasedvalueChanged
is called when the slider is movedtouchDown
is called both when a long press is detected or a regular touch is detectedtouchDragFinished
(A custom UIControlEvent) is called when the slider has selected a new value and been released
SizeSlideButton is available under the MIT license. See the LICENSE file for more info.