#57 從程式製作國旗圖案 — 練習回傳的 function
import UIKitfunc getCrossView() -> UIView {let view = UIView(frame: CGRect(x: 0, y: 0, width: 170, height: 105))view.backgroundColor = UIColor.whitelet hRedView = UIView(frame: CGRect(x: 0, y: 0, width: 54, height: 20))hRedView.backgroundColor = UIColor.redhRedView.center = view.centerview.addSubview(hRedView)let vRedView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 54))vRedView.backgroundColor = UIColor.redvRedView.center = view.centerview.addSubview(vRedView)return view}var points = [CGPoint(x: 0, y: 0),CGPoint(x: 230, y: 0),CGPoint(x: 0, y: 162),CGPoint(x: 230, y: 162)]let redView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 267))redView.backgroundColor = UIColor.redfor point in points {let crossView = getCrossView()crossView.frame.origin = pointredView.addSubview(crossView)}redView
import UIKitlet backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 254, height: 169))backgroundView.backgroundColor = UIColor.whitefunc getRectView() -> UIView {let rectView = UIView(frame: CGRect(x: 0, y: 0, width: backgroundView.frame.width, height: backgroundView.frame.height / 9))rectView.backgroundColor = UIColor(red: 5/255, green: 94/255, blue: 177/255, alpha: 1)return rectView}var rectPoints = [CGPoint(x: 0, y: 0),CGPoint(x: 0, y: backgroundView.frame.height / 9 * 2),CGPoint(x: 0, y: backgroundView.frame.height / 9 * 4),CGPoint(x: 0, y: backgroundView.frame.height / 9 * 6),CGPoint(x: 0, y: backgroundView.frame.height / 9 * 8),]for point in rectPoints {let rectViews = getRectView()rectViews.frame.origin = pointbackgroundView.addSubview(rectViews)}let whiteSquareView = UIView(frame: CGRect(x: 0, y: 0, width: backgroundView.frame.height / 9 * 5, height: backgroundView.frame.height / 9 * 5))whiteSquareView.backgroundColor = UIColor.whitefunc getCrossView() -> UIView {let squareView = UIView(frame: CGRect(x: 0, y: 0, width: backgroundView.frame.height / 9 * 2, height: backgroundView.frame.height / 9 * 2))squareView.backgroundColor = UIColor(red: 5/255, green: 94/255, blue: 177/255, alpha: 1)return squareView}var squarePoints = [CGPoint(x: 0, y: 0),CGPoint(x: backgroundView.frame.height / 9 * 3, y: 0),CGPoint(x: 0, y: backgroundView.frame.height / 9 * 3),CGPoint(x: backgroundView.frame.height / 9 * 3, y: backgroundView.frame.height / 9 * 3)]for point in squarePoints {let squareCrossView = getCrossView()squareCrossView.frame.origin = pointwhiteSquareView.addSubview(squareCrossView)}backgroundView.addSubview(whiteSquareView)