Swift基础之常用的UI控件(三)

在前两篇中介绍了UILabel、UIControl、UIButton和UITextField,在本篇文章中会介绍一下UIImageView、UITextView等等

UIImagevIew-图像视图

1.图像视图的创建

  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.从网络地址获取图片

 //定义NSURL对象
 let url = NSURL(string: "http://hangge.com/blog/images/logo.png")
 //从网络获取数据流
 let data = NSData(contentsOf: url! as URL)
 //通过数据流初始化图片
 let newImage = UIImage(data: data! as Data)
 let imageView = UIImageView(image:newImage);
 self.view.addSubview(imageView)

4.实现动画效果

  class ViewController: UIViewController {
 
       var imageView:UIImageView!
       override func viewDidLoad() {
       super.viewDidLoad()

       imageView=UIImageView()
       imageView.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
       //设置动画图片
       imageView.animationImages=[UIImage(named:"icon")!,UIImage(named:"icon2")!]
       //设置每隔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的创建

    let textview = UITextView(frame:CGRect(x: 10, y: 10, width: 100, height: 100))
    textview.layer.borderWidth=1  //边框粗细
    textview.layer.borderColor=UIColor.gray.cgColor //边框颜色
    textview.backgroundColor = UIColor.clear //背景颜色
    textview.backgroundColor = UIColor.clear
    textview.isEditable = true //设置为可编辑状态
    //可选范围
    textview.selectedRange = NSRange(location: 0, length: Int.max)
    //内容是否可选
    textview.isSelectable=false
    self.view.addSubview(textview)

2.UITextView的代理方法

UITextView的代理方法与UITextField的代理方法非常类似,在这里就不过多介绍了。大家千万不要忘记设置代理哦。{{{(>_<)}}}

   func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        print("开始输入")
        return true
   }
   func textViewDidBeginEditing(_ textView: UITextView) {
        print("已经开始输入")
   }
   func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        print("输入结束")
        return true
   }
   func textViewDidEndEditing(_ textView: UITextView) {
        print("已经输入结束")
   } 
   func textViewDidChange(_ textView: UITextView) {
        print("已经改变")
   }

UISwitch-开关按钮

    let switch1 = UISwitch()
    //设置开始位置
    switch1.center = CGPoint(x:100, y:100)
    //设置默认值
    switch1.isOn = true
    //添加事件
    switch1.addTarget(self, action: #selector(ViewController.switchDidChange), for: .ValueChanged)

UISegmentedControl-分段选择器

    //选项除了文字还可以是图片
    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 //默认选中第二项
    segmentedController.addTarget(self, action: Selector("segmentDidchange:"),for: UIControlEvents.valueChanged)  //添加值改变监听
    self.view.addSubview(segmentedController)
定义点击事件方法
    func segmentDidchange(segmented:UISegmentedControl){
        //获得选项的索引
        print(segmented.selectedSegmentIndex)
        //获得选择的文字
        print(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
    }
选择器中选项的添加、删除与修改
   //添加文字选项
   segmentedController.insertSegmentWithTitle("新增选项",atIndex:1,animated:true)
   //添加图片选项
   segmentedController.insertSegmentWithImage(UIImage(named:"icon")!,atIndex:1,animated: true)
   //移除选项
   segmentedController.removeSegmentAtIndex(1,animated:true)
   //修改选项颜色,包括图片
   segmentedController.tintColor=UIColor.blueColor()
   //修改选项文字
   segmentedController.setTitle("哎呦",forSegmentAtIndex:1)
   //修改选项图片
   segmentedController.setImage(UIImage(named:"icon"),forSegmentAtIndex:2)
   //修改选项内容偏移位置
   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  //当前默认值
   //设置滑块的值,同时有动画
   slider.setValue(0.8,animated:true)
   slider.continuous=false  //滑块滑动停止后才触发ValueChanged事件
   slider.addTarget(self,action:"sliderDidchange:", forControlEvents:UIControlEvents.ValueChanged)
   self.view.addSubview(slider)

UIProgressView-进度条

  // 创建进度条
  var progressView=UIProgressView(progressViewStyle:UIProgressViewStyle.Default)
  progressView.center=self.view.center
  progressView.progress=0.5 //默认进度50%
  self.view.addSubview(progressView)
  //设置进度,同时有动画效果
  progressView.setProgress(0.8,animated:true)
  //改变进度条颜色
  progressView.progressTintColor=UIColor.greenColor()  //已有进度颜色
  progressView.trackTintColor=UIColor.blueColor()  //剩余进度颜色(即进度槽颜色)

UIStepper-微调器或叫步进器

    super.viewDidLoad()
    stepper=UIStepper()
    stepper.center=self.view.center
    //设置stepper的范围与初始值
    stepper.maximumValue=10
    stepper.minimumValue=1
    stepper.value=5.5
    //设置每次增减的值
    stepper.stepValue=0.5
    //设置stepper可以按住不放来连续更改值
    stepper.continuous=true
    //设置stepper是否循环(到最大值时再增加数值从最小值开始)
    stepper.wraps=true
    stepper.addTarget(self,action:"stepperValueIschanged",
    forControlEvents: UIControlEvents.ValueChanged)
    label=UILabel(frame:CGRectMake(100,100,300,30))
    label.text = "当前值为:\(stepper.value)"
使用tintColor属性可以设置微调器的颜色。 而加减符号图标,背景图片,中间分割线图片都可以替换成自己图片
//用自定义的图片替换“-”和“+”
stepper.setDecrementImage(UIImage(named: "sub.png"),forState: UIControlState.Normal)
stepper.setIncrementImage(UIImage(named: "add.png"), forState: UIControlState.Normal)

关于简单常用的UI控件今天就介绍到这里了,在接下来的文章中会继续介绍关于UIAlertView、UIActionSheet等控件,希望大家继续关注 O(∩_∩)O~~~

你可能感兴趣的:(Swift基础之常用的UI控件(三))