04 從程式製作國旗圖案🏳️‍⚧️

分別製作了德國🇩🇪法羅群島🇫🇴孟加拉人民共和國🇧🇩

Germany 德國國旗

我的寫法是拉一個底,再加上兩個不同顏色的長方形。

加入rect 再慢慢來調整座標和長寬大小就完成嚕😚

但現在寫程式動作還很慢,摸索熟悉中,希望能越打越順.✌🏻🤘🏻✌🏻

import UIKit

var rect = CGRect(x: 0, y: 0, width: 500, height: 300)
let blackView = UIView(frame: rect)
blackView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1)

//加入紅色塊
rect = CGRect(x: 0, y: 100, width: 500, height: 100)
let redView = UIView(frame: rect)
redView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
blackView.addSubview(redView)

//加入黃色塊
rect = CGRect(x: 0, y: 200, width: 500, height: 100)
let yellowView = UIView(frame: rect)
yellowView.backgroundColor = UIColor(red: 238/255, green: 204/255, blue: 0/255, alpha: 1)
blackView.addSubview(yellowView)

Faroe Islands

法羅群島國旗

利用Peter上課講的 subview&superview觀念來疊加.

import UIKit

var rect = CGRect(x: 0, y: 0, width: 460, height: 300)
let backgroundView = UIView(frame: rect)
backgroundView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
//加入藍色塊
rect = CGRect(x: 0, y: 110, width: 460, height: 75)
let blueView = UIView(frame: rect)
blueView.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 1)
backgroundView.addSubview(blueView)

//加入藍色塊2
rect = CGRect(x: 120, y: 0, width: 75, height: 460)
let blueView2 = UIView(frame: rect)
blueView2.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 1)
backgroundView.addSubview(blueView2)

//加入紅色塊
rect = CGRect(x: 0, y: 135, width: 460, height: 30)
let redView = UIView(frame: rect)
redView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
backgroundView.addSubview(redView)

//加入紅色塊2
rect = CGRect(x: 143, y: 0, width: 30, height: 460)
let redView2 = UIView(frame: rect)
redView2.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
backgroundView.addSubview(redView2)

Bangladesh
孟加拉人民共和國

有點偏移的原中心,很酷的國家🤭

import UIKit

var rect = CGRect(x: 0, y: 0, width: 460, height: 280)
let backgroundView = UIView(frame: rect)
backgroundView.backgroundColor = UIColor(red: 46/255, green: 139/255, blue: 87/255, alpha: 1)

//加入圓圈
rect = CGRect(x: (backgroundView.frame.width - 240)/2, y: (backgroundView.frame.height - 170) / 2, width:150, height: 150)
let circleView = UIView(frame: rect)
circleView.backgroundColor = UIColor(red: 230/255, green: 0, blue: 92/255, alpha: 1)
backgroundView.addSubview(circleView)
circleView.layer.cornerRadius = 75
backgroundView

參考連結:

--

--