日常随笔第五弹

尝试创建简单的视图

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let redview = UIView()//创建一个视图
        redview.frame = CGRect(x:100, y:50 , width:200 ,height: 200)//定义视图所在的位置以及宽高
        redview.backgroundColor = UIColor.redColor()//定义视图背景颜色
        self.view.addSubview(redview)//将视图放在界面的子视图下
        
        let blueview = UIView()
        blueview.frame = CGRect(x:100, y:50 , width: 100, height: 100)
        blueview.backgroundColor = UIColor.blueColor()
        self.view.addSubview(blueview)//blueview和Subview时self的子视图
        
        let greenView = UIView(frame: CGRectMake(100,50,100,100))
        greenView.backgroundColor = UIColor.greenColor()
        self.view.addSubview(greenView)
        redview.addSubview(blueview)
        redview.alpha = 0.7//设置视图透明度
        redview.hidden = true//将视图进行隐藏
        
        // 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.
    }
}

循环语句使用

var weather = "Sun"
switch weather{
    case "Rain" :
    print("出行不便")
    case "Sun" :
    print("可以出行")
    fallthrough//继续执行下一条
    default :
    print("出行不便")
}
//for  循环指定范围(0到5)
for xx in 0 ... 5{
    print(xx)
}
//for 循环指定范围(0到4)
for xx1 in 0..<5{
    print(xx1)
}

var j = 0
var i = 0
repeat{
    i = j+1
    j += 1
    print(i)
}while j < 4//使用repeat while执行循环,使用while条件来结束循环

var i = 0
while i < 5{//使用while来循环,给出结束循环条件
    print(i)
    i += 1
}

let arr = [1,2,3,4,5]
for i in arr {
    print(i)
}

let dict = ["key1":12 , "key3":34 , "key2":23 , "key4":45 , "key5":56]
for _ in dict{//使用下划线占位,可能暂时用不到该数据
    print(xx.0,xx.1)
}
for (key,value) in dict{
    print("\(key):\(value)")
}

课后习题

var xx1 :CGFloat = 250
        for xx in 1...4{
            var yy1 :CGFloat = 250
            for yy in 1...5-xx{
                let redview = UIView()
                redview.frame = CGRectMake(xx1 ,yy1 ,40 ,40 )
                if xx == 3 && yy == 2
                {
                    redview.backgroundColor = UIColor.greenColor()
                }
                else{
                    redview.backgroundColor = UIColor.redColor()
                }
                self.view.addSubview(redview)
            yy1 -= 50
            }
            xx1 -= 50
        }

 var y : CGFloat = 10
 var i = 3
 repeat{
        let label = UILabel()
        label.text = "hello the world"
        label.frame = CGRectMake(100, y, 200, 100)
        self.view.addSubview(label)
        i -= 1
        y += 50
}while i > 0
        

你可能感兴趣的:(日常随笔第五弹)