用程式設計國旗圖案
這次的作業是要用程式來繪製國旗,畫了以下的幾種範例:
- 首先,先依照範例繪製了一個日本國旗:
import UIKit
var japan = CGRect (x: 0, y: 0, width:200, height:134)
let japanBackGroundView = UIView(frame: japan)
japanBackGroundView.backgroundColor = UIColor (red:255/255, green:255/255, blue:255/255, alpha:1)
japan = CGRect(x:(japanBackGroundView.frame.width — 106)/2, y: (japanBackGroundView.frame.height — 106)/2, width:106, height:106)
let japanCircleView = UIView(frame:japan)
japanCircleView.backgroundColor = UIColor(red: 1,green: 0, blue: 0, alpha: 1)
japanBackGroundView.addSubview(japanCircleView)
japanCircleView.layer.cornerRadius = 53
japanCircleView.clipsToBounds = true
japanBackGroundView
2. 再來仿照日本國旗,畫出帛琉國旗
比較不一樣的地方在於,帛琉國旗的圓圈是比較偏左邊的,所以在座標上有動了一點小調整
var palau = CGRect (x: 0, y: 0, width:400, height:250)
let palauBackGroundView = UIView(frame: palau)
palauBackGroundView.backgroundColor = UIColor (red:0, green:0.6, blue:1, alpha:1)
palau = CGRect(x: 100, y: 50, width: 150, height: 150)
let palauCircleView = UIView(frame:palau)
palauCircleView.backgroundColor = UIColor(red:1, green:1, blue:0, alpha:1)
palauBackGroundView.addSubview(palauCircleView)
palauCircleView.layer.cornerRadius = 75
palauCircleView.clipsToBounds = true
palauBackGroundView
3. 法國國旗
今年才去過法國,所以就按照Peter的範例,繪製了一個法國國旗
var france = CGRect (x:0, y:0, width:300, height:200)
let franceBarWidth = france.width/3
let franceBackgroundView = UIView(frame:france)
let freedomBlue = UIView(frame: CGRect(x:0, y:0, width:franceBarWidth, height:franceBackgroundView.frame.height))
freedomBlue.backgroundColor = UIColor(red:0, green:85/255, blue:164/255, alpha:1)
franceBackgroundView.addSubview(freedomBlue)
let equalityWhite = UIView(frame:freedomBlue.frame.offsetBy(dx:franceBarWidth, dy:0))
equalityWhite.backgroundColor = UIColor(red:1, green:1, blue:1, alpha:1)
franceBackgroundView.addSubview(equalityWhite)
let universalLoveRed = UIView(frame:equalityWhite.frame.offsetBy(dx: franceBarWidth, dy: 0))
universalLoveRed.backgroundColor = UIColor(red: 250/255, green: 60/255, blue: 50/255, alpha: 1)
franceBackgroundView.addSubview(universalLoveRed)
4. 跟法國很像,只是直的變成橫的,畫個俄羅斯國旗吧
去年才去過,是一個令人會想再去一次的國家
var russia = CGRect(x:0, y:0, width:300, height:200)
let russiaBarHeight = russia.height/3
let russiaBackgroundView = UIView(frame:russia)
let russiaWhite = UIView(frame: CGRect(x:0, y:0, width:russiaBackgroundView.frame.width, height:russiaBarHeight))
russiaWhite.backgroundColor = UIColor(red:1,green:1, blue:1, alpha:1)
russiaBackgroundView.addSubview(russiaWhite)
let russiaBlue = UIView(frame:russiaWhite.frame.offsetBy(dx:0, dy:russiaBarHeight))
russiaBlue.backgroundColor = UIColor(red:0, green:0.2784, blue:0.7294, alpha:1)
russiaBackgroundView.addSubview(russiaBlue)
let russiaRed = UIView(frame:russiaBlue.frame.offsetBy(dx: 0, dy:russiaBarHeight))
russiaRed.backgroundColor = UIColor(red:0.835, green:0.169, blue:0.118, alpha:1)
russiaBackgroundView.addSubview(russiaRed)
5. 最後,看到Peter稱讚南韓國旗很厲害,那就來畫個北韓吧
金小胖萬歲!!!
北韓國旗分成五層:
(1) 底層藍色長方形
(2) 倒數第二層白色長方形
(3) 紅色長方形
(4) 白色圓形
(5) 紅色星形
import UIKit
var northKorea = CGRect(x:0, y:0, width:200, height:100)
let northKoreaBackgroundView = UIView(frame:northKorea)
northKoreaBackgroundView.backgroundColor = UIColor(red:0.0082, green:0.3251, blue:2/3, alpha:1)
northKorea = CGRect(x:0, y:16, width:200, height:68)
let northKoreaWhiteRect = UIView(frame: northKorea)
northKoreaWhiteRect.backgroundColor = UIColor(red:1, green:1, blue:1, alpha:1)
northKoreaBackgroundView.addSubview(northKoreaWhiteRect)
northKorea = CGRect(x:0, y:20, width:200, height:60)
let northKoreaRedRect = UIView(frame: northKorea)
northKoreaRedRect.backgroundColor = UIColor(red:0.929, green:0.11, blue:0.153, alpha:1)
northKoreaBackgroundView.addSubview(northKoreaRedRect)
northKorea = CGRect(x:134/3, y:28, width:44, height:44)
let northKoreaWhiteCircle = UIView(frame:northKorea)
northKoreaWhiteCircle.backgroundColor = UIColor(red:1, green:1, blue:1, alpha:1)
northKoreaBackgroundView.addSubview(northKoreaWhiteCircle)
northKoreaWhiteCircle.layer.cornerRadius = 22
northKoreaWhiteCircle.clipsToBounds = true
northKoreaBackgroundView
let path = UIBezierPath()
path.move(to: CGPoint(x:66.67, y:28.49))
path.addLine(to: CGPoint(x:61.85, y:43.33))
path.addLine(to: CGPoint(x:46.22, y:43.376))
path.addLine(to: CGPoint(x:58.84, y:52.59))
path.addLine(to: CGPoint(x:54.028, y:67.417))
path.addLine(to: CGPoint(x:66.7, y:58.257))
path.addLine(to: CGPoint(x:79.352, y:67.437))
path.addLine(to: CGPoint(x:74.67, y:52.565))
path.addLine(to: CGPoint(x:87.134, y:43.38))
path.addLine(to: CGPoint(x:71.78, y:43.26))
path.close()
let northKoreaStarView = UIView(frame: northKoreaBackgroundView.frame)
northKoreaStarView.backgroundColor = UIColor(red:0.929, green:0.11, blue:0.153, alpha:1)
let starShapeLayer = CAShapeLayer()
starShapeLayer.path = path.cgPath
northKoreaStarView.layer.mask = starShapeLayer
northKoreaBackgroundView.addSubview(northKoreaStarView)