iOS-從程式製作國旗圖案-韓國國旗

因為喜歡吃泡菜,所以來做做看韓國國旗~~

最先要解決的問題當然是中間的圓,有曲線,所以先用Bezier畫曲線來做個mask~ 有一點要注意!! 當mask套用到圓上面時,座標是依照圓的座標來設定的~ 意思是整張圖是330x220px,但是圓是120x120,所以這個mask的x軸是從0開始,到120結束~!

繪製曲線:

建立兩個圓形,把mask套用到紅色圓形上面,如上圖

接著要做旁邊的黑白線條,我是先畫4塊大黑色,之後再畫白色塊來扣掉~

寫了function,匯入4個座標+想畫黑色還是白色,用來加入Path

function寫法可以參考上文

用上文方法畫出四邊形

接著就是把座標一個個寫,畫出Path~

白色線也如法炮製,最後得出韓國國旗~

成功了~看到這邊,有沒有覺得有濃濃的泡菜味飄出呢~

附上全部程式碼~

import UIKitvar rect = CGRect(x: 0, y: 0, width: 330, height: 220)
let backgroundView = UIView(frame: rect)
backgroundView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
rect = CGRect(x: 0, y: 0, width: 330, height: 220)
let ViewBlack = UIView(frame: rect)
ViewBlack.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
backgroundView.addSubview(ViewBlack)
rect = CGRect(x: 0, y: 0, width: 330, height: 220)
let ViewWhite = UIView(frame: rect)
ViewWhite.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
backgroundView.addSubview(ViewWhite)
rect = CGRect(x: (backgroundView.frame.width — 120)/2, y: (backgroundView.frame.height — 120) / 2, width: 120, height: 120)
let circleViewBlue = UIView(frame: rect)
circleViewBlue.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 1)
circleViewBlue.layer.cornerRadius = 60
circleViewBlue.clipsToBounds = true
backgroundView.addSubview(circleViewBlue)
let circleViewRed = UIView(frame: rect)
circleViewRed.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
circleViewRed.layer.cornerRadius = 60
circleViewRed.clipsToBounds = true
backgroundView.addSubview(circleViewRed)
let startPoint = CGPoint(x: 0, y: 60)
let endPoint = CGPoint(x: 120, y: 60)
let controlPoint1 = CGPoint(x: 60, y: 120)
let controlPoint2 = CGPoint(x: 60, y: 0)
let path = UIBezierPath()
path.move(to: startPoint)
path.addCurve(to: endPoint, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
var point = CGPoint(x: 120, y: 0)
path.addLine(to: point)
point = CGPoint(x: 0, y: 0)
path.addLine(to: point)
point = CGPoint(x: 0, y: 60)
path.addLine(to: point)
path.close()
path
let maskLayerRed = CAShapeLayer()
maskLayerRed.path = path.cgPath
circleViewRed.layer.mask = maskLayerRed
circleViewRedcircleViewBluelet AllBlackPath = UIBezierPath()
let AllWhitePath = UIBezierPath()
func drawPath(p1:CGPoint,p2:CGPoint,p3:CGPoint,p4:CGPoint,allPath:UIBezierPath){
let nowPath = UIBezierPath()
nowPath.move(to: p1)
nowPath.addLine(to: p2)
nowPath.addLine(to: p3)
nowPath.addLine(to: p4)
nowPath.close()
allPath.append(nowPath)
}
drawPath(p1:CGPoint(x: 78, y: 23),
p2:CGPoint(x: 108, y: 43),
p3:CGPoint(x: 78, y: 87),
p4:CGPoint(x: 48, y: 67),
allPath:AllBlackPath)
drawPath(p1:CGPoint(x: 219, y: 43),
p2:CGPoint(x: 249, y: 23),
p3:CGPoint(x: 279, y: 67),
p4:CGPoint(x: 249, y: 87),
allPath:AllBlackPath)
drawPath(p1:CGPoint(x: 78, y: 133),
p2:CGPoint(x: 48, y: 153),
p3:CGPoint(x: 78, y: 196),
p4:CGPoint(x: 108, y: 176),
allPath:AllBlackPath)
drawPath(p1:CGPoint(x: 219, y: 176),
p2:CGPoint(x: 249, y: 196),
p3:CGPoint(x: 279, y: 153),
p4:CGPoint(x: 249, y: 133),
allPath:AllBlackPath)
AllBlackPathdrawPath(p1:CGPoint(x: 86, y: 28),
p2:CGPoint(x: 90, y: 31),
p3:CGPoint(x: 60, y: 75),
p4:CGPoint(x: 56, y: 72),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 97, y: 35),
p2:CGPoint(x: 101, y: 38),
p3:CGPoint(x: 71, y: 82),
p4:CGPoint(x: 67, y: 79),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 226, y: 37),
p2:CGPoint(x: 230, y: 34),
p3:CGPoint(x: 260, y: 79),
p4:CGPoint(x: 256, y: 82),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 238, y: 29),
p2:CGPoint(x: 242, y: 27),
p3:CGPoint(x: 272, y: 71),
p4:CGPoint(x: 268, y: 74),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 54, y: 148),
p2:CGPoint(x: 58, y: 145),
p3:CGPoint(x: 89, y: 189),
p4:CGPoint(x: 84, y: 192),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 66, y: 140),
p2:CGPoint(x: 70, y: 136),
p3:CGPoint(x: 101, y: 181),
p4:CGPoint(x: 96, y: 184),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 256, y: 136),
p2:CGPoint(x: 260, y: 140),
p3:CGPoint(x: 230, y: 184),
p4:CGPoint(x: 226, y: 180),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 268, y: 144),
p2:CGPoint(x: 272, y: 147),
p3:CGPoint(x: 242, y: 192),
p4:CGPoint(x: 238, y: 188),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 231, y: 63),
p2:CGPoint(x: 240, y: 56),
p3:CGPoint(x: 244, y: 61),
p4:CGPoint(x: 234, y: 68),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 255, y: 46),
p2:CGPoint(x: 263, y: 41),
p3:CGPoint(x: 267, y: 46),
p4:CGPoint(x: 258, y: 51),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 70, y: 165),
p2:CGPoint(x: 79, y: 159),
p3:CGPoint(x: 83, y: 164),
p4:CGPoint(x: 74, y: 170),
allPath:AllWhitePath)
drawPath(p1:CGPoint(x: 235, y: 151),
p2:CGPoint(x: 266, y: 172),
p3:CGPoint(x: 263, y: 177),
p4:CGPoint(x: 232, y: 156),
allPath:AllWhitePath)
AllWhitePathlet maskLayerBlack = CAShapeLayer()
maskLayerBlack.path = AllBlackPath.cgPath
ViewBlack.layer.mask = maskLayerBlack
let maskLayerWhite = CAShapeLayer()
maskLayerWhite.path = AllWhitePath.cgPath
ViewWhite.layer.mask = maskLayerWhite
backgroundView

--

--