Q:
Unable to pass
Beta-cell autoimmu
# -*- encoding: ut
Q:
SQL Server Man
In this study, we
Q:
Find the numbe
The present invent
The Evolution of
Q:
PHP - How to s
The present inventQ:
How to set up custom event listener for UISlider in Swift?
I am working on a universal project in Swift. I want to handle a custom event whenever a user changes the value of a slider. I tried following code but it does not seem to work. This code is placed inside view controller class and not a view.
slider.addTarget(self, action: "sliderChanged", forControlEvents: .ValueChanged)
A:
Add this function in viewDidLoad and add sliderChanged()
func sliderChanged() {
//call slider changed function
}
Add Event to custom Event of Slider Class on value Changed
@IBAction func sliderChanged(sender: AnyObject) {
//your code here
}
You should enable the 'User Interaction' for your slider otherwise this will not work
A:
Make sure you've added @IBOutlet for slider, here is the simple implementation of slider event:
@IBOutlet weak var slider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
slider.addTarget(self, action: "sliderChanged", forControlEvents: .ValueChanged)
}
func sliderChanged() {
print("Value Changed")
}
A:
Add the following in your viewDidLoad() to listen for the slider value changed event:
slider.addTarget(self, action: "sliderChanged:", forControlEvents: .ValueChanged)
Note that it uses the "sliderChanged:" selector name which corresponds to the selector below:
@IBAction func sliderChanged(sender: UISlider) {
}
This selector can also be done programmatically instead of through IB:
@IBAction func sliderChanged(sender: UISlider) {
}
You can get some more details of Selectors here:
http://www.macocrunch.com/2015/03/02/ios-swift-uicontrols-property-ivar-properties-overloading-and-selectors/
The default event for a UISlider to fire is ValueChanged. However,
you can also hook this up to another control/action by either setting
it through Interface Builder or programmatically like this:
@IBAction func sliderChanged(sender: UISlider) {
//some code
}
Note that sliderChanged() is the selector that needs to be hooked up, not sliderChanged
A:
Add target for ValueChanged and get your sliderValue
slider.addTarget(self, action: "sliderChanged", forControlEvents: .ValueChanged)
var valueChanged = 0.0
func sliderChanged(){
let newValue = Int(slider.value) //Value is the current position
//Use this to decide action you want to do
if newValue == 1{
//DO THIS
valueChanged = 0.0
}
else if newValue == 2{
//DO THIS
valueChanged = 0.1
}
else if newValue == 3{
//DO THIS
valueChanged = 0.2
}
else if newValue == 4{
//DO THIS
valueChanged = 0.3
}
else if newValue == 5{
//DO THIS
valueChanged = 0.