美文网首页
Note 4 UI简单制作和循环语法

Note 4 UI简单制作和循环语法

作者: cry_0416 | 来源:发表于2016-07-17 19:36 被阅读34次

简单UI

let redView  = UIView() 
//创建新视图 //红色
let greenView = UIView() 
//绿色
let blueView = UIView(frame:CGRect(x:90,y:90,width: 50,height: 50)) 
//蓝色
greenView.backgroundColor = UIColor.greenColor()
//设置背景色

redView.frame = CGRectMake(100, 100, 100, 100)
//在界面中加入一个矩形区域,设置他的大小,矩形左上角相对屏幕左上角的坐标
redView.backgroundColor = UIColor.redColor() 
//设置背景色

greenView.frame = CGRectInset(redView.frame, 0 , 0)
blueView.backgroundColor = UIColor.blueColor() 
//设置背景色

self.view.addSubview(redView)//将redView显示在self.view上
//self.view.addSubview(greenView)
redView.addSubview(greenView)
//每个页面都有一个self.view 表示整个界面
//Subview,view2是self的子视图
//redView.addSubview(blueView)
//Subview,view1是newView的子视图
//同一个视图的子视图,根据添加顺序顺序叠放,后添加的显示在前
//父视图的属性会影响子视图

循环

if/else

if weekday == "星期二" {
print("休息")
}else if weekday == "星期六" {
print("自习")
}
else {
print("上课")
}

while

while num < 5{
    sum = sum + num
    num += 1
}

repeat while循环 与while一样,但是repeat至少执行一次,相当于do while

var num = 0
repeat{
    sum = sum + num
    num += 1
}while num < 5
print(sum)

//用repeat的时候,while判断语句中的变量要注意

//经常用repeat的时候没注意去定义赋值num

switch/case

Switch/case循环
switch num {
case 1:
    print(1)
fallthrough//如果想执行玩继续往下执行
case 2:
    print(2)
case 3:
    print(3)
case 4:
    break//如果不想做任何事情用break
default:
    print("其他")
}

for

for i in 0..<5{
    print(i)
}
print("-------")

// 0...5,相当于[0,5]
for i in 0...5{
    print(i)
}
print("-------")

//依次取数组中的数
for i in [1, 2, 3, 5, 6]{
    print(i)
}
//依次取数组中的字符串
for i in ["i" , "am", "cry"]{
    print(i)
}
print("-------")

//"_"用来占位
for _ in 0...5{
    print("hello")
}
print("-------")

//依次读取字符串中的字符
let hi  = "hello"
for i in hi.characters {
    print(i)
    
}


//for在字典中遍历
let dict = ["key1":12,
            "key2":23,
            "key3":34]
//1.
for (k, v) in dict{
    print(k,v)
}
//2.
for pair in dict{
    print(pair.0,pair.1)
}
//print(pair.key,pair.value)
//3.0的swift才支持

今天的一个小作业

作业

用for循环


for line in 1...4{

    let yzb = line * ( 50 + 20 )
    
    for i in 1...line{
        let view = UIView()
        let xzb = ( 50 + 20 ) * i
        
        if i > 1 && i % 2 == 0 && line > 1 && line % 2 != 0{
            view.backgroundColor = UIColor.redColor()
        }else{
        view.backgroundColor = UIColor.greenColor()
        }
        
        view.frame = CGRect(x: xzb, y: yzb , width : 50 ,height : 50)
        self.view.addSubview(view)
} 

用repeat while循环

var line = 1
repeat{
  
    let yzb = line * 70
    
    var i = 1
    
        repeat{
       
        let view = UIView()
        let xzb = 70 * i
        
        if i > 1 && i % 2 == 0 && line > 1 && line % 2 != 0{
            view.backgroundColor = UIColor.redColor()
        }else{
            view.backgroundColor = UIColor.greenColor()
        }
        
        view.frame = CGRect(x: xzb, y: yzb , width : 50 ,height : 50)
        self.view.addSubview(view)
        i += 1
        
    }while i <= line
    
    line += 1
}while line <= 4

相关文章

网友评论

      本文标题:Note 4 UI简单制作和循环语法

      本文链接:https://www.haomeiwen.com/subject/znqpjttx.html