swift资源库-2-初识UI

来源:互联网 发布:linux ntp服务器配置 编辑:程序博客网 时间:2024/06/05 20:13


class ViewController: UIViewController {
    let View_W = UIScreen.mainScreen().bounds.size.width
    let View_H = UIScreen.mainScreen().bounds.size.height
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //
        
        view.backgroundColor = UIColor.grayColor()
        //UIVIEW
        //let 修饰 v 并且赋值,该常量的内存地址不允许修改,但是可以修改其内部的属性
        //常量&变量的使用原则:尽量先用 let,只有需要变的时候,再用 var,能够更加安全
        let v = UIView(frame: CGRectMake(0,0,180,100))
        v.backgroundColor = UIColor.redColor()
        v.alpha = 0.6
        v.center = CGPointMake(View_W/2, View_H/8)
        view.addSubview(v)
        
        text()
        
    }

    func text(){
        let b = UIButton(frame: CGRectMake(30,330,120,40))
        b.setTitle("点击", forState: UIControlState.Normal)
        b.setTitle("高亮", forState: UIControlState.Highlighted)
        b.setTitle("选择", forState: UIControlState.Selected)
        b.titleLabel?.text="normal"
        b.tag = 12;
        b.addTarget(self, action:Selector(btnclicked(b)), forControlEvents: UIControlEvents.TouchUpInside)
        b .setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        b.backgroundColor = UIColor.yellowColor()
        b.center = CGPointMake(View_W/2, View_H/3.5)
        view.addSubview(b)
        
        
    }
    //  按钮点击事件
    func btnclicked(sender:UIButton){
        if sender.tag == 5 {
            let alert = UIAlertController(title: "提醒", message: "点击",preferredStyle:  UIAlertControllerStyle.Alert)
            
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


0 0
原创粉丝点击