#3 自畫像

運用SwiftUI ZStack (深度堆疊),設計。

程式碼:

import SwiftUI

struct ContentView: View {
var body: some View {
ZStack {

Capsule() //頭髮
.frame(width: 385, height: 600)
.foregroundStyle(Color.black)
.offset(y: 160)

Ellipse() //臉
.frame(width: 300, height: 300)
.foregroundStyle(Color(CGColor(red: 251/255, green: 229/255, blue: 214/255, alpha: 1)))
.offset(y: 60)

Rectangle() //脖子
.frame(width: 60, height: 100)
.foregroundStyle(Color(CGColor(red: 251/255, green: 229/255, blue: 214/255, alpha: 1)))
.offset(y: 240)

Circle() //頭髮
.trim(from: 0.5, to: 1)
.frame(width: 370)
.foregroundStyle(Color.black)
.offset(y: 35)

Rectangle() //頭髮分邊
.trim(from: 0.5, to: 1)
.frame(width: 12, height: 70)
.foregroundStyle(Color(CGColor(red: 251/255, green: 229/255, blue: 214/255, alpha: 1)))
.offset(x: -80)
.rotationEffect(.degrees(-6))

Ellipse() //帽子圓球
.frame(width: 50, height: 50)
.foregroundStyle(Color(CGColor(red: 200/255, green: 104/255, blue: 182/255, alpha: 1)))
.offset(y: -220)
.shadow(radius: 10)

Ellipse() //帽子
.trim(from: 0.5, to: 1)
.frame(width: 375, height: 300)
.foregroundStyle(Color(CGColor(red: 200/255, green: 104/255, blue: 182/255, alpha: 1)))
.offset(y: -60)
.shadow(radius: 10)

Rectangle() //帽子反折
.frame(width: 375, height: 80)
.foregroundStyle(Color(CGColor(red: 200/255, green: 104/255, blue: 182/255, alpha: 1)))
.offset(y: -30)
.shadow(radius: 10)

Ellipse() //右眼
.frame(width: 30, height: 30)
.foregroundStyle(Color.black)
.offset(x: -80,y: 70)

Ellipse() //左眼
.frame(width: 30, height: 30)
.foregroundStyle(Color.black)
.offset(x: 80,y: 70)

//鼻子
Rectangle()
.frame(width: 25, height: 50)
.foregroundStyle(Color(CGColor(red: 248/255, green: 203/255, blue: 173/255, alpha: 1)))
.offset(x: -13,y: 120)
.rotationEffect(.degrees(-5))
Rectangle()
.frame(width: 25, height: 50)
.foregroundStyle(Color(CGColor(red: 251/255, green: 229/255, blue: 214/255, alpha: 1)))
.offset(x: -11,y: 115)
.rotationEffect(.degrees(-6))

Circle() //嘴巴
.trim(from: 0, to: 0.5)
.stroke(Color.red, style: StrokeStyle(lineWidth: 8, lineCap: .round))
.frame(width: 60)
.offset(y: 345)
.scaleEffect(y: 0.5)

RoundedRectangle(cornerRadius: 70) //衣服
.foregroundStyle(Color(CGColor(red: 112/255, green: 48/255, blue: 160/255, alpha: 1)))
.frame(width: 450, height: 400)
.offset(y: 470)

// 花朵上
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 140,y: -110)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 132,y: -126)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 115,y: -125)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 110,y: -110)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 125,y: -99)

// 花朵下
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 180,y: 380)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 172,y: 364)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 155,y: 365)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 150,y: 380)
Ellipse()
.frame(width: 20, height: 20)
.foregroundStyle(Color(CGColor(red: 255/255, green: 255/255, blue: 121/255, alpha: 1)))
.offset(x: 165,y: 391)

}
}
}

--

--