用 UIBezierPath 畫 Marvel logo
Published in
3 min readDec 22, 2019
//
// ViewController.swift
// 7
//
// Created by Apple on 2019/12/7.
// Copyright © 2019 Apple. All rights reserved.
//import UIKitclass ViewController: UIViewController {
func draw(moveX:CGFloat,moveY:CGFloat,Degree:CGFloat,size:CGFloat)
{
let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 2000, height: 2000))
backgroundView.backgroundColor = UIColor(red: 171/255, green: 44/255, blue: 40/255, alpha: 1)
let redPath = UIBezierPath(ovalIn: CGRect(x: 250, y: 250, width: 1500 , height: 1500))
let redShape = CAShapeLayer()
redShape.path = redPath.cgPath
backgroundView.layer.mask = redShape
let whitePath = UIBezierPath(ovalIn: CGRect(x: 400, y: 400, width: 1200, height: 1200))
let whiteShape = CAShapeLayer()
whiteShape.path = whitePath.cgPath
whiteShape.fillColor = UIColor.white.cgColor
backgroundView.layer.addSublayer(whiteShape)
let red2Path = UIBezierPath(ovalIn: CGRect(x: 550, y: 550, width: 900, height: 900))
let red2Shpae = CAShapeLayer()
red2Shpae.path = red2Path.cgPath
red2Shpae.fillColor = UIColor(red: 171/255, green: 44/255, blue: 40/255, alpha: 1).cgColor
backgroundView.layer.addSublayer(red2Shpae)
let bluePath = UIBezierPath(ovalIn: CGRect(x: 700, y: 700, width: 600, height: 600))
let blueShpae = CAShapeLayer()
blueShpae.path = bluePath.cgPath
blueShpae.fillColor = UIColor(red: 3/255, green: 13/255, blue: 26/255, alpha: 1).cgColor
backgroundView.layer.addSublayer(blueShpae)
let starPath = UIBezierPath()
starPath.move(to: CGPoint(x: 1000, y: 700))
starPath.addLine(to: CGPoint(x: 933, y: 907))
starPath.addLine(to: CGPoint(x: 715, y: 907))
starPath.addLine(to: CGPoint(x: 891, y: 1036))
starPath.addLine(to: CGPoint(x: 824, y: 1243))
starPath.addLine(to: CGPoint(x: 1000, y: 1114))
starPath.addLine(to: CGPoint(x: 1176, y: 1243))
starPath.addLine(to: CGPoint(x: 1109, y: 1036))
starPath.addLine(to: CGPoint(x: 1285, y: 907))
starPath.addLine(to: CGPoint(x: 1067, y: 907))
starPath.close()
let starShape = CAShapeLayer()
starShape.path = starPath.cgPath
starShape.fillColor = UIColor.white.cgColor
backgroundView.layer.addSublayer(starShape)
backgroundView.transform = CGAffineTransform(translationX: moveX, y: moveY).scaledBy(x: 0.1, y: 0.1)
// view.addSubview(backgroundView)
let path = UIBezierPath()
path.move(to: CGPoint(x:340, y:15))
path.addLine(to: CGPoint(x:414, y:15))
path.addLine(to: CGPoint(x: 418, y: 380))
path.addLine(to: CGPoint(x: 339, y: 295))
path.addLine(to: CGPoint(x: 339, y: 178))
path.addLine(to: CGPoint(x: 260, y: 348))
path.addLine(to: CGPoint(x: 340, y: 348))
path.addLine(to: CGPoint(x: 340, y: 311))
path.addLine(to: CGPoint(x: 414, y: 390))
path.addLine(to: CGPoint(x: 340, y: 450))
path.addLine(to: CGPoint(x: 340, y: 420))
path.addLine(to: CGPoint(x: 235, y: 420))
path.addLine(to: CGPoint(x: 152, y: 588))
path.addLine(to: CGPoint(x: 63, y: 588))
path.close()
//draw triangle
path.move(to: CGPoint(x: 420, y: 400))
path.addLine(to: CGPoint(x: 422, y: 465))
path.addLine(to: CGPoint(x: 340, y: 465))
path.close()
//draw circle
let aDegree = CGFloat.pi/180
path.move(to: CGPoint(x: 452, y: 66))
path.addArc(withCenter: CGPoint(x: 315, y: 310), radius: 280, startAngle: aDegree*300, endAngle: aDegree*118, clockwise: true)
path.addLine(to: CGPoint(x: 206, y: 526))
path.addArc(withCenter: CGPoint(x: 315, y: 310), radius: 240, startAngle: aDegree*116, endAngle: aDegree*302, clockwise: false)
path.close()
path.move(to: CGPoint(x: 310, y: 35))
path.addArc(withCenter: CGPoint(x: 315, y: 310), radius: 280, startAngle: aDegree*269.6, endAngle: aDegree*141, clockwise: false)
path.addLine(to: CGPoint(x: 113, y: 462.5))
path.addArc(withCenter: CGPoint(x: 315, y: 310), radius: 240, startAngle: aDegree*144.2, endAngle: aDegree*265, clockwise: true)
path.close()
let AvengersLayer = CAShapeLayer()
AvengersLayer.path = path.cgPath
let AvengersView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000))
AvengersView.backgroundColor = UIColor(red: 0.63, green: 0.66, blue: 0.564, alpha: 1)
AvengersView.layer.mask = AvengersLayer
AvengersView.transform = CGAffineTransform(translationX: moveX, y: moveY).scaledBy(x: size, y: size).rotated(by: Degree)
view.addSubview(AvengersView)
}
override func viewDidLoad() {
super.viewDidLoad()
draw(moveX: -100, moveY: 0, Degree: 0, size: 0.5)
draw(moveX: -200, moveY: 100, Degree:CGFloat.pi / 180*30, size: 0.3)
draw(moveX: -200, moveY: 200, Degree:CGFloat.pi / 180*21, size: 0.3)
draw(moveX: -200, moveY: 300, Degree:CGFloat.pi / 180*60, size: 0.3)
draw(moveX: -300, moveY: 400, Degree:CGFloat.pi / 180*10, size: 0.4)
draw(moveX: -500, moveY: 500, Degree:CGFloat.pi / 180*90, size: 0.6)
draw(moveX: -400, moveY: -200, Degree:CGFloat.pi / 180*80, size: 0.6)
}}