swift3.0 代碼建立經典界面的九宮圖--優化篇

在上一篇只是簡單實現了九宮圖效果,本章須要造成APP界面九宮圖效果算法

 

 override func viewDidLoad() {ide

        super.viewDidLoad()orm

        createnine()blog

    }get

 

//九宮圖算法it

    func createnine(){io

//高select

        let kAppViewH:CGFloat=80im

//寬        touch

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)

        }  

    }

 

 

效果以下 

相關文章
相關標籤/搜索