Swift UI控件详细介绍(上)

UI控件

首先介绍一下AppDelegate.swift
@UIApplicationMain 调用了OC中的UIApplicationMain函数;
UIApplicationMain是iOS应用程序的入口
UIApplicationMain:
a.建立了一个UIApplication对象,表明当前应用程序. 做用是用来检测当前应用程序状态的改变。 ios

  • 1.在这个方法中来搭建应用程序中的全部的界面
  • 2.获取应用程序须要展现的数据
  • 3.使用界面展现数据

    注意:若是不在这个方法中去建立window,那么程序会经过Main.storyboard去建立应用程序的界面swift

b.建立一个遵照UIApplicationDelegate的协议的类的对象做为UIApplication的代理,做用是处理应用程序状态的改变(建立AppDelegate对象而且设置为UIApplication对象的代理)数组

这个里面一个有六个方法分别是
1.当应用程序已经启动成功后,会自动调用这个方法 application
2.当应用程序将要成为非活跃状态的时候会自动调用这个方法。applicationWillResignActive闭包

活跃状态:程序在屏幕上可见
非活跃状态:程序没有显示在屏幕上(按home键进入后台、来电打断、在当前应用程序打开其余的应用程序app

3.应用程序已经进入后台的时候会自动调用( 进入后台:按home键)applicationDidEnterBackgroundless

按下home键:command + shift + h
在这个方法中通常去暂停视频/音频播放;游戏须要暂停游戏;保存数据iview

4.应用程序将要进入前台的时候会自动调用( 进入前台)applicationWillEnterForegrounddom

按两下home键:command + shift + h + h(模拟器)ide

5.应用程序已经变成活跃状态的时候会自动调用 applicationDidBecomeActive函数

a.程序启动成功后
b.程序从后台从新进入前台
c.来电打断结束

6.应用程序将要终止的时候会调用applicationWillTerminate

UIview

UIview:是ios中全部视图(控件)直接/间接的父类;因此uiview的属性和方法,对于其余类型的视图都有效
视图:在屏幕上能看见的全部东西属于视图

1.建立UIView的对象

let redView = UIView.init()

2.设置frame属性(由坐标(x,y)和大小(width,height)两部分组成

redView.frame = CGRectMake(10, 10, 100, 100)
a必须设置坐标和大小(默认坐标(0.0),大小(0,0)) b将视图添加到已经展现在屏幕上的视图上 ios中因此的结构都有一个对应的Make方法用来快速的建立一个结构体变量

3.把视图添加到界面上

self.view.addSubview(redView)

4.设置背景颜色

视图的背景颜色默认是透明
CGFloat就是UI中的浮点型
颜色建立方式:a经过类型方法建立指定颜色

redView.backgroundColor = UIColor.greenColor()

b经过三原色建立颜色(red,green,blue:红绿蓝的值(0-1) alpna:透明的值(0-1)

redView.backgroundColor = UIColor.init(red: 30/255.0, green: 133/255.0, blue: 26/255.0, alpha: 1)

总结:计算视图坐标的时候,注意相对性,当前视图被添加到那个视图上那么当前视图的坐标就是相对于谁来算的

相关属性

let readView = UIView.init() self.view.addSubview(readView)

设置背景颜色

readView.backgroundColor = UIColor.redColor()

1.frame(坐标和大小)

readView.frame = CGRectMake(100, 100, 100, 100)

2.center(中心点)
a经过frame和肯定视图的中心点坐标

print(readView.center)

b能够经过center的值,取改变视图的坐标

readView.center = CGPointMake(200, 200) print(readView.frame)

3.bounds(坐标和大小)
默认状况下bounds的坐标是(0.0),大小和视图的frame大小同样

了解:
rg改变bouns的大小,frame的大小和坐标都改变,center不变
若是改变bounds的坐标,不影响当前视图的位置。可是影响添加当前视图上的子视图不建议修该bounds

形变

transform(变形):当前视图发生形变,那么添加到当前视图上全部的视图会跟着一块儿形变
a缩放改变 参数1:x方向上的缩放比例,参数2y方向上的缩放比例

readView.transform = CGAffineTransformMakeScale(0.5, 0.5)

b.旋转(参数旋转角度(圆周率对应的角度值)(0-360 或 0 到 2*M_PI))

readView.transform = CGAffineTransformMakeRotation(CGFloat( M_PI_4))

c:多个变形同时发生 在另一个形变的前提下旋转 参数1:另一个形变 参数2:旋转角度

readView.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.5, 0.5), CGFloat(M_PI_4/2))

在另一个形变的前提下平移

readView.transform = CGAffineTransformTranslate(readView.transform, 0, 300)

在另外一个形变的前提下缩放

readView .transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(100, 0), 0.5, 2)

组合两个形变

readView.transform = CGAffineTransformConcat(readView.transform, CGAffineTransformMakeTranslation(0.5, 300))

父子视图

MARK:-父子视图的特色和方法
建立黄色视图

let yellView=UIView.init(frame: CGRectMake(0, 0, 50, 50)) yellView.backgroundColor=UIColor.yellowColor()

设置tag值,默认都是0,设置tag值的时候值必须大于0
tag的做用是区分界面不一样的视图

redVIew.tag=10

1.一个视图只有一个父视图,能够有多个子视图
连续将同一个视图添加到两个视图。最会添加有效

redVIew.addSubview(yellView)
         self.view.addSubview(yellView) yellView.tag=11

2获取一个视图的父视图

let auperView = redVIew.superview auperView?.backgroundColor = UIColor.greenColor()

3.获取一个视图的因此子视图

let sub = self.view.subviews

4.经过tga值拿到指定的值的子视图

let sub1 = self.view.viewWithTag(11) sub1?.frame.origin.y+=200

层次关系

在一个视图上,添加多个子视图的时候,子视图之间若是有公共的部分,那么后添加的子视图会覆盖先添加的
通常状况下,若是想要将一个视图显示在最下面,就最后添加,要想显示在最上面的就最后添加
建立视图

let view1 = self.creatView(CGRectMake(50, 100,100,100), backColor: UIColor.yellowColor()) let view2 = self.creatView(CGRectMake(100, 150,100,100), backColor: UIColor.redColor()) let view3 = self.creatView(CGRectMake(150, 200,100,100), backColor: UIColor.greenColor()) let view4 = self.creatView(CGRectMake(180, 150,100,100), backColor: UIColor.blueColor())

将指定的视图放到最上层

self.view.bringSubviewToFront(view1)

将指定视图放到最下层

self.view.sendSubviewToBack(view3)

将指定的视图插入到另外一个视图上面

self.view.insertSubview(view3, aboveSubview: view2)

将指定的视图插入到另外一个视图的下面

self.view.insertSubview(view2, belowSubview: view1)

动画

动画 UIView的动画是用来改变更画的视图frame相关属性,背景颜色透明度 形变等
下面介绍4种
建立视图

subView.frame = CGRectMake(0, 0, 100, 100) subView.center = self.view.center subView.backgroundColor = UIColor.blueColor() self.view.addSubview(subView) layearAction() UIViewAnimation1()

参数1:动画时间 参数2:延迟时间 参数3:整个动画完成以后自动调用这个方法

func UIViewAnimation1(){ UIView.animateWithDuration(2, animations: { self.subView.transform = CGAffineTransformMakeTranslation(0, -300) }) { (_) -> Void in // 动画结束须要执行的代码 UIView.animateWithDuration(3, animations: { self.subView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4)) self.subView.transform=CGAffineTransformMakeTranslation(0, 0) }) } }
参数1:动画时间 参数2:延迟时间 参数3:选项 (.Repeat动画重复执行,.Autoreverse自动回到动画开始的状态) 参数4:动画结束时视图状态的闭包 参数5:整个动画过程完成后须要执行的闭包
func UIViewAnimation2(){ UIView.animateWithDuration(1, delay: 0.5, options: [ .Repeat, .Autoreverse,.CurveEaseInOut], animations: { // self.subView.alpha = 0.6 self.subView.transform = CGAffineTransformMakeTranslation(0, -300) self.subView.transform = CGAffineTransformMakeRotation(CGFloat(360)) self.subView.backgroundColor = UIColor.greenColor() }, completion: nil) }

功能:执行这个方法前视图状态,动画切换到闭包里面设置的最终状态 参数1:动画时间(单位秒) 参数2:设置动画结束是的视图状态

func UIViewAnimation3(){ UIView.animateWithDuration(1) { // 在这设置视图动画结束是状态 self.subView.frame.origin.y=50 self.subView.frame.size = (CGSizeMake(50, 50)) self.subView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4)) self.subView.backgroundColor = UIColor.redColor() // 设置透明度 self.view.alpha = 0.3 } }
参数1:动画时间 参数2:延迟时间 参数3:弹簧板压力系数 参数4:弹簧初始加速度 参数5:选项 参数6:设置动画结束视图状态 参数7:动画结束后执行的闭包
func UIViewAnimation4(){ UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [ .Repeat, .Autoreverse], animations: { self.subView.alpha = 0.6 self.subView.transform = CGAffineTransformMakeTranslation(0, -300) self.subView.transform = CGAffineTransformMakeRotation(CGFloat(720)) // 注意:对于有圆角的矩形,改变大小而又不影响形状,只能经过形变取缩放,不能直接改变frame中的size self.subView.transform = CGAffineTransformMakeScale(0, -300 ) self.subView.backgroundColor = UIColor.randomColor() }, completion: nil) }

补充layer 属性是负责视图的形状(显示) 切圆角 当圆角值为正方形的一半,就能够切一个圆

func layearAction(){ self.subView.layer.cornerRadius = 20 //设置边框 self.subView.layer.borderWidth = 3 self.subView.layer.borderColor = UIColor.randomColor().CGColor }

UIlable

UIlable:UiView -> UIView的属性方法UIlabel 都拥有,从UIView继承下来的属性

// 建立UIlabel对象 let label = UILabel.init(frame: CGRectMake(10, 10, 400, 600)) // 添加到界面 self.view.addSubview(label) // 设置颜色 label.backgroundColor = UIColor.greenColor() // 1.UILabel特有的属性 // 设置label上显示文字 label.text = "《书湖阴先生壁》茅檐常扫净无苔,花木成畦手自栽。一水护田将绿绕,两山排闼送青来。" // 拿到lable上的文字 print(label.text) // 2.设置字体(默认大小17) // 使用系统字体 设置字体大小 label.font = UIFont.systemFontOfSize(26) // 参数1:字体大小 参数:2 字体粗细 使用系统字体 label.font = UIFont.systemFontOfSize(36, weight: 6) // 使用系统粗体 设置大小 label.font = UIFont.boldSystemFontOfSize(36) // 使用系统斜体 设置字体大小 label.font = UIFont.italicSystemFontOfSize(66) // 3.设置文本颜色(默认黑色) label.textColor = UIColor.redColor() // 设置阴影颜色 label.shadowColor = UIColor.blueColor() // sz阴影的偏移 label.shadowOffset.height=6 label.shadowOffset.width=6 label.shadowOffset = CGSizeMake(0, 1) // 设置文字的剧中模式(默认左对齐) label.textAlignment = .Center // 设置行数 label.numberOfLines = 15 // 自动换行 // label.numberOfLines = 0 // 设置换行模式 ByWordWrapping以单词为单位换行 ByCharWrapping 以字符为单位换行 label.lineBreakMode = .ByCharWrapping

补充使用本身字体的步骤
1.将ttf文件拖到工程中
2.在info.plist文件中添加键值对 Fontsprovided by appli 将字体添加到系统字体库
3.经过提供系统名的构造方法取建立字体(要先找到本身添加的字体的字体名

label.font = UIFont.init(name: "HYZhuanShuF", size: 26)

根据文字设置label大小

须要显示在Lable上的文字

let str = "ikdh" // 计算显示指定文字所须要的最小空间 // 1.将swift的字符串转换成OC的字符串 let ocstr = str as NSString // 2.计算字符串的大小 // 参数1:限制显示当前字符串的最大宽度和长度 // 参数2:渲染方式(UsesLineFragmentOrigin) // 参数3:肯定文字字体大小(NSFontAttributeName)字体对应的Key值 NSForegroundColorAttributeName颜色值 // 参数4: let strSize = ocstr.boundingRectWithSize(CGSizeMake(200, 200), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(17)], context: nil).size print(strSize) // 建立lable显示文字 let lable = UILabel.init(frame: CGRectMake(100, 100, strSize.width, strSize.height)) lable.backgroundColor = UIColor.blueColor() self.view.addSubview(lable) lable.text = str lable.numberOfLines = 0

UIImageView

建立UIImageView对象 let img = UIImageView(frame: CGRectMake(100, 100, 200, 300)) // 设置背景颜色 img.backgroundColor = UIColor.yellowColor() // 添加到界面 self.view.addSubview(img) // UIImageView专有的属性 // image属性 // 经过图片名去建立一个图片对象(注意:若是图片的格式是png,那么图片名的后缀能够省略,可是其余格式的图片的图片后缀名不能省略 img.image = UIImage.init(named: "luffy") // 经过图片路径去建立一个图片对象 // 将文件(处了Swift文件)放到工程中实质是放到当前应用程序的包文件中 // 拿到工程中图片路径先要获取包文件的路径 // 拿到包中的指定的文件路径 let k = NSBundle.mainBundle().pathForResource("luffy", ofType: "png") img.image = UIImage(contentsOfFile: k!) // 比较经过图片名和经过图片地址建立图片对象 的两种方法 // 1.1.经过图片名建立的图片对象在程序结束后才会被销毁,只会建立一次,经过图片地址建立的图片对象是当前图片对象不在使用的时候就销毁 // 1.2.使用图片名建立图片的状况下:建立小图标的时候,在工程中会重复使用的图片 // 1.3.使用图片地址建立图片对象的状况:不会频繁的在多个界面出现大图 // 2.内容模式 img.contentMode = .ScaleToFill
帧动画
//调用下面建立的帧动画的方法 CrectImage() // 建立一个定时器,而且自动开启 // 参数1.定时间 参数2.调用对象的方法 参数3.存储定时时间到了之后须要调用的方法(能够不带参也能够带参,带参数只能带一个参数,而且参数类型是NSTimer类型) 参数4.给当前的NSTimer的userInfo属性赋值 参数5. 是否重复 // 功能每一个0.1秒 self取调用一次timerAction方法 NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "timerAction:", userInfo: nil, repeats: true) } // 参数:当前定时器 func timerAction(timer:NSTimer){ self.imgView.frame.origin.x += 6 // 判断小鸟是否飞到了屏幕边缘 if self.imgView.frame.origin.x >= self.view.bounds.width-self.imgView.bounds.width{ self.imgView.frame.origin.y += 60 self.imgView.frame.origin.x -= 60 // 中止定时器 // timer.fireDate = NSDate.distantFuture() // 继续 // timer.fireDate = NSDate.distantPast() } if self.imgView.frame.origin.y >= self.view.bounds.width-self.imgView.bounds.width{ self.imgView.frame.origin.y -= 60 self.imgView.frame.origin.x -= 60 } } func CrectImage(){ // 建立一UIImageView // 经过图片建立一个Image:UIImageView的大小是图片大小坐标是(0.0) imgView = UIImageView.init(image: UIImage.init(named: "DOVE 1.png")) // 在界面显示 self.view.addSubview(imgView) // 3.使用UIImageview播放帧动画 // 3.1设置帧动画数组 var imgAray = [UIImage]() for item in 1...18{ let imgname = "DOVE \(item).png" let img = UIImage.init(named: imgname) imgAray.append(img!) } imgView.animationImages = imgAray // 3.2设置动画时间,单位秒 imgView.animationDuration = 0.5 // 设置动画重复次数(默认是0,无线循环) imgView.animationRepeatCount = 0 // 3.4开始动画 imgView.startAnimating() }

UIButton(按钮)

文字按钮

// 1.建立UIButton对象 let botton = UIButton.init(frame: CGRectMake(100, 100, 200, 200)) // 2.设置背景颜色 botton.backgroundColor = UIColor.redColor() // 3.添加到视图 self.view.addSubview(botton) // 4.设置按钮上显示的文字 // 参数1 想要在按钮上显示的文字 参数2 :状态 // Normal-》正常状态显示没有被点击 Highlighted-》高量状态按下去尚未弹起来的时候状态 ->Selected选中状态 -》.Disabled不可用状态 botton.setTitle("世界你好布!", forState: .Normal) botton.setTitle("我很好!", forState: .Highlighted) // 设置当前按钮是否选中(默认是false) botton.selected = false botton.setTitle("世界你好1!", forState: .Selected) // 设置不可用(默认是true) botton.enabled = true botton.setTitle("不可用", forState: .Disabled) // 设置文字颜色(能够给不一样的状态设置不一样的颜色) botton.setTitleColor(UIColor.yellowColor(), forState: .Highlighted) // 注意按钮上的文字和颜色,必须经过对应的set方法取根据状态设置,其余相关的属性能够经过拿到titlelabel取设置 // 设置按钮文字字体 botton.titleLabel?.font = UIFont.systemFontOfSize(38) // 设置对齐方式 botton.titleLabel?.textAlignment = .Center // 给按钮添加时间 参数1:调用方法的对象 参数2 :指定时间发生后参数1要去调用的方法(能够不带参数,带只能带一个 而且必须是UIButton) 参数3:设置事件 // TouchDown->按下时间 ->按下弹起事件 // 功能当按钮被按下的时候self就会取调用buttonAction方法 botton.addTarget(self, action: "buttonAction:", forControlEvents: .TouchDown)

图片按钮

let imageButton = UIButton.init(frame: CGRectMake(100, 300, 200, 200)) // 设置背景图片 参数1 :图片 参数2 :状态 imageButton.setImage(UIImage.init(named: "luffy4"), forState: .Normal) imageButton.layer.borderColor = UIColor.init(red: CGFloat(arc4random()%256)/255, green: CGFloat(arc4random()%256)/255, blue: CGFloat(arc4random()%256)/255, alpha: 1).CGColor // 添加点击时间 imageButton.addTarget(self, action: "buttonAction1:", forControlEvents: .TouchDown)

图片文字按钮

let itbutton = UIButton.init(frame: CGRectMake(100, 500, 200, 200)) self.view.addSubview(itbutton) itbutton.setTitle("你好", forState: .Normal) // itbutton.setImage(UIImage.init(named: "luffy"), forState: .Normal) itbutton.setTitleColor(UIColor.greenColor(), forState: .Normal) // 添加事件 itbutton.addTarget(self, action: "buttonAction1:", forControlEvents: .TouchDown) itbutton.setBackgroundImage(UIImage.init(named: "luffy"), forState: .Normal) itbutton.titleLabel?.font = UIFont.boldSystemFontOfSize(36) }

UITextField(文本框)

//UITextField:UIControl:UIView //===========UIView的属性和方法======= //1.建立UITextField对象 let textField = UITextField.init(frame: CGRectMake(100, 100, 200, 50)) //2.添加到界面上 self.view.addSubview(textField) //3.设置背景颜色 textField.backgroundColor = UIColor.yellowColor() //4.再建立一个UITextField对象 let textField2 = UITextField.init(frame: CGRectMake(100, 200, 200, 50)) self.view.addSubview(textField2) textField2.backgroundColor = UIColor.yellowColor() textField2.delegate = self //=====textField的专有属性和方法==== //(1)文字相关 //1.text属性 //设置文本输入框的内容 textField.text = "你好世界" //拿到文本输入框的内容 print(textField.text) //2.文字颜色 textField.textColor = UIColor.brownColor() //3.设置文字字体 textField.font = UIFont.systemFontOfSize(14) //4.设置占位文字(在输入框的内容为空的时候就会显示出来) textField.placeholder = "请输入帐号" //5.设置文本的对齐方式(默认是:左对齐) textField.textAlignment = .Left //6.密文显示(默认是false) textField.secureTextEntry = true //(2)显示相关 //1.设置文本框的样式 textField.borderStyle = .RoundedRect //2.设置清除按钮模式(清除按钮实质是rightView) //(前提是输入框中有文字) //Always -> 一直显示 //Never -> 从不显示(默认) //WhileEditing -> 当文本输入框处于编辑状态的时候才显示 //UnlessEditing ->当文本输入框处于非编辑状态的时候才显示 //注:当文本输入框中有光标的时候就是处于编辑状态 textField.clearButtonMode = .Always //3.左视图 let leftImageView = UIImageView.init(frame: CGRectMake(0,0, 40,40)) leftImageView.image = UIImage.init(named: "luffy1") //设置左视图 textField.leftView = leftImageView //设置左视图的显示模式(肯定何时显示,默认是从不显示) textField.leftViewMode = .Always //4.右视图 //当右视图显示的时候,清除按钮不能显示 /* let rightLabel = UILabel.init(frame: CGRectMake(0, 0, 40, 40)) rightLabel.text = "你好" textField.rightView = rightLabel textField.rightViewMode = .Always */ //(3)键盘相关 //1.设置键盘上返回按钮的样式 textField.returnKeyType = .Search //2.键盘样式 textField.keyboardType = .Default //3.设置自定义的键盘 //自定义的键盘,只有高度有效。宽度是屏幕的宽度 let myInputView = UIView.init(frame: CGRectMake(0, 0, 0, 256)) myInputView.backgroundColor = UIColor.redColor() //textField.inputView = myInputView //4.设置子键盘 let accessoryView = UIView.init(frame: CGRectMake(0, 0, 0, 50)) accessoryView.backgroundColor = UIColor.greenColor() textField.inputAccessoryView = accessoryView //(4)设置代理 //textField ->委托 //self -> 代理 textField.delegate = self

拓展

extension ViewController: UITextFieldDelegate{ //1.当按键盘上的返回按钮的时候,会自动调用 func textFieldShouldReturn(textField: UITextField) -> Bool{ print("返回按钮被点击") //收起键盘(结束编辑) //1.放弃第一响应者 textField.resignFirstResponder() //2.直接让指定的textField结束编辑 textField.endEditing(true) //3.让self.view上的全部的子视图都结束编辑 self.view.endEditing(true) return true } //2.当点击textField弹出来的键盘上的按键的时候会自动调用这个方法 //参数1:委托 //参数2:当前输入的字符所在的位置 //参数3:当前输入的字符串(在键盘上按的键的值) //返回值:是否可改变textField的text属性();false -> 按键盘上的按键无效 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{ print(range) print(string) if string == "0" { print("进入秘密页") } return true } //3.当文本输入框已经结束编辑的时候会自动调用这个方法 func textFieldDidEndEditing(textField: UITextField){ print("已经结束编辑") } //4.当文本输入框已经开始编辑的时候会自动调用这个方法 func textFieldDidBeginEditing(textField: UITextField){ print(textField.text) print("已经开始编辑") } //5.当文本输入框将要结束编辑的时候会自动调用这个方法 //返回:设置当前的textField是否能够结束编辑(默认是true) func textFieldShouldEndEditing(textField: UITextField) -> Bool{ print("将要结束编辑") //要求文本输入框中的文字长度要大于等于8的时候才能结束编辑 if textField.text?.characters.count >= 8 { return true } return false } //6.在textField将要开始编辑的时候会自动调用 //参数:当前这个协议对应的委托 //返回值:设置当前的textField是否能够进行编辑(默认是true) func textFieldShouldBeginEditing(textField: UITextField) -> Bool{ print("将要开始编辑") return true //false ->让textField不能进行编辑 } }

.UISwitch(开关)

func creatSwitch() { //1.建立开关对象 //UISwitch:UIControl:UIView let sw = UISwitch.init(frame: CGRectMake(100, 100, 100, 50)) //2.添加到界面上 self.view.addSubview(sw) //3.核心属性:开关状态(默认是:关) //设置开关的状态 sw.on = true //false -> 关 sw.setOn(false, animated: true) //拿到当前的状态 print(sw.on) //4.核心方法: //参数1:调用方法的对象 //参数2:指定的事件发生后参数1要去调用的方法对应的selector //参数3:事件 //功能:当开关的值(开关的状态)发生改变的时候,self会去调用switchAction方法 sw.addTarget(self, action: "switchAction:", forControlEvents: .ValueChanged) //5.设置开关开的状态的颜色(默认是绿色) sw.onTintColor = UIColor.redColor() //6.开关关闭的时候的边框颜色 sw.tintColor = UIColor.purpleColor() //7.设置开关上的滑块的颜色 sw.thumbTintColor = UIColor.yellowColor()

UISlider(滑条)

//1.建立滑条对象 //UISlider:UIControl:UIView let slider = UISlider.init(frame: CGRectMake(100, 160, 200, 20)) //2.添加到界面上 self.view.addSubview(slider) //3.核心属性:值 //value:滑块的位置对应的值(默认是0~1) slider.value = 0.5 //最小值和最大值 slider.minimumValue = 0 slider.maximumValue = 100 //4.核心方法 slider.addTarget(self, action: "sliderAction:", forControlEvents: .ValueChanged) //5.是否连续改变 slider.continuous = false //文字图片给以前的用法是同样的就不一一介绍了

UIStepper(步进器)

//1.建立步进器对象 let stepper = UIStepper.init(frame: CGRectMake(100, 200, 100, 50)) //2.添加到界面上 self.view.addSubview(stepper) //3.核心属性:值 //当前值 stepper.value = 1 print(stepper.value) //最小值和最大值 stepper.minimumValue = 0 stepper.maximumValue = 10 //步进(每按一下加或者减,增长/减小的值) stepper.stepValue = 1 //步进值必须大于0 //4.核心方法 stepper.addTarget(self, action: "stepperAction:", forControlEvents: .ValueChanged) //5.设置值是否连续改变(按住不放的时候) stepper.continuous = false //6.设置是否重复 false->按住不放的时候不计数;true->按住不放的时候计数(默认) stepper.autorepeat = false //7.设置填充颜色 stepper.tintColor = UIColor.redColor()

UIProgressView(进度条)

func creatProgress() { //1.建立进度条对象 //UIProgressView : UIView let progress = UIProgressView.init(frame: CGRectMake(100, 300, 200, 20)) progress.tag = 100 //2.添加到界面上 self.view.addSubview(progress) //3.核心属性 //进度:0~1 //设置当前进度 progress.progress = 0.5 progress.setProgress(0.6, animated: true) //4.颜色相关 //5.图片相关

UIActivityIndicatorView(活动指示器)

func creatActivity() { //1.建立活动指示器对象 //UIActivityIndicatorView : UIView let activity = UIActivityIndicatorView.init(frame: CGRectMake(100, 360, 50, 50)) //2.添加到界面上 self.view.addSubview(activity) //3.想要让活动指示器显示,必须让它开始动画 activity.startAnimating() //4.中止动画->活动指示器就会消失 //activity.stopAnimating() //5.设置活动指示器的样式 activity.activityIndicatorViewStyle = .WhiteLarge

UISegmentedControl(多段选择器)

func creatSegement() { //1.建立多段选择器对象 //UISegmentedControl : UIControl //参数1:分段选择器上的内容对应的数组 let segemnet = UISegmentedControl.init(items: ["海贼王","火影忍者","死神"]) segemnet.frame = CGRectMake(100, 400, 200, 50) //2.显示在界面上 self.view.addSubview(segemnet) //3.核心属性 //a.每一个分段上的内容 ->经过建立分段选择器的时候去设置 //b.当前选中的分段的下标(从0开始) segemnet.selectedSegmentIndex = 0 //4.核心方法 segemnet.addTarget(self, action: "segementAction:", forControlEvents: .ValueChanged) //5.拿到分段选择的分段数 print(segemnet.numberOfSegments) //6.设置填充颜色 segemnet.tintColor = UIColor.whiteColor() }

以上事件响应

extension ViewController{ //4.分段选择器事件 func segementAction(segement:UISegmentedControl) { print(segement.selectedSegmentIndex) //拿到当前被选中的分段的title print(segement.titleForSegmentAtIndex(segement.selectedSegmentIndex)) } //3.步进器 func stepperAction(stepper:UIStepper) { print(stepper.value) } //2.滑条 func sliderAction(slider:UISlider) { print(slider.value) //拿到进度条 let progress = self.view.viewWithTag(100) as! UIProgressView let t = slider.value/(slider.maximumValue - slider.minimumValue) progress.setProgress(t, animated: true) } //1.开关事件 func switchAction(sw:UISwitch) { if sw.on { print("开关打开") }else{ print("开关关闭") } } }

警告框

//1.建立一个表单视图 //UIAlertController : UIViewController //参数1:标题 //参数2:信息 //参数3:风格(ActionSheet->表单,Alert->警告框) let alterController = UIAlertController.init(title: nil, message: "消息", preferredStyle: .Alert) //2.添加到界面上 //参数1:须要显示的视图控制 self.presentViewController(alterController, animated: true, completion: nil) //3.添加选项按钮 //参数1:选项对应的按钮上的文字 //参数2:风格 //参数3:当当前选项对应的按钮被点击后会执行的代码对应的闭包 let action = UIAlertAction.init(title: "相机", style: .Default) { (_) in print("相机被点击") } //Destructive风格 let action2 = UIAlertAction.init(title: "相册", style: .Destructive) { (_) in print("相册被点击") } //Cancel风格 let action3 = UIAlertAction.init(title: "取消", style: .Cancel) { (_) in print("取消") } //添加相机对应的action alterController.addAction(action) //添加相册对应的action alterController.addAction(action2) // alterController.addAction(action3)

UITextView (显示区文本)

//1.建立textView对象 //UITextView:UIScrollView : UIView textView = UITextView.init(frame: CGRectMake(100, 100, 200, 70)) //2.添加到界面上 self.view.addSubview(textView) //3.设置背景颜色 textView.backgroundColor = UIColor.yellowColor() //4.text属性 textView.text = " default is nil. Can be useful with the appearance proxy on custom UIView subclasses." //5.设置是否能够选中和编辑 //默认是true -> 能够选中和编辑 textView.selectable = true //6.是否能够选中删除全部 textView.clearsOnInsertion = true //7.其余的属性和方法参考UITextField
//在点击当前页面的时候会自动调用这个方法 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { //5.获取选中的范围 let range = textView.selectedRange print(range) } }
相关文章
相关标签/搜索