๐view.inverseMask()
blend mode - ็จ
.destinationOutๅฏไปฅ้ๅฐ้กไผผ็ๆๆใ โญ๏ธ
import SwiftUI
extension View {
// view.inverseMask(_:)
public func inverseMask<M: View>(_ mask: M) -> some View {
// exchange foreground and background
let inversed = mask
.foregroundColor(.black) // opacity = 0 (hide)
.background(Color.white) // opacity = 1 (show)
.compositingGroup() // โญ๏ธ composite all layers
// โญ๏ธ dark -> transparent (opacity = 0)
// bright -> opaque black (opacity = 1)
// ๆ้ปใ่ฒ ็ใ็ๆ่ฆบใ
.luminanceToAlpha() // โญ๏ธ turn luminance into alpha (opacity)
return self.mask(inversed)
}
}
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
// light bulb (resizable image)
let lightbulb = Image(systemName: "lightbulb")
.resizable().scaledToFit().padding(24)
// rounded rect (shape)
let roundedRect = RoundedRectangle(cornerRadius: 20)
// rounded gradient border
let border = roundedRect
.stroke(Neu.gradient.diagonalBorderDark, lineWidth: 2)
// card container
return ZStack {
// card background
Neu.color.grayBackground
// card
Neu.gradient.horizontalDark
.inverseMask(lightbulb) // โญ๏ธ inverse mask
.shadow(color: Color.black.opacity(0.6), radius: 4, x: 4, y: 4)
.frame(width: 150, height: 200)
.clipShape(roundedRect)
.overlay(border)
.shadow(color: Color.white.opacity(0.9), radius: 18, x: -18, y: -18)
.shadow(color: Color.black.opacity(0.3), radius: 14, x: 14, y: 14)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
How to Create a Neumorphic Design With SwiftUI - RayWenderlich
็ดฐ็ฏ่ชชๆ
ๅจ .inverseMask() ไธญ็จๅฐ็ .foregroundColor(.black) ๅ .backgroud(Color.white)๏ผๅฐ Image ไพ่ชชๆฒๆไปปไฝไฝ็จ๏ผๅฆไธๅๆ็คบ๏ผ

ไฝ .luminanceToAlpha() ๆๅฐๅๅไธญใ้ป่ฒ็้จๅใ่ฝ็บใ้ๆใ(opacity = 0)๏ผ่ใ็ฝ่ฒ็้จๅใๅ่ฝ็บใๅ จ้ปใ(opacity = 1)๏ผ้ๆ็ๅๆ้ปๅ็ธ็็ใๅบ็ใ(่ฒ ็)ใ
ๅฅ็จ .mask() ็ๆๅ๏ผopacity = 1 ็ๆใไฟ็ๅไพ็่ฒๅฝฉใ๏ผopacity = 0 ็้จๅๅ่ฝ็บใ้ๆใใ้ๅฐฑๆฏ็ฒไป้บผๅๅๅฐ็็็ผ็ๅๅจๆฏ่ผ้ป๏ผๆ้ ๆๆๅพ็ๅๅจๅฐ็็ผ็ๅๅจๆไบ้ๆใ
ๅ ไธ้ป่ฒๅๆฏใ็ฝ่ฒ่ๆฏ้ฝๆๆ๏ผ้ๆๆๆๅ ฉๅๅๅฑค๏ผๅฅ็จ .compositingGroup() ๆๅไฝตๅๅฑคใๅไฝตๅพ๏ผๅๅฅ็จ .luminanceToAlpha()๏ผmask ๅๅๅๆฏ็้จๅ (ไนๅฐฑๆฏๅไพๅ็คบ็้จๅ) ่ฝ็บ้ๆ๏ผ้ๆๅๅฅ็จ .mask() ๅฐฑๅฏไปฅใๆๆใๅๅ็้ๅ้จๅ๏ผ้ไนๅฐฑๆฏ็บไป้บผ .inverseMask() ๆๆๆ็ๅๅ ใ

Last updated
Was this helpful?