在前兩篇中介紹了UILabel、UIControl、UIButton和UITextField,在本篇文章中會(huì)介紹一下UIImageView佳恬、UITextView等等
UIImagevIew-圖像視圖
1.圖像視圖的創(chuàng)建
let imageView = UIImageView(image:UIImage(named:"icon"))
imageView.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
self.view.addSubview(imageView)
//修改圖片
imageView.image = UIImage(named:"icon2")
//保持圖片比例
imageView.contentMode = .scaleAspectFit
2.從文件目錄中獲取圖片
let path = Bundle.main.path(forResource: "apple", ofType: "png")
let newImage = UIImage(contentsOfFile: path!)
let imageView = UIImageView(image:newImage)
self.view.addSubview(imageView)
3.從網(wǎng)絡(luò)地址獲取圖片
//定義NSURL對(duì)象
let url = NSURL(string: "http://hangge.com/blog/images/logo.png")
//從網(wǎng)絡(luò)獲取數(shù)據(jù)流
let data = NSData(contentsOf: url! as URL)
//通過數(shù)據(jù)流初始化圖片
let newImage = UIImage(data: data! as Data)
let imageView = UIImageView(image:newImage);
self.view.addSubview(imageView)
4.實(shí)現(xiàn)動(dòng)畫效果
class ViewController: UIViewController {
var imageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView=UIImageView()
imageView.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
//設(shè)置動(dòng)畫圖片
imageView.animationImages=[UIImage(named:"icon")!,UIImage(named:"icon2")!]
//設(shè)置每隔0.5秒變化一次
imageView.animationDuration=0.5
self.view.addSubview(imageView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.startAnimating()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.stopAnimating()
}
}
UITextView-多行文本輸入框
1.UITextView的創(chuàng)建
let textview = UITextView(frame:CGRect(x: 10, y: 10, width: 100, height: 100))
textview.layer.borderWidth=1 //邊框粗細(xì)
textview.layer.borderColor=UIColor.gray.cgColor //邊框顏色
textview.backgroundColor = UIColor.clear //背景顏色
textview.backgroundColor = UIColor.clear
textview.isEditable = true //設(shè)置為可編輯狀態(tài)
//可選范圍
textview.selectedRange = NSRange(location: 0, length: Int.max)
//內(nèi)容是否可選
textview.isSelectable=false
self.view.addSubview(textview)
2.UITextView的代理方法
UITextView的代理方法與UITextField的代理方法非常類似毁葱,在這里就不過多介紹了。大家千萬不要忘記設(shè)置代理哦倾剿。{{{(>_<)}}}
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
print("開始輸入")
return true
}
func textViewDidBeginEditing(_ textView: UITextView) {
print("已經(jīng)開始輸入")
}
func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
print("輸入結(jié)束")
return true
}
func textViewDidEndEditing(_ textView: UITextView) {
print("已經(jīng)輸入結(jié)束")
}
func textViewDidChange(_ textView: UITextView) {
print("已經(jīng)改變")
}
UISwitch-開關(guān)按鈕
let switch1 = UISwitch()
//設(shè)置開始位置
switch1.center = CGPoint(x:100, y:100)
//設(shè)置默認(rèn)值
switch1.isOn = true
//添加事件
switch1.addTarget(self, action: #selector(ViewController.switchDidChange), for: .ValueChanged)
UISegmentedControl-分段選擇器
//選項(xiàng)除了文字還可以是圖片
let items=["Hello" as AnyObject,"World" as AnyObject,UIImage(named: "star")!] as [AnyObject]
let segmentedController=UISegmentedControl(items:items)
segmentedController.center=self.view.center
segmentedController.selectedSegmentIndex=1 //默認(rèn)選中第二項(xiàng)
segmentedController.addTarget(self, action: Selector("segmentDidchange:"),for: UIControlEvents.valueChanged) //添加值改變監(jiān)聽
self.view.addSubview(segmentedController)
定義點(diǎn)擊事件方法
func segmentDidchange(segmented:UISegmentedControl){
//獲得選項(xiàng)的索引
print(segmented.selectedSegmentIndex)
//獲得選擇的文字
print(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
}
選擇器中選項(xiàng)的添加前痘、刪除與修改
//添加文字選項(xiàng)
segmentedController.insertSegmentWithTitle("新增選項(xiàng)",atIndex:1,animated:true)
//添加圖片選項(xiàng)
segmentedController.insertSegmentWithImage(UIImage(named:"icon")!,atIndex:1,animated: true)
//移除選項(xiàng)
segmentedController.removeSegmentAtIndex(1,animated:true)
//修改選項(xiàng)顏色芹缔,包括圖片
segmentedController.tintColor=UIColor.blueColor()
//修改選項(xiàng)文字
segmentedController.setTitle("哎呦",forSegmentAtIndex:1)
//修改選項(xiàng)圖片
segmentedController.setImage(UIImage(named:"icon"),forSegmentAtIndex:2)
//修改選項(xiàng)內(nèi)容偏移位置
segmentedController.setContentOffset(CGSizeMake(10,7),forSegmentAtIndex:1)
UISlider-滑塊
let slider=UISlider(frame:CGRectMake(0,0,300,50))
slider.center=self.view.center
slider.minimumValue=0 //最小值
slider.maximumValue=1 //最大值
slider.value=0.5 //當(dāng)前默認(rèn)值
//設(shè)置滑塊的值,同時(shí)有動(dòng)畫
slider.setValue(0.8,animated:true)
slider.continuous=false //滑塊滑動(dòng)停止后才觸發(fā)ValueChanged事件
slider.addTarget(self,action:"sliderDidchange:", forControlEvents:UIControlEvents.ValueChanged)
self.view.addSubview(slider)
UIProgressView-進(jìn)度條
// 創(chuàng)建進(jìn)度條
var progressView=UIProgressView(progressViewStyle:UIProgressViewStyle.Default)
progressView.center=self.view.center
progressView.progress=0.5 //默認(rèn)進(jìn)度50%
self.view.addSubview(progressView)
//設(shè)置進(jìn)度示罗,同時(shí)有動(dòng)畫效果
progressView.setProgress(0.8,animated:true)
//改變進(jìn)度條顏色
progressView.progressTintColor=UIColor.greenColor() //已有進(jìn)度顏色
progressView.trackTintColor=UIColor.blueColor() //剩余進(jìn)度顏色(即進(jìn)度槽顏色)
UIStepper-微調(diào)器或叫步進(jìn)器
super.viewDidLoad()
stepper=UIStepper()
stepper.center=self.view.center
//設(shè)置stepper的范圍與初始值
stepper.maximumValue=10
stepper.minimumValue=1
stepper.value=5.5
//設(shè)置每次增減的值
stepper.stepValue=0.5
//設(shè)置stepper可以按住不放來連續(xù)更改值
stepper.continuous=true
//設(shè)置stepper是否循環(huán)(到最大值時(shí)再增加數(shù)值從最小值開始)
stepper.wraps=true
stepper.addTarget(self,action:"stepperValueIschanged",
forControlEvents: UIControlEvents.ValueChanged)
label=UILabel(frame:CGRectMake(100,100,300,30))
label.text = "當(dāng)前值為:\(stepper.value)"
使用tintColor屬性可以設(shè)置微調(diào)器的顏色芝硬。 而加減符號(hào)圖標(biāo),背景圖片绍绘,中間分割線圖片都可以替換成自己圖片
//用自定義的圖片替換“-”和“+”
stepper.setDecrementImage(UIImage(named: "sub.png"),forState: UIControlState.Normal)
stepper.setIncrementImage(UIImage(named: "add.png"), forState: UIControlState.Normal)
關(guān)于簡單常用的UI控件今天就介紹到這里了皮官,在接下來的文章中會(huì)繼續(xù)介紹關(guān)于UIAlertView、UIActionSheet等控件藻丢,希望大家繼續(xù)關(guān)注 O(∩_∩)O~~~