[英]How to make Inner shadow in SwiftUI
I'd like to make View's (Text, Button, etc) Inner shadow in SwiftUI我想在 SwiftUI 中制作 View 的(文本、按钮等)内部阴影
There is Outer shadow but there is no Inner shadow in SwiftUI SwiftUI 中有外阴影但没有内阴影
What I want to make is Neumorphism UI using SwiftUI我想做的是使用 SwiftUI 的 Neumorphism UI
Image from "Neumorphism in user interfaces" by Michal Malewicz
图片来自Michal Malewicz的“用户界面中的新拟态”
I'd like to make button pressed UI我想让按钮按下 UI
but I don't know where to start to make inner shadow但我不知道从哪里开始制作内影
This is what I did to create an inner shadow like the one in the picture.这就是我为创建一个像图片中的阴影一样的内部阴影所做的。 I created it in another swiftui file and just called in in my main content view but you can display it however you'd like.
我在另一个 swiftui 文件中创建了它,并在我的主要内容视图中调用了它,但您可以随意显示它。
I created a Button in a ZStack only because I first recreated it with a rounded rectangle but I think this would would in a HStack or VStack as well just haven't tried them.我在 ZStack 中创建了一个 Button 只是因为我首先用圆角矩形重新创建了它,但我认为这也会在 HStack 或 VStack 中进行,只是还没有尝试过。 To create the inner shadow I created an overlay and clipped the shadows to the shape.
为了创建内部阴影,我创建了一个叠加层并将阴影剪裁到形状上。
ZStack{
Button(action: {}){
Image(systemName: "heart.fill")
.resizable()
.frame(width: 40, height: 40)
.padding(25)
.foregroundColor(Color(red: 52/255, green: 57/255, blue: 133/255))
.overlay(
RoundedRectangle(cornerRadius: 15)
.stroke(Color(red: 236/255, green: 234/255, blue: 235/255),
lineWidth: 4)
.shadow(color: Color(red: 192/255, green: 189/255, blue: 191/255),
radius: 3, x: 5, y: 5)
.clipShape(
RoundedRectangle(cornerRadius: 15)
)
.shadow(color: Color.white, radius: 2, x: -2, y: -2)
.clipShape(
RoundedRectangle(cornerRadius: 15)
)
)
.background(Color(red: 236/255, green: 234/255, blue: 235/255))
.cornerRadius(20)
}
The end result looked like this:最终结果如下所示:
You can play around with the colors and the shadows to get exactly what you want but hopefully this helps!你可以玩弄颜色和阴影来得到你想要的东西,但希望这会有所帮助!
You could use a SwiftUI shape as background.您可以使用 SwiftUI 形状作为背景。
Use .fill(.shadow(.inner(radius: 1, y: 1)))
for inner shadow.使用
.fill(.shadow(.inner(radius: 1, y: 1)))
作为内阴影。
The .foregroundColor
of the shape is the background color of the button.形状的
.foregroundColor
是按钮的背景颜色。
For a RoundedRectangle Button same as the question对于与问题相同的圆角矩形按钮
Button(action: {}){
Image(systemName: "heart.fill")
.resizable()
.frame(width: 40, height: 40)
.padding(25)
.foregroundColor(Color(red: 52/255, green: 57/255, blue: 133/255))
.background(
RoundedRectangle(cornerRadius: 20, style: .continuous)
.fill(
.shadow(.inner(color: Color(red: 197/255, green: 197/255, blue: 197/255),radius: 3, x:3, y: 3))
.shadow(.inner(color: .white, radius: 3, x: -3, y: -3))
)
.foregroundColor(Color(red: 236/255, green: 234/255, blue: 235/255)))
}
}
For a Circle Button对于圆形按钮
Button(action: {}){
Image(systemName: "heart.fill")
.resizable()
.frame(width: 40, height: 40)
.padding(25)
.foregroundColor(Color(red: 52/255, green: 57/255, blue: 133/255))
.background(
Circle()
.fill(
.shadow(.inner(color: Color(red: 197/255, green: 197/255, blue: 197/255),radius: 5, x:3, y: 3))
.shadow(.inner(color: .white, radius:5, x: -3, y: -3))
)
.foregroundColor(Color(red: 236/255, green: 234/255, blue: 235/255)))
}
There is now a new ShapeStyle
called shadow
:现在有一个名为
shadow
的新ShapeStyle
:
From the documentation - the following example creates a circle filled with the current foreground style that uses an inner shadow:从文档中 - 以下示例创建一个填充了当前前景样式的圆,该样式使用内部阴影:
Circle().fill(.shadow(.inner(radius: 1, y: 1)))
In most contexts the current style is the foreground, but not always.
在大多数情况下,当前样式是前景,但并非总是如此。 For example, when setting the value of the background style, that becomes the current implicit style.
例如,设置背景样式的值时,即成为当前的隐式样式。
Circle()
.fill(.shadow(.inner(radius: 1, y: 1)))
.foregroundStyle(.red)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.