swift学习之UI控件(一)

时间:2021-08-25 15:48:24

//

//  ViewController.swift

//  test

//

//  Created by chuangqu on 15/7/23.

//  Copyright (c) 2015年 theway. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

var lable = UILabel(frame: CGRectMake(120, 20, 100, 20))

lable.text = "小猪飞!"

lable.textColor = UIColor.blueColor()

lable.backgroundColor = UIColor.brownColor()

lable.textAlignment = NSTextAlignment.Center

lable.shadowColor = UIColor.grayColor()

lable.shadowOffset = CGSizeMake(-5, 5)

lable.font = UIFont(name: "zapfino", size: 20)

self.view.addSubview(lable)

var file = UITextField(frame: CGRectMake(120, 80, 100, 20))

file.text = "肖"

file.textColor = UIColor.blueColor()

self.view.addSubview(file)

var button:UIButton = UIButton.buttonWithType(UIButtonType.System)as!UIButton;

button.frame = CGRectMake(120, 150, 100, 80)

button.setTitle("猪fjygjyy", forState:UIControlState.Normal)

//        button.setImage(UIImage(named: "0"), forState: .Normal)

button.adjustsImageWhenHighlighted = false

button.setBackgroundImage(UIImage(named: "0"), forState: .Normal)//设置背景

button.addTarget(self, action: Selector("test"), forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button)

segmented ()

/*

创建一个文本框

*/

var textfield = UITextField(frame: CGRectMake(10, 20, 100, 20))

textfield.borderStyle = UITextBorderStyle.RoundedRect//设置文本框为圆边圆角

textfield.placeholder = "yuebu"//设置提示语句

//        textfield.text = "hhhh"//设置默认值

textfield.adjustsFontSizeToFitWidth = true //设置当超出文本时文字省略方式

//        textfield.textAlignment = NSTextAlignment.Center//设置文字的水平对齐属性

//        textfield.contentVerticalAlignment = UIControlContentVerticalAlignment.Center//垂直对齐方式

//简写

//        textfield.returnKeyType = UIReturnKeyType.Go//设置键盘的返回键作用

textfield.contentVerticalAlignment = .Center

textfield.clearButtonMode = .WhileEditing

//        textfield.clearButtonMode = UITextFieldViewMode.UnlessEditing

//        textfield.clearButtonMode = .UnlessEditing

self.view.addSubview(textfield)

//        kaiguan()

img ()

animation ()

//         progess()

//        test()

// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

func test() {

//        button.setTitle("小大", forState: UIControlState.Normal)//不能改变UI,需在UI线程中操作

println("我肾很好!")

}

func kaiguan (){

var uiswitch : UISwitch

uiswitch = UISwitch()

uiswitch.center = CGPointMake(100, 50)

uiswitch.on = true

self.view.addSubview(uiswitch)

}

func segmented (){

var items = ["张","萧","阿","姨"]//,UIImage(named: "icon")]

var seg = UISegmentedControl(items: items)

seg.center = self.view.center

seg.selectedSegmentIndex = 1

self.view.addSubview(seg)

}

func img (){

var imge = UIImageView(image: UIImage(named: "3"))

imge.frame = CGRectMake(90,350, 200, 200)

self.view.addSubview(imge)

}

var imageview : UIImageView!

func animation (){

imageview = UIImageView()

imageview.frame = CGRectMake(90,350, 200, 200)

//帧动画设置

//        imageview.animationImages = [UIImage(named:"0"),UIImage(named:"3")]

//设置变化时间

imageview.animationDuration = 0.5

self.view.addSubview(imageview)

}

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

imageview.startAnimating()

}

override func viewWillDisappear(animated: Bool) {

super.viewWillDisappear(animated)

imageview.stopAnimating()

}

//    override func DidLoad() {

//        super.viewDidLoad()

//        imageview = UIImageView()

//        imageview.frame = CGRectMake(90,350, 200, 200)

//        //帧动画设置

//        imageview.animationImages = [UIImage(named: "3"),UIImage(named: "3"),UIImage(named: "3")]

//        //设置变化时间

//        imageview.animationDuration = 0.5

//        self.view.addSubview(imageview)

//

//    }

func progess(){

var progres = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)

progres.center = self.view.center

progres.progress = 0.5

progres.setProgress(0.8, animated: true)

self.view.addSubview(progres)

var alert = UIAlertView()

alert.frame = CGRectMake(200, 150, 100, 100)

alert.title = "刘大神帅不"

alert.message = "是/否"

alert.addButtonWithTitle("是")

alert.addButtonWithTitle("否")

alert.cancelButtonIndex = 0

alert.delegate = self

alert.show()

hhh(alert, clickedButtonAlertIndex: alert.cancelButtonIndex)

}

func hhh(alert:UIAlertView,clickedButtonAlertIndex button:Int){

if button == alert.cancelButtonIndex {

print("不帅")

}

else{

print("不帅!!!!")

}

}

}