swift 4.2用代码实现UISwitch(开关控件),UIS
2018-12-26 本文已影响19人
yytester
ViewController.swift代码:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
var leftSwitch: UISwitch!
var rightSwitch: UISwitch!
var sliderValue: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screen = UIScreen.main.bounds
//1. 添加rightSwitch控件,rightSwitch与屏幕左边距,leftSwitch与屏幕右边距
let switchScreenSpace: CGFloat = 39
self.rightSwitch = UISwitch()
var frame = self.rightSwitch.frame
frame.origin = CGPoint(x: switchScreenSpace , y: 98) //设置frame中的origin,origin是控件的坐标原点.
//重新设置控件的位置
self.rightSwitch.frame = frame
//设置控件状态
self.rightSwitch.isOn = true
//指定事件处理方法
self.rightSwitch.addTarget(self, action: #selector(switchValueChanged(_:)), for: .valueChanged)
self.view.addSubview(self.rightSwitch)
//2. 添加leftSwitch控件
self.leftSwitch = UISwitch()
frame = self.leftSwitch.frame
frame.origin = CGPoint(x: screen.size.width - (frame.size.width + switchScreenSpace), y: 98)
//重新设置控件的位置
self.leftSwitch.frame = frame
//设置控件状态
self.leftSwitch.isOn = true
//指定事件处理方法
self.leftSwitch.addTarget(self, action: #selector(switchValueChanged(_:)), for: .valueChanged)
self.view.addSubview(self.leftSwitch)
//3. 添加segmentedControl控件
let segments = ["Right","Left"]
let segmentdControl = UISegmentedControl(items: segments)
let scWidth: CGFloat = 220
let scHeight: CGFloat = 29 //默认高度
let scTopView: CGFloat = 186
frame = CGRect(x: (screen.size.width - scWidth)/2 , y: scTopView, width: scWidth, height: scHeight)
//重新设置控件的位置
segmentdControl.frame = frame
//指定事件处理方法
segmentdControl.addTarget(self, action: #selector(touchDown(_ :)), for: .valueChanged)
self.view.addSubview(segmentdControl)
//4. 添加slider控件
let sliderWidth: CGFloat = 300
let sliderHeight: CGFloat = 31 //默认高度
let sliderdTopview: CGFloat = 298
let slider = UISlider(frame: CGRect(x: (screen.size.width - sliderWidth)/2, y: sliderdTopview, width: sliderWidth, height: sliderHeight))
slider.minimumValue = 0.0
slider.maximumValue = 100.0
slider.value = 50.00
//指定事件处理方法
slider.addTarget(self, action: #selector(sliderValueChange(_:)), for: .valueChanged)
self.view.addSubview(slider)
//5. 添加SliderValue标签,SliderValue:标签与Slider之间的距离
let labelSliderValueSliderSpace: CGFloat = 30
let labelSliderValue = UILabel(frame:CGRect(x: slider.frame.origin.x, y: slider.frame.origin.y - labelSliderValueSliderSpace, width: 103, height: 21))
labelSliderValue.text = "SliderValue: "
self.view.addSubview(labelSliderValue)
//6. 添加sliderValue标签
self.sliderValue = UILabel(frame: CGRect(x: labelSliderValue.frame.origin.x + 120, y: labelSliderValue.frame.origin.y, width: 50, height: 21))
self.sliderValue.text = "50"
self.view.addSubview(self.sliderValue)
}
@IBAction func switchValueChanged(_ sender: AnyObject) {
let witchSwitch = sender as! UISwitch
let setting = witchSwitch.isOn
self.leftSwitch.setOn(setting, animated: true)
self.rightSwitch.setOn(setting, animated: true)
}
//点击分段控件,控制开关控件的隐藏或显示
@IBAction func touchDown(_ sender: AnyObject){
let segmentedControl = sender as! UISegmentedControl
print("选择的段: " , segmentedControl.selectedSegmentIndex)
if(self.leftSwitch.isHidden == true) {
self.rightSwitch.isHidden = false
self.leftSwitch.isHidden = false
} else {
self.rightSwitch.isHidden = true
self.leftSwitch.isHidden = true
}
}
//用标签显示滑块的值
@IBAction func sliderValueChange(_ sender: AnyObject) {
let slider = sender as! UISlider
let progressAsInt = Int(slider.value)
let newText = String(format: "%d", progressAsInt)
print("滑块的值: ", newText)
self.sliderValue.text = newText
}
}
运行截图:
image.png