在上一篇只是简单实现了九宫图效果,本章需要形成APP界面九宫图效果
override func viewDidLoad() {
super.viewDidLoad()
createnine()
}
//九宫图算法
func createnine(){
//高
let kAppViewH:CGFloat=80
//宽
let kAppViewW:CGFloat=80
//行个数
let kColCount:Int=3
//间隔
let kStart:Int=20
let MarginX:CGFloat=(self.view.frame.size.width-CGFloat(kColCount)*kAppViewW)/CGFloat(kColCount+1)
let MarginY:CGFloat=10
for i in 0 ..< 12 {
let row:Int=i/kColCount;
let col:Int=i%kColCount;
let colF:CGFloat=CGFloat(col);
let x=MarginX+CGFloat(col)*(kAppViewW+MarginX);
let y=CGFloat(kStart)+MarginY+CGFloat(row)*(kAppViewH+MarginY);
var imageView=UIImageView(image: UIImage(named: ""))
imageView.frame=CGRect(x: 0, y: 0, width: 60, height: 60)
//增加Lable
var nameLabel=UILabel(frame: CGRect(x:0, y: 60, width: Int(kAppViewW), height: 20))
nameLabel.text="功能"
nameLabel.textAlignment=NSTextAlignment.center
nameLabel.backgroundColor=UIColor.blue
//var btnBuuton=UIButton.button
//增加按钮
let btnButton=UIButton(frame: CGRect(x: 0, y: 20, width: 60, height: 60))
btnButton.tag=i
btnButton.setTitle("按钮", for: UIControlState.normal)
btnButton.addTarget(self, action: #selector(btnClick(sender:)), for:
.touchUpInside)
let view=UIView(frame: CGRect(x: x, y: y, width: kAppViewW, height: kAppViewH))
view.backgroundColor=UIColor.red
view.addSubview(imageView)
view.addSubview(nameLabel)
view.addSubview(btnButton)
//加入视图
self.view.addSubview(view)
}
}
效果如下