TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift UISlider Example

UISlider. A value can be set low or high—like a volume control. This is a Slider—a UISlider in Swift. We can use a Slider to change other controls or set variables.
Get started. Please begin with a Single View Application. Open the object library pane and drag a Slider to the iPhone screen. We will add a Label to interact with the slider.
Add label. Here is our Slider next to a Label. In our tutorial, we will change the Label's text dynamically, as the Slider is moved back and forth.
Add outlets. Please add outlets to the Slider and the Label. These are inserted as UISlider and UILabel fields in the ViewController.swift file.

Name: Provide names for the UISlider and UILabel—I used "simpleSlider" and "simpleLabel" to stress how simple I hope they will be.

Code so far. Here is our ViewController.swift file at this point. We have a UISlider and a UILabel field. These were generated by Xcode.
Example ViewController: Swift // // ViewController.swift // ExampleTime9 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBOutlet weak var simpleSlider: UISlider! @IBOutlet weak var simpleLabel: UILabel! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Primary action triggered. With the Slider, the "Primary Action Triggered" event is triggered when it is adjusted. This is the perfect event to handle.

Tip: To add an event handler for "Primary Action Triggered," drag from the adjacent circle to the ViewController file.

Next: Please add some code to the event handler (named "sliderMoved" here). Here we use the UILabel and the UISlider together.

Example ViewController with IBAction: Swift // // ViewController.swift // ExampleTime9 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction func sliderMoved(sender: AnyObject) { // Get Float value from Slider when it is moved. let value = simpleSlider.value // Assign text to string representation of float. simpleLabel.text = String(value) } @IBOutlet weak var simpleSlider: UISlider! @IBOutlet weak var simpleLabel: UILabel! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Notes, sliderMoved. In sliderMoved we get a Float from the UISlider. This is a value between 0 and 1.0. It is a Float, but we can convert it to a String—we use a String for the Label.
Run simulator. We begin the simulator by clicking on the right arrow in Xcode. The project builds. And a complete disaster appears on our screen.
Results, move Slider. Drag the UISlider's circle to the left and right. The Label's text should adjust to read the UISlider value.

Important: When learning new platforms, we must focus on how things work, not whether they are useful or beautiful or not.

A review. A UISlider is like a volume control. But it can be used for many things other than volume: it can adjust size limits, speeds, difficulty levels.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf