IOS basics 09 switch, Slider, and segment control, ios Basics

Source: Internet
Author: User

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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.