1、代码集合中涉及的控件有:
按钮、操作表、图像控件UIImageView、文本框UITextField、 警告框UIAlertView、 选择控件UISegmentControl、开关按钮 UISwitch、进度条UIProgressView、滑块UiSlider、选择框UI。
2、 关于其他控件的用法,后续更新......
3、 若查阅, windows OS,请在本页面按键control + F, 输入查找内容,MAC os,按键 command + F,输入查找内容。
4、源码如下:
import UIKit class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. ///----图像控件 UIImage // UI_ImageView(); // var pickerView = UIPickerView(); // pickerView.selectRow(1,inComponent: 0,animated: true); // pickerView.selectRow(2,inComponent: 1,animated: true); // pickerView.selectRow(3,inComponent: 2,animated: true); // self.view.addSubview(pickerView); // // ///---创建一个按钮 // let cgr = CGRect(x:10,y:20,width: 200,height: 30); // var button = UIButton(frame: cgr); // button.backgroundColor = UIColor.blueColor(); // button.setTitle("点我试试",forState: .Normal); // self.view.addSubview(button); // button.addTarget(self,action: "getPickViewValue",forControlEvents: .TouchUpInside); ///---操作表 // var actionsheet = UIActionSheet(); // actionsheet.addButtonWithTitle("取消"); // actionsheet.addButtonWithTitle("选项1"); // actionsheet.addButtonWithTitle("选项2"); // // actionsheet.cancelButtonIndex = 0; // actionsheet.showInView(self.view); ///--警告框 // //UI_AlertView(); // ///---滑块 // //UI_Slider(); // // ///---进度条 // //UI_Process(); // // // ///---选择控件 // //UI_SegmentedControl(); // // // // //UI_Switch(); // // //UI_TextView(); // ///--创建 // //UI_TextField(); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } ///---警告框的使用 // func UI_AlertView() // { // // let alert = UIAlertController(title: "系统提示",message: "暂无WiFi可用?",preferredStyle: UIAlertControllerStyle.Alert); // // ///---设置提示内容 // alert.message = "暂无WiFi可用?"; // ///--添加2个按钮 // alert.addButtonWithTitle("取消"); // alert.addButtonWithTitle("确定"); // ///--设置按钮的顺序 // alert.cancelButtonIndex = 0; // // alert.delegate = self; // alert.show(); // self.view.addSubview(alert); // } // func alert(alert:UIAlertView,clickedButtonAtIndex buttonIndex:Int) // { // if (buttonIndex == alert.cancelButtonIndex) // { // print("你点击了取消"); // } // else // { // print("你点击了确定"); // } // } ///---滑块 // func UI_Slider() // { // let cgr = CGRect(x:10,height: 30); // let slider = UiSlider(frame: cgr); // // slider.center = self.view.center; // slider.minimumValue = 0; // slider.maximumValue = 100; // slider.value = 10; // // slider.backgroundColor = UIColor.blueColor(); // slider.tintColor = UIColor.greenColor(); // // // slider.addTarget(self,action:"",forControlEvents: <#T##UIControlEvents#>) // // self.view.addSubview(slider); // } ///--进度条 func UI_Process() { let proc = UIProgressView(progressViewStyle:UIProgressViewStyle.Default); proc.center = self.view.center; proc.progress = 0.5; proc.setProgress(0.8,animated: true); ///---设置颜色 ///--小于1 proc.progressTintColor = UIColor.blueColor(); ///--- 等于1 proc.trackTintColor = UIColor.greenColor(); self.view.addSubview(proc); } ///----选择控件 func UI_SegmentedControl() { let items = ["选项1","选项2","选项3"]; let segc = UISegmentedControl(items: items); segc.center = self.view.center; ///---下表是从0开始的 segc.selectedSegmentIndex = 0; ///---设置控件的颜色 segc.tintColor = UIColor.grayColor(); ///--修改选项的文字 segc.setTitle("swift",forSegmentAtIndex: 1); ///--插入,第三个参数为:是否显示插入动画 segc.insertSegmentWithTitle("we love ",atIndex: 1,animated: false); ///---删除选项 segc.removeSegmentAtIndex(2,animated: true); segc.insertSegmentWithTitle("swift",atIndex: 2,animated: true); ///--获取选择的选项的值 segc.addTarget(self,action: ("segcChanged:"),forControlEvents:UIControlEvents.ValueChanged); self.view.addSubview(segc); } func segcChanged(segc: UISegmentedControl) { print("选择的文字为"); print(segc.titleForSegmentAtIndex(segc.selectedSegmentIndex)) } /*下面的控件有:button,textview, textfieled,*/ func UI_TextField() { ///---设置位置 var cgr = CGRect(x:10,height: 30); ///--实例化对象的位置 var textField = UITextField(frame:cgr); ///---设置圆角形状 textField.borderStyle = UITextBorderStyle.RoundedRect; ///---设置文本样式 textField.placeholder = "请输入用户名"; ///---设置默认值 textField.text = "we love swift 2.1"; ///---设置自动调整大小,使得所有文字都能显示 textField.adjustsFontSizeToFitWidth = true; ///---设置使用的字号的最小值 textField.minimumFontSize = 15; ///---设置文字对齐方式 textField.textAlignment = NSTextAlignment.Center; ///---设置键盘样式 textField.returnKeyType = UIReturnKeyType.Go; ///---设置清除按钮 textField.clearButtonMode = UITextFieldviewmode.Always; self.view.addSubview(textField); } func UI_TextView() { ///---设置位置 var cgr = CGRect(x:10,width: 300,height: 100); var textView = UITextView(frame:cgr); textView.layer.borderWidth = 1; textView.layer.borderColor = UIColor.grayColor().CGColor; textView.editable = true; textView.textColor = UIColor.greenColor(); textView.font = UIFont(name: "Zapfino",size: 10); textView.dataDetectorTypes = UIDataDetectorTypes.PhoneNumber; self.view.addSubview(textView); } func UI_Switch() { var cgr = CGRect(x:10,height: 100); var m_switch = UISwitch(frame:cgr); m_switch.on = true; m_switch.addTarget(self,action: Selector("m_switchChanged"),forControlEvents: UIControlEvents.ValueChanged); self.view.addSubview(m_switch); } func m_switchChanged() { print("这里是开关的选择事件"); } /* 图像控件 UIImageView */ func UI_ImageView() { ///--设置图像 let imageView = UIImageView(image: UIImage(named: "image")); ///--设置位置 imageView.frame = CGRectMake(10,10,100,100); <pre name="code" class="plain">import UIKit class ImageViewAnimat:UIViewController { var imageView:UIImageView! override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated); imageView.startAnimating(); } override func viewWillDisappear(animated: Bool) { super.viewWillAppear(animated); imageView.stopAnimating(); } override func viewDidLoad() { super.viewDidLoad(); imageView = UIImageView(); imageView.frame = CGRectMake(10,100); ///---设置动画图片 imageView.animationImages = [UIImage(named: "image1")!,UIImage(named:"image2")!]; ///---设置切换速率,1s 一次 imageView.animationDuration = 1; self.view.addSubview(imageView); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
5、补上 关于使用 控件 UIImageView实现动画的代码:
@H_404_39@import UIKit class ImageViewAnimat:UIViewController { var imageView:UIImageView! override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated); imageView.startAnimating(); } override func viewWillDisappear(animated: Bool) { super.viewWillAppear(animated); imageView.stopAnimating(); } override func viewDidLoad() { super.viewDidLoad(); imageView = UIImageView(); imageView.frame = CGRectMake(10,1s 一次 imageView.animationDuration = 1; self.view.addSubview(imageView); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }6、微调器 UIStepper
代码如下:
import UIKit class ViewController: UIViewController { ///---新增代码 var stepper:UIStepper!; var label:UILabel!; override func viewDidLoad() { super.viewDidLoad() /* 微调器 */ super.viewDidLoad(); stepper = UIStepper(); stepper.center = self.view.center; ///设置范围与初始值 stepper.maximumValue = 100; stepper.minimumValue = 1; stepper.value = 11; ///--设置每次 增减的值 stepper.stepValue = 1; ///---设置stepper可以按住不放来连续更改值 stepper.continuous = true; ///---设置stepper是否循环(到最大值时在增加数值从最小值开始) stepper.wraps = true; ///---设置stepper的事件 stepper.addTarget(self,action: "StepperClicked",forControlEvents: .ValueChanged); ///---显示控件 self.view.addSubview(stepper); ///----设置label的位置 label = UILabel(frame: CGRectMake(100,190,300,30)); ///---设置label的值 label.text = "当前值为:" + String(stepper.value); ///--显示控件 self.view.addSubview(label); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } ///---stepper 事件函数 func StepperClicked() { label.text = "当前值为:\(stepper.value)"; } }
7、滚动视图的创建
在图片大道屏幕容纳不下的时候,UIKit框架中提供了UIScrollView类实现滚动视图,可以使用手指滚动屏幕来方便浏览整个页面。代码如下:
///---自定义代码开始---- var scrollView = UIScrollView(); scrollView.frame = self.view.bounds; var imageview = UIImageView(image: UIImage(named: "host")); scrollView.contentSize = imageview.bounds.size; scrollView.addSubview(imageview); self.view.addSubview(scrollView); ///---自定义代码结束----通过frame属性设置滚动视图可视区域的位置和大小,上面的代码中可视区域的大小设置为了屏幕的大小,然后需要调用UIScrollView的addsubview方法向滚动的视图中添加显示 的内容,还设置了contentsize内容大小,内容大小是包括了隐藏的部分。
滚动条的风格可以由属性indicatorStyle指定,indicatorStyle值如下:
UIScrollViewIndicaStyle.Default 默认滚动样式,为白色
UIScrollViewIndicaStyle.Black 黑色样式的滚动
UIScrollViewIndicaStyle.White 白色样式的滚动
原文链接:https://www.f2er.com/swift/324588.html