元件化
將畫面中不同的元件 (component) 寫成一個獨立的 View,讓程式碼可以更有組織,也可讓主程式更精簡。
重點
「縮放、平移、旋轉」 view:
.scaleEffect() .offset() .rotationEffect() .rotation3DEffect()
設定 view 的「渲染模式」:
.blendMode() (#todo:研究渲染模式如何使用?)
幫 view 設「陰影」: .shadow(radius:)

/*
Design+Code - SwiftUI for iOS 13
https://designcode.io/swiftui-layout-and-stacks
https://designcode.io/swiftui-components-and-visual-effects
*/
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
ZStack {
TitleView()
BackCard(.purple)
.scaleEffect(0.9) // 縮放
.offset(x: 0, y: -40) // 平移
.rotationEffect(.degrees(10)) // 旋轉
.rotation3DEffect(.degrees(10), axis: (x: 1, y: 0, z: 0)) // 3D 軸轉
.blendMode(.hardLight) // 渲染模式
BackCard(.pink)
.scaleEffect(0.95)
.offset(x: 0, y: -20)
.rotationEffect(.degrees(5))
.rotation3DEffect(.degrees(5), axis: (x: 1, y: 0, z: 0))
.blendMode(.hardLight)
Card()
.blendMode(.hardLight)
BottomCard()
} //
// 讓外框自動採用「最大的寬度與高度」(而不是採用最小的)
// ⚠️ 注意:
// 不要寫成 `frame(width: .infinity, height: .infinity)`
// 否則會當掉!
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
}
}
PlaygroundPage.current.setLiveView(ContentView())Design+Code - SwiftUI for iOS 13
Apple doc
Last updated
Was this helpful?