IOS basics 09 switch, Slider, and segment control, ios Basics
IOS basic articles 09 switch, Slider, and segment controls
Directory:
1. Case Study
The Switch, Slider, and Segmented controls are all subclasses of UIControl. In this article, we will use a case study to learn how to use these three controls.
As shown in, this case includes two Switch controls, one Segmented Control, two labels, and one Slider Control ):
1 @ IBAction func switchValueChanged (sender: AnyObject) {2 let senderSwitch = sender! UISwitch // obtain the control currently clicked and forcibly convert it to Switch 3 4 let value = senderSwitch. on // obtain the status value 5 6 after the current Switch is clicked // if the current click is LeftSwitch 7 if (senderSwitch = self. leftSwitch) {8 self. rightSwitch. setOn (! Value, animated: true) // set the status value of the Switch on the right 9} else {// if you are currently clicking RightSwitch10 self. LeftSwitch. setOn (! Value, animated: true) // set the Switch status value on the left to 11} 12}
Then press control and drag the Switch on the right to the above method. In this way, the actions of the two switches are bound to one method:
1 @ IBOutlet weak var SliderValue: UILabel!
The Label on the right is used to display the current value of the slider. here we need to implement an Action for the slider:
1 @ IBAction func sliderValueChanged (sender: AnyObject) {2 let slider = sender! UISlider // The current slider 3 let value = Int (slider. value * 100) // get the current value (0.x) multiplied by the Int value after 100 4 let showText = NSString (format: "% d", value) // format the data 5 self. sliderValue. text = showText as String // displayed on Label 6}
The running effect is as follows:
1 @ IBAction func touchDown (sender: AnyObject) {2 if (self. leftSwitch. hidden) {// if the current switch control is displayed 3 // hide both switches 4 self. leftSwitch. hidden = false 5 self. rightSwitch. hidden = false 6} else {// if the current switch control is hidden 7 // both switches are displayed 8 self. leftSwitch. hidden = true 9 self. rightSwitch. hidden = true10} 11}
The effect is as follows: