從程式製作國旗 俄羅斯和紐西蘭

陳璟元
海大 SwiftUI iOS / Flutter App 程式設計
3 min readOct 25, 2018

我先從間單的開始做起,所以選了俄羅斯來練習,加上熟悉語法

然後我開始嘗試較為困難的紐西蘭,因為紐西蘭是我想去的國家之一,看看當地的火山、溫泉、冰河、湖泊,享受南半球的氣候

第一部分

製作出左上角英國的圖案,有許多前人製作的英國國旗,讓我在製作上快了一些,雖然算比例還是讓我頭痛了一下

再來是星星的部分

同樣也是非常感謝版上的同學,使我在學習如何繪製星星時困擾少了許多,因為紐西蘭上有白色星星和紅色星星,所以很想用function的方式,不過還不是很熟悉用法,索性就直接寫下去,然後我在做星星時才發現 .append不然可以省下一些不必要的程式碼了,最後紐西蘭國旗不是完美的100%一模一樣,但還是有開心一下下。

附上程式碼

import UIKit//俄羅斯var rect1 = CGRect(x: 0, y: 0, width: 300, height: 210)let backgroundView1 = UIView(frame: rect1)backgroundView1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)rect1 = CGRect(x: 0, y: 70, width: 300, height: 70)let blue = UIView(frame: rect1)blue.backgroundColor = UIColor(red: 0, green: 57.0/255.0, blue: 166.0/255.0, alpha: 1)rect1 = CGRect(x: 0, y: 140, width: 300, height: 70)let red = UIView(frame: rect1)red.backgroundColor = UIColor(red: 213.0/255.0, green: 43.0/255.0, blue: 30.0/255.0, alpha: 1)backgroundView1.addSubview(blue)backgroundView1.addSubview(red)// 紐西蘭// 藍底var flag = CGRect(x: 0, y: 0, width: 240, height: 120)let flagbackgroundView = UIView(frame: flag)flagbackgroundView.backgroundColor = UIColor(red: 0, green: 38/255, blue: 127/255, alpha: 1)//白色部分var white = CGRect(x: 0, y: 0, width: 120, height: 60)let whiteView = UIView(frame: white)whiteView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)flagbackgroundView.addSubview(whiteView)let Path = UIBezierPath()var point = CGPoint(x: 0, y: 0)Path.move(to: point)point = CGPoint(x: 13.2, y: 0)Path.addLine(to: point)point = CGPoint(x: 120, y: 53.4)Path.addLine(to: point)point = CGPoint(x: 120, y: 60)Path.addLine(to: point)point = CGPoint(x: 106.8, y: 60)Path.addLine(to: point)point = CGPoint(x: 0, y: 6.6)Path.addLine(to: point)Path.close()let Layer = CAShapeLayer()Layer.path = Path.cgPathwhiteView.layer.mask = Layervar white1 = CGRect(x: 0, y: 0, width: 120, height: 60)let whiteView1 = UIView(frame: white1)whiteView1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)flagbackgroundView.addSubview(whiteView1)let Path1 = UIBezierPath()var point1 = CGPoint(x: 106.8, y: 0)Path1.move(to: point1)point1 = CGPoint(x: 120, y: 0)Path1.addLine(to: point1)point1 = CGPoint(x: 120, y: 6.6)Path1.addLine(to: point1)point1 = CGPoint(x: 13.2, y: 60)Path1.addLine(to: point1)point1 = CGPoint(x: 0, y: 60)Path1.addLine(to: point1)point1 = CGPoint(x: 0, y: 53.4)Path1.addLine(to: point1)Path1.close()let Layer1 = CAShapeLayer()Layer1.path = Path1.cgPathwhiteView1.layer.mask = Layer1var white2 = CGRect(x: 0, y: 0, width: 120, height: 60)let whiteView2 = UIView(frame: white2)whiteView2.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)flagbackgroundView.addSubview(whiteView2)let Path2 = UIBezierPath()var point2 = CGPoint(x: 49.5, y: 0)Path2.move(to: point2)point2 = CGPoint(x: 69.3, y: 0)Path2.addLine(to: point2)point2 = CGPoint(x: 69.3, y: 60)Path2.addLine(to: point2)point2 = CGPoint(x: 49.5, y: 60)Path2.addLine(to: point2)Path2.close()let Layer2 = CAShapeLayer()Layer2.path = Path2.cgPathwhiteView2.layer.mask = Layer2var white3 = CGRect(x: 0, y: 0, width: 120, height: 60)let whiteView3 = UIView(frame: white3)whiteView3.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)flagbackgroundView.addSubview(whiteView3)let Path3 = UIBezierPath()var point3 = CGPoint(x: 0, y: 19.6)Path3.move(to: point3)point3 = CGPoint(x: 120, y: 19.6)Path3.addLine(to: point3)point3 = CGPoint(x: 120, y: 39.6)Path3.addLine(to: point3)point3 = CGPoint(x: 0, y: 39.6)Path3.addLine(to: point3)Path3.close()let Layer3 = CAShapeLayer()Layer3.path = Path3.cgPathwhiteView3.layer.mask = Layer3flagbackgroundViewvar red1 = CGRect(x: 0, y: 0, width: 120, height: 60)let redView1 = UIView(frame: red1)redView1.backgroundColor = UIColor(red: 210.0/255.0, green: 16.0/255.0, blue: 52.0/255.0, alpha: 1)flagbackgroundView.addSubview(redView1)let Path4 = UIBezierPath()var point4 = CGPoint(x: 52.8, y: 0)Path4.move(to: point4)point4 = CGPoint(x: 66.6, y: 0)Path4.addLine(to: point4)point4 = CGPoint(x: 66.6, y: 60)

參考資料:

--

--