I have a subclass of UIControl what I want to achieve is a UISlider with 2 UIButton-like thumbs
but I'm coufused if should I use
touchesBegan(::)
or
beginTrackingWithTouch(::)
and how could i force my button follow touch events What I've only achieved is highlighting a button with overriding its @highlighted property like this :
class RangeSliderThumbLayer: UIButton {
override var highlighted: Bool {
didSet {
//print("did set (highlighted)")
setNeedsDisplay()
if highlighted {
self.backgroundColor = UIColor.redColor()
} else {
self.backgroundColor = UIColor.greenColor()
}
}
}
here how I've tried to move my button :
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
previousLocation = touch.locationInView(self)
print(previousLocation)
lowerThumbButton.frame = CGRectMake(previousLocation.x, 0, 20, 20)
// Hit test the thumb layers
if lowerThumbButton.pointInside(previousLocation, withEvent: event) {
print("point indide")
lowerThumbButton.highlighted = true
} else if upperThumbButton.frame.contains(previousLocation) {
upperThumbButton.highlighted = true
}
retu lowerThumbButton.highlighted || upperThumbButton.highlighted
}
private func boundValue(value: Double, toLowerValue lowerValue: Double, upperValue: Double) -> Double {
retu min(max(value, lowerValue), upperValue)
}
override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
let location = touch.locationInView(self)
let value = round(location.x / step)
let validValue = minimumValue + (Double(value) * increase)
let deltaLocation = Double(location.x - previousLocation.x)
print(value)
lowerThumbButton.frame = CGRectMake(value, 0, 20, 20)
previousLocation = location
if lowerThumbButton.highlighted {
lowerValue = boundValue(validValue, toLowerValue: minimumValue, upperValue: upperValue)
} else if upperThumbButton.highlighted {
upperValue = boundValue(validValue, toLowerValue: lowerValue, upperValue: maximumValue)
}
retu true
}
